Skip to content
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

Load AMD modules with the same crossorigin attribute as document.currentScript #801

Merged
merged 6 commits into from
Dec 6, 2018

Conversation

keanulee
Copy link
Contributor

@keanulee keanulee commented Dec 3, 2018

No description provided.

@@ -278,6 +289,7 @@ function loadDeps(

// We have a dependency on a real module.
const dependency = getModule(resolveUrl(module.urlBase, depSpec));
dependency.crossorigin = module.crossorigin;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only set crossorigin once, just to make things more predictable.

We could use undefined to mean, not yet set to any value and check for it here.

It'd still be racy, but I'm not sure how we could ameliorate that, as we want to do as much loading as we can as early as we can. In any case, it's a pretty obscure edge case, where you import the same dep both from a script with a crossorigin and from one without.

// can still get document.currentScript. Note: IE11 doesn't support
// crossorigin attribute nor currentScript, so it will always be null.
const crossorigin = document.currentScript ?
document.currentScript.getAttribute('crossorigin') : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the default value for modules has changed to "same-origin" in Chrome 71. That's a much more useful default, we should probably default to that as well
https://www.chromestatus.com/feature/6710957388595200

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK same-origin is not a valid value for the crossorigin attribute. When applied to a script tag in Chrome:

image

Copy link
Contributor

@rictic rictic Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the source is:

<!-- No crossorigin attribute -->
<script type="module" src="src/components/my-app.js"></script>

Which AMD transforms to:

<!-- No crossorigin attribute -->
<script>define(['src/components/my-app.js']);</script>

Then the AMD version is a non-module script that would use "no-cors" mode, not "same-origin". Defaulting to crossorigin="anonymous" would break the existing behavior on builds from module source that don't include the crossorigin attribute, though adding crossorigin="anonymous" makes it closer to native module behavior.

I guess this is one of those design decisions between closer to native module behavior vs. keeping it simple (i.e. not mimicking crossorigin behavior unless the user explicitly uses the attribute). I'm inclined to stick with the latter because making this default change would break existing preload headers on AMD builds (i.e. would need to add crossorigin=anonymous; to preload headers - ref Polymer/prpl-server#85), though it's true that in these cases the preload headers on module (non-transformed) builds would never have worked properly anyways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. So the main goal – I assume – of defaulting to crossorigin="anonymous" is to behave more like normal script tags. So in that sense setting a crossorigin would make things more complicated in cases like preload headers.

On the other hand, those preload headers won't work with modules, and the goal of this loader is to be an AMD loader that behaves as much as possible like the native module loader.

How much work would it be to modify our push manifest generator to add the crossorigin property? I've approved Polymer/prpl-server#85

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default for normal script tags is no crossorigin attribute, not crossorigin="anonymous", which requires no modification to existing preload headers. Switching the default to crossorigin="anonymous" would require changes to preload headers, which is why I'm hesitant on making the change.

We (shop and pwa-starter-kit) don't rely on the push-manifest generator anymore since we're now only using bundled output and want URL-to-assets mappings, so we write our own push-manifest which we manually modify.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we're in agreement on the technical details.

I think defaulting to crossorigin="anonymous" is the right call. It will cause a bit of friction in the short term, but only friction that people would see when they use real modules.

The point of this library is to emulate modules, so I believe that's what we should do here.

@keanulee keanulee merged commit 5aebb66 into master Dec 6, 2018
@keanulee keanulee deleted the crossorigin branch December 6, 2018 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants