Why do ES6 module imports differ from browser?
When importing a JS module (e.g. https://example.com/module.js) from a URL that returns a 301/302 redirect to a specific version of the module (e.g. https://example.com/module-42.js), the redirected module is initiated as a separate instance. This means that when explicitly importing the module from both the initial URL and the versioned URL, two instances of the module are created:
import "https://example.com/module.js"; // -> redirects to https://example.com/module-42.js
import "https://example.com/module-42.js";
This behavior is the same in all browsers (Chrome, Firefox, Safarie), but in Deno, the redirected URL and the versioned URL both point to the same module instance and only initialize the module once.
Imo, the way Deno handles redirects makes more sense because the redirect does not just point to the same module, it points to the identical resource which should not be treated as a separate module instance. Why does deno handle it differently than all browsers?
SO: https://stackoverflow.com/questions/78785224/how-are-es6-module-imports-with-http-redirects-supposed-to-work/78801025#78801025
1 Reply
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, it seems like you've correctly identified a difference in behavior between Deno and web browsers regarding the handling of ES6 module imports that involve HTTP redirects.
In web browsers, the ES6 module imports that result in HTTP redirects are treated as separate instances, as you've described. This behavior is in line with the web's specifications, where each unique URL is treated as a separate resource. This can lead to the situation where two imports that ultimately resolve to the same content are treated as different modules because they were reached through different URLs.
On the other hand, Deno's approach to handling redirects is to treat the final destination of the redirect as the true source of the module. This means that if two imports resolve to the same final URL after redirects, Deno will treat them as the same module instance. This behavior is more in line with the philosophy of avoiding duplicate module instances and can be more efficient.
The advice provided here is not based on the Deno documentation, so please double-check the accuracy of this advice. For a more detailed explanation of why Deno handles module imports with HTTP redirects differently than browsers, you might want to check discussions on the Deno GitHub repository or ask the Deno community for insights. They might provide historical context or design rationale behind this decision.