Skip to content

Handling of 301 redirects within script type module #613

@guybedford

Description

@guybedford

The implementation seems great, but I do have one small worry with the handling of redirects.

The problem

Correct me if I'm wrong, but to describe the use case here:

Say I have a server that acts like a registry, where https://registry.server.com/x.js redirects to https://some-cdn.com/x.js.

If I create a module tag that loads from https://registry.server.com/x.js it will store https://registry.server.com/x.js as the module registry record for x.

Now if https://some-cdn.com/y.js depends on import './x.js', that would resolve to https://some-cdn.com/x.js, and we'd end up with two registry entries for x, which would mean executing the code for x twice.

This kind of immediately defeats the point of supporting 301 redirects in the first place surely?

Naive Solution

When I was first writing code for these use cases, the way to handle this scenario seemed simple - we let the contents of https://registry.server.com/x.js be:

export * from 'https://some-cdn.com/x.js';

That then does the required thing by creating two entries in the registry, both reflecting the same module value without double execution of x.

Unfortunately though, in the spec, there is an exception that default is not included in export *. Thus if x had a default export we'd need to know that in advance and add an additional export {default} from 'https://some-cdn.com/x.js. This expectation of needing to know the shape of x ahead of time, breaks the isolation of concerns of creating a registry server that is independent of the package structure. Breaking package upgrades would need to be catered for in the registry, meaning it would need to be effectively a versioned registry service.

Please let me know if the problem and attempted solution are clear here? I'd be interested to hear thoughts, as it is small cases like this that can really break workflows. Supporting 301 redirects is already something that doesn't naturally fit into the module loader conceptual model (whatwg/loader#124 (comment)), and to do it for a use case that is effectively broken (as far as I can tell and please let me know if I'm wrong), seems unfortunate.

//cc @domenic

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions