Skip to content

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

Merged
taylorotwell merged 3 commits intolaravel:9.xfrom
timacdonald:preload-crossorigin
Nov 1, 2022
Merged

[9.x] Inherit crossorigin attributes while preloading#44800
taylorotwell merged 3 commits intolaravel:9.xfrom
timacdonald:preload-crossorigin

Conversation

@timacdonald
Copy link
Copy Markdown
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