Skip to content

Commit 9ea933a

Browse files
committed
[Preload] Preload module script entries as modulepreload
A `preload; as=script` hint uses classic-script fetch semantics that mismatch a `<script type=module>`, so the browser discards the preloaded response. modulepreload matches the module fetch, with or without SRI.
1 parent 2fda3e8 commit 9ea933a

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/Asset/TagRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public function renderScriptTags(string $entryName, ?string $packageName = null,
8383
$tagAttributes = ['src' => $url, 'type' => 'module'] + $attributes + $this->scriptAttributes;
8484
$this->applyIntegrity($tagAttributes, $reference, $integrity);
8585
$tags[] = \sprintf('<script %s></script>', $this->attributes($tagAttributes));
86-
$this->preload($url, 'preload', 'script', $reference, $integrity);
86+
// modulepreload, not `preload as=script`: the tag is a module, and a classic-script preload
87+
// mismatches its credentials/CORS mode so the browser discards it.
88+
$this->preload($url, 'modulepreload', null, $reference, $integrity);
8789
}
8890

8991
return implode('', $tags);

tests/Asset/TagRendererTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ public function testRegistersPreloadLinksOnTheRequestWhenWebLinkIsAvailable()
273273
}
274274

275275
$this->assertSame(['modulepreload'], $byHref['/build/shared-e5.js']['rels']);
276-
$this->assertSame(['preload'], $byHref['/build/app-a1b2.js']['rels']);
277-
$this->assertSame('script', $byHref['/build/app-a1b2.js']['as']);
276+
// The <script type=module> entry is preloaded as modulepreload, not `preload as=script` (whose
277+
// classic-script fetch mode mismatches the module and gets discarded by the browser).
278+
$this->assertSame(['modulepreload'], $byHref['/build/app-a1b2.js']['rels']);
279+
$this->assertNull($byHref['/build/app-a1b2.js']['as']);
278280
$this->assertSame(['preload'], $byHref['/build/app-c3.css']['rels']);
279281
$this->assertSame('style', $byHref['/build/app-c3.css']['as']);
280282
}

0 commit comments

Comments
 (0)