【发布时间】:2020-01-26 04:56:51
【问题描述】:
我想将 Polymer LitElement 与 Go 后端一起使用。 使用 LitElement,我在 JavaScript 模块中实现了 Web 组件! 对于服务器端的路由,我使用这样的 Gorilla Mux
mux := mux.NewRouter()
mux.PathPrefix("/").Handler(http.FileServer(http.Dir("./wwwroot")))
这会正确加载静态 html 文件。当 html 文件引用实现 Web 组件的 js 文件时,我收到以下错误(在 Chrome 中):
加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。
当我将组件模块重命名为具有扩展名 mjs 时,文件正确加载,但 LitElement 的模块无法加载并出现相同的错误。由于我对所有第三方 JavaScript 模块的文件扩展名没有影响,我不知道如何解决这个问题。
(我想如果我使用 Polymer 3 而不是 LitElement,我会遇到同样的问题)
有什么想法吗?
更新
这是使用 curl 请求 lit-element.js JavaScript 模块的输出
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
注意内容类型!!!
【问题讨论】:
-
您的问题分析有问题。 FileServer 在内部使用 ServeContent,而 ServeContent 为您管理内容类型。 golang.org/pkg/net/http/#ServeContent 可以试试 curl js 资源并分享结果吗?
-
好的,看看我的更新
标签: javascript go gorilla polymer-3.x lit-element