-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add +module
to mime type for script type=module
#558
Comments
relevant to: #443 whatwg/loader#121 |
Why? What use case does this solve? |
Please see https://wiki.whatwg.org/wiki/FAQ#Is_there_a_process_for_adding_new_features_to_a_specification.3F instead of jumping to a proposed solution. |
@domenic I consider this a bug because a mime type is being overloaded. Is that considered a feature? |
MIME types are used to distinguish executable code from images mainly. If there's another use case in mind, let us know, but there's no need to make mime types up for every different possible variation. Adding a new mime type is a big burden on server deployments and needs to be counterbalanced by user-facing gains. |
Unclear on relation to images. I can't send down the parser goal from the server right now and the accept does not present the parser goal to the server. |
Can you describe your use case in more detail? If browsers added this mime type to the list of acceptable mime types for script type module, what scenarios would be enabled that were previously impossible? |
You mentioned the Accept header. HTML does not (and will not) use that for reasons discussed in https://wiki.whatwg.org/wiki/Why_not_conneg. The only place the script mime types matter is that responses with non-JS mime types are interpreted as error responses (similar to 404). |
If responses are the only thing that matters I guess I can close this; the module goal is a strict super set of the script goal (albeit with newer/nicer things). I have slight misgivings about the overloading since it changes where |
We might use |
@annevk it does change semantics though with making var statements suddenly not globally available. |
Yeah, I agree that theoretically a new MIME type can be justified, but practically I don't really see a good reason to do it. |
I think this is a reasonable idea, though I don't have a great understanding of what deployment difficulties it might create. Modules parse with different syntax than scripts; it's not just strict mode (which you could anyway get by eval'ing an XMLHttpRequest result, for example). For a script embedded in a page, this is indicated in the same document by type="module", but for a standalone js file, the module-ness is not attached to it anymore, and just indicated by the one who's including it. |
Still haven't found an answer for
|
It would have a "benefit" if it was the only type we accepted for modules. That way you can prevent classic from executing as module and vice versa. It also allows the browser to syntax-highlight module scripts correctly, when viewed as standalone file. |
Not vice versa since classic scripts ignore MIME type... although I suppose it could be changed to "ignore for everything except the module MIME type", if we really wanted. But we should keep in mind that requiring MIME types has historically not been without pain in the ass for Web developers. |
@zcorpan given https://www.w3.org/Bugs/Public/show_bug.cgi?id=27852 we can probably do vice versa if we wanted to. @domenic it's been a little hard to get it out of @bmeck, but I think the justification is that they have a platform where the host language's implementation uses the MIME type to figure out classic or module script. They would like their platform's solution to not break when the same scripts get delivered to browsers. It would be relatively trivial for us to support this case by just adding |
@annevk I got things sorted out, it was confusion on the right body to go to. We are ok now by not going through WHATWG. I mistook this as the place to register a mime, when it is really IANA I should talk to directly. |
@bmeck note that even if you register a MIME type, you'd still need to change the HTML Standard if you want it to be recognized by browsers. |
if script/modules fetches from non browser user agent, how I can find whether response is JS or ESM ? for example, I wanna fetch fetch('/script').then(res => {
const type = res.headers.get('content-type')
if (type === 'application/javascript') {
return saveBodyInFile("script.js", res.text());
} else if (type === 'application/javascript+modules') {
return saveBodyInFile("script.mjs", res.text());
}
}) this seems help env like node.js who need to know whether response from http is JS or EJS but not know metadata in HTML (means |
You can't find out without knowing the context. It's easy to produce files that are both valid classic and module scripts. Without a script element or import statement to judge which the author intended, either or both could be correct. This is somewhat similar to how you can't tell the difference between LICENSE.txt and README.txt via mime types; both are text/plain. (Not sure this is the best analogy, but it's what I can come up with at the moment...) |
thats seems not the point of this. I think that the point is “execution context need to know it’s script or esm before exec/parsing it, so put them in METADATA” why only in mime-type, its ok to leave both are same ? |
Protocols are not useful on their own. They are useful in the context of consumers. Browser consumers are able to differentiate one way. Node is able to differentiate another. I'd suggest any other consumers figure out their own counterparts for differentiating. E.g. perhaps your script could take a command-line flag. |
FWIW, I disagree with that and I wish I had tried somewhat harder for a new MIME type, but the ship has sailed at this point. Perhaps at some point we can have some kind of |
Browsers have knowledge about file extensions actually for the purposes of |
See w3c/FileAPI#51 about standardizing that. |
@annevk wouldn't that invalidate the note in the MIME sniffing standard? If so, wouldn't |
It wouldn't, because the resource is not retrieved over HTTP. And even if it would map to that, it would still be parsed as ordinary JavaScript when fetched via |
@annevk |
I'm just illustrating how the must requirement you cited above ends up being false in major implementations. |
@annevk we can amend the draft then. Would changing it to have an additional usage restriction that |
Unless you actually change the HTML Standard that |
@annevk |
How can it be out-of-scope for such a broad requirement? |
@annevk the same way that I can serve |
We can't change |
It actually ends up rejecting a number of MIME types these days: https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-mime-type? |
@annevk I'm open to suggestions, but still think it doesn't respect MIMEs since it lets you use |
That's true, but we also don't have a statement somewhere that says that resources labeled with |
@annevk That seems a different argument about if MIME is reliable in all situations. |
It's #558 (comment) rephrased. |
@annevk open to suggestions but don't agree about the usage restriction being false? |
It's weird actually. Why is a usage restriction phrased as a requirement on clients? If it was something like ".mjs must only be used for resources intended to be parsed as module scripts" that'd be more acceptable. |
@annevk wouldn't that phrasing effectively remove the restriction entirely? It is about intent rather than any sort of concrete property of a resource. |
@bmeck what is the restriction you're trying to enforce? Currently what you have reads as a restriction on a consumer of the resource, which seems wrong for a usage restriction (those should apply to the producer only). |
@annevk the ability to author to the Module goal of ECMAScript and not have it ambiguous with Script. This has different limitations on environments that are server/client (Server/Browser) or just environment (Node).
I don't see how they only apply to producers. |
Already having issues with it in Chrome 62: Failed to load module script: The server responded with a non-JavaScript MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec. I'm trying to use type="module" with "index.mjs" |
@jakubrpawlowski on Apache you could do something like |
@annevk Thank you for your answer. For those of you using Node try node-mime's mime.define. Relevant article: What Happens When We Serve JavaScript With Random Mime Types Unfortunately since I use PhoneGap CLI to serve the app this is not a solution for me. I will just have to stick to .js extension and using babel for now. |
Possibly/probably needed or useful for data: uris. |
Please add
+module
to the mime type since this is a different parsing goal and fails current parsing forapplication/javascript
.The text was updated successfully, but these errors were encountered: