javascriptgogorillapolymer-3.xlit-element

Golang and JavaScript modules


I want to use Polymer LitElement with a Go backend. With LitElement I implement the web components in JavaScript modules! For routing on the server-side I use Gorilla Mux like this

mux := mux.NewRouter()
mux.PathPrefix("/").Handler(http.FileServer(http.Dir("./wwwroot")))

This loads static html files correctly. When a html file references a js file that implements a web component I get the following error (in Chrome):

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

When I rename my component module to have the extension mjs the file loads correctly but then the module for LitElement fails to load with the same error. Since I have no influence on the file extensions of all third party JavaScript modules I don't know how to fix this.

(I guess I would experience the same problems if I were using Polymer 3 instead of LitElement)

Any ideas?

Update

Here is the output from requesting the lit-element.js JavaScript module with curl

PS C:\Test\Polymer\LitElement> curl http://localhost:8082/node_modules/lit-element/lit-element.js

StatusCode        : 200
StatusDescription : OK
Content           : /**
                     * @license
                     * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
                     * This code may only be used under the BSD style license found at
                     * http://polymer.github.io/LICENSE.txt
                     * Th...
RawContent        : HTTP/1.1 200 OK
                    Accept-Ranges: bytes
                    Content-Length: 8925
                    Content-Type: text/plain; charset=utf-8
                    Date: Thu, 26 Sep 2019 11:38:23 GMT
                    Last-Modified: Sat, 26 Oct 1985 08:15:00 GMT

                    /**
                     * @licen...
Forms             : {}
Headers           : {[Accept-Ranges, bytes], [Content-Length, 8925], [Content-Type, text/plain; charset=utf-8], [Date,
                    Thu, 26 Sep 2019 11:38:23 GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 8925

Notice the Content-Type!!!


Solution

  • Are you sure you hit the right end point ?

    See that small example (that you can try on your host to give a check)

    $ tree
    .
    ├── main.go
    └── wwwroot
        └── test.js
    
    1 directory, 2 files
    
    $ cat main.go 
    package main
    
    import (
        "net/http"
    
        "github.com/gorilla/mux"
    )
    
    func main() {
        mux := mux.NewRouter()
        mux.PathPrefix("/").Handler(http.FileServer(http.Dir("./wwwroot")))
    
        http.ListenAndServe(":8080", mux)
    }
    
    
    $  cat wwwroot/test.js
    
    $ go run main.go &
    [1] 11841
    $ curl -v http://localhost:8080/test.js
    *   Trying ::1:8080...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 8080 (#0)
    > GET /test.js HTTP/1.1
    > Host: localhost:8080
    > User-Agent: curl/7.65.3
    > Accept: */*
    > 
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < Accept-Ranges: bytes
    < Content-Length: 0
    < Content-Type: application/javascript
    < Last-Modified: Thu, 26 Sep 2019 12:12:15 GMT
    < Date: Thu, 26 Sep 2019 12:15:36 GMT
    < 
    * Connection #0 to host localhost left intact