Skip to content

Commit

Permalink
sort preloading tags while generating
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Sep 27, 2022
1 parent 2ef7dec commit 39e78dd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 48 deletions.
19 changes: 10 additions & 9 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,30 @@ public function __invoke($entrypoints, $buildDirectory = null)
foreach ($entrypoints as $entrypoint) {
$chunk = $this->chunk($manifest, $entrypoint);

$preloads->push($this->makePreloadTagForChunk(
$preloads->push([
$chunk['src'],
$this->assetPath("{$buildDirectory}/{$chunk['file']}"),
$chunk,
$manifest
));
]);

foreach ($chunk['imports'] ?? [] as $import) {
$preloads->push($this->makePreloadTagForChunk(
$preloads->push([
$import,
$this->assetPath("{$buildDirectory}/{$manifest[$import]['file']}"),
$manifest[$import],
$manifest
));
]);

foreach ($manifest[$import]['css'] ?? [] as $css) {
$partialManifest = Collection::make($manifest)->where('file', $css);

$preloads->push($this->makePreloadTagForChunk(
$preloads->push([
$partialManifest->keys()->first(),
$this->assetPath("{$buildDirectory}/{$css}"),
$partialManifest->first(),
$manifest
));
]);

$tags->push($this->makeTagForChunk(
$partialManifest->keys()->first(),
Expand All @@ -302,12 +302,12 @@ public function __invoke($entrypoints, $buildDirectory = null)
foreach ($chunk['css'] ?? [] as $css) {
$partialManifest = Collection::make($manifest)->where('file', $css);

$preloads->push($this->makePreloadTagForChunk(
$preloads->push([
$partialManifest->keys()->first(),
$this->assetPath("{$buildDirectory}/{$css}"),
$partialManifest->first(),
$manifest
));
]);

$tags->push($this->makeTagForChunk(
$partialManifest->keys()->first(),
Expand All @@ -320,7 +320,8 @@ public function __invoke($entrypoints, $buildDirectory = null)

[$stylesheets, $scripts] = $tags->partition(fn ($tag) => str_starts_with($tag, '<link'));

$preloads = $preloads->sortByDesc(fn ($link) => str_contains($link, 'rel="preload"'));
$preloads = $preloads->sortByDesc(fn ($args) => $this->isCssPath($args[1]))
->map(fn ($args) => $this->makePreloadTagForChunk(...$args));

return new HtmlString($preloads->join('').$stylesheets->join('').$scripts->join(''));
}
Expand Down
1 change: 0 additions & 1 deletion src/Illuminate/Http/Middleware/VitePreloading.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function handle($request, $next)
if (Vite::preloadedAssets() !== []) {
$response->header('Link', Collection::make(Vite::preloadedAssets())
->map(fn ($attributes, $url) => "<{$url}>; ".implode('; ', $attributes))
->sortByDesc(fn ($link) => str_contains($link, 'rel="preload"'))
->join(', '));
}
});
Expand Down
20 changes: 10 additions & 10 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,16 @@ public function testItGeneratesPreloadDirectivesForJsAndCssImports()
.'<script type="module" src="https://example.com/'.$buildDir.'/assets/Login.8c52c4a3.js"></script>', $result->toHtml()
);
$this->assertSame([
'https://example.com/'.$buildDir.'/assets/app.9842b564.css' => [
'rel="preload"',
'as="style"',
],
'https://example.com/'.$buildDir.'/assets/Login.8c52c4a3.js' => [
'rel="modulepreload"',
],
'https://example.com/'.$buildDir.'/assets/app.a26d8e4d.js' => [
'rel="modulepreload"',
],
'https://example.com/'.$buildDir.'/assets/app.9842b564.css' => [
'rel="preload"',
'as="style"',
],
'https://example.com/'.$buildDir.'/assets/AuthenticationCard.47ef70cc.js' => [
'rel="modulepreload"',
],
Expand Down Expand Up @@ -813,16 +813,17 @@ public function testItCanSpecifyAttributesForPreloadedAssets()
$result->toHtml());

$this->assertSame([
"https://example.com/$buildDir/assets/app.versioned.js" => [
'rel="modulepreload"',
"https://example.com/$buildDir/assets/app.versioned.css" => [
'rel="preload"',
'as="style"',
'general="attribute"',
'crossorigin',
'data-persistent-across-pages="YES"',
'keep-me',
'empty-string=""',
'zero="0"',
],
"https://example.com/$buildDir/assets/import.versioned.js" => [
"https://example.com/$buildDir/assets/app.versioned.js" => [
'rel="modulepreload"',
'general="attribute"',
'crossorigin',
Expand All @@ -831,9 +832,8 @@ public function testItCanSpecifyAttributesForPreloadedAssets()
'empty-string=""',
'zero="0"',
],
"https://example.com/$buildDir/assets/app.versioned.css" => [
'rel="preload"',
'as="style"',
"https://example.com/$buildDir/assets/import.versioned.js" => [
'rel="modulepreload"',
'general="attribute"',
'crossorigin',
'data-persistent-across-pages="YES"',
Expand Down
28 changes: 0 additions & 28 deletions tests/Http/Middleware/VitePreloadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,4 @@ public function testItAddsPreloadLinkHeader()
'<https://laravel.com/app.js>; rel="modulepreload"; foo="bar"'
);
}

public function testItPrioritizesCss()
{
$app = new Container();
$app->instance(Vite::class, new class extends Vite
{
protected $preloadedAssets = [
'https://laravel.com/app.js' => [
'rel="modulepreload"',
'foo="bar"',
],
'https://laravel.com/app.css' => [
'rel="preload"',
'bar="baz"',
],
];
});
Facade::setFacadeApplication($app);

$response = (new VitePreloading)->handle(new Request, function () {
return new Response('Hello Laravel');
});

$this->assertSame(
$response->headers->get('Link'),
'<https://laravel.com/app.css>; rel="preload"; bar="baz", <https://laravel.com/app.js>; rel="modulepreload"; foo="bar"'
);
}
}

0 comments on commit 39e78dd

Please sign in to comment.