我应该为 JavaScript 源映射文件使用什么 MIME 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19911929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:13:37  来源:igfitidea点击:

What MIME type should I use for JavaScript source-map files?

javascripthtmlhttpsource-maps

提问by James A. Rosen

I want to add source mapsto my site, but I'd like to exercise some control over how they're served. What is an appropriate MIME type to use for them?

我想将源地图添加到我的网站,但我想对它们的服务方式进行一些控制。什么是适合它们的 MIME 类型?

Some data

一些数据

  • The content itself is JavaScript, but not meant to be executed as such.
  • CDN.js serves http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.mapwith Content-Type: application/octet-stream.
  • Google serves http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.mapwith Content-Type: application/json
  • The Source Maps spec states that maps should begin with )]}to prevent them from being evaluated as actual JavaScript (and thus exposing cross-site scripting attacks). That makes the file invalid JSON and valid, but un-runnable JavaScript.
  • 内容本身是 JavaScript,但并不意味着要这样执行。
  • CDN.jshttp://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.mapContent-Type: application/octet-stream.
  • Googlehttp://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.mapContent-Type: application/json
  • Source Maps 规范指出,地图应该首先)]}防止它们被评估为实际的 JavaScript(从而暴露跨站点脚本攻击)。这使文件成为无效的 JSON 和有效但不可运行的 JavaScript。

回答by Paul Irish

application/octet-streamis fine.

application/octet-stream很好。

On the Google CDN we do it as application/jsononly so you can view source in the browser.

在 Google CDN 上,我们这样做application/json只是为了您可以在浏览器中查看源代码。

No DevTools actually care what the content-type is, so you can ignore it safely.

没有 DevTools 实际上关心内容类型是什么,所以你可以安全地忽略它。