Skip to content

Vite - Add required crossorigin attribute to modulepreload #53424

@indykoning

Description

@indykoning

Laravel Version

11.30.0

PHP Version

8.3

Database Driver & Version

No response

Description

CORS (the crossorigin parameter) is required when dealing with scripts of type="module" or rel="modulepreload"
whatwg/html#1888

Thus adding these links as headers will prove ineffective both in Http/2 ServerPush and Http/3 Early Hints.
We might want to default the crossorigin parameter to anonymous to ensure preloading will always be effective.

'crossorigin' => $this->resolveScriptTagAttributes($src, $url, $chunk, $manifest)['crossorigin'] ?? false,

Steps To Reproduce

Set up your regular laravel project, run yarn && yarn run build
add the link elements to the link header.

In a clean Laravel installation update your web.php to reflect

    return response(view('welcome'))->header('Link', '<http://laravel.test/build/assets/app-<your hash>.js>; as=script; rel=preload');

See the console warning in your browser claiming the preload has not been used due to incompatible crossorigin between your link headers and your script.
image

Also note in the network tab how the script is downloaded twice.
image

First during the preload, but since that is thrown away. The second time because it finds the script in your html.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions