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

[9.x] Inherit crossorigin attributes while preloading #44800

Merged
merged 3 commits into from
Nov 1, 2022

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Nov 1, 2022

"CORS"...the thing every developer wants to wake up and hear about πŸ’†β€β™‚οΈ

This PR improves upon the preloading functionality with Vite by having preload tags inherit the crossorigin attribute from their standard script / link tag counterpart.

If an app is adding a crossorigin attribute...

Vite::useScriptTagAttributes([
    'crossorigin' => true,
]);

That results in the following HTML...

<link rel="modulepreload" href="https://exmaple.com/build/assets/app.0b16cbf2.js">
<script type="module" src="https://exmaple.com/build/assets/app.0b16cbf2.js" crossorigin></script>

The trouble is that the two tags must have matching crossorigin tags, otherwise a double fetch is performed. To manually solve this, you can specify the crossorigin tag for preload tags...

Vite::useScriptTagAttributes([
    'crossorigin' => true,
]);

Vite::usePreloadTagAttributes([
    'crossorigin' => true,
]);

After this PR, there is no need to manually specify the preload tag attribute for crossorigin, as it will be inherited from the useScriptTagAttributes setup.

This means that the following setup...

Vite::useScriptTagAttributes([
    'crossorigin' => true,
]);

results in the following HTML...

<link rel="modulepreload" href="https://exmaple.com/build/assets/app.0b16cbf2.js" crossorigin>
<script type="module" src="https://exmaple.com/build/assets/app.0b16cbf2.js" crossorigin></script>

where both tags have the same crossorigin attribute. A develop may still override the preload tag attribute for the crossorigin attribute, however I can't see any reason why you would ever want to do this.

@timacdonald timacdonald changed the title [9.x] Inherit crossorigin attributes [9.x] Inherit crossorigin attributes while preloading Nov 1, 2022
@taylorotwell taylorotwell merged commit 243a8f9 into laravel:9.x Nov 1, 2022
@timacdonald timacdonald deleted the preload-crossorigin branch November 1, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants