Skip to content

Commit

Permalink
[9.x] Output only unique asset / preload tags (#45404)
Browse files Browse the repository at this point in the history
* Ensures preload tags are unique

* Ensure Vite assets are unique

* style fix
  • Loading branch information
timacdonald authored Dec 23, 2022
1 parent 02e27f8 commit bfab0d5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ public function __invoke($entrypoints, $buildDirectory = null)
}
}

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

$preloads = $preloads->sortByDesc(fn ($args) => $this->isCssPath($args[1]))
$preloads = $preloads->unique()
->sortByDesc(fn ($args) => $this->isCssPath($args[1]))
->map(fn ($args) => $this->makePreloadTagForChunk(...$args));

return new HtmlString($preloads->join('').$stylesheets->join('').$scripts->join(''));
Expand Down
51 changes: 51 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,57 @@ public function testItCanConfigureTheManifestFilename()
rmdir(public_path($buildDir));
}

public function testItOnlyOutputsUniquePreloadTags()
{
$buildDir = Str::random();
$this->makeViteManifest([
'resources/js/app.css' => [
'file' => 'assets/app-versioned.css',
'src' => 'resources/js/app.css',
],
'resources/js/Pages/Welcome.vue' => [
'file' => 'assets/Welcome-versioned.js',
'src' => 'resources/js/Pages/Welcome.vue',
'imports' => [
'resources/js/app.js',
],
],
'resources/js/app.js' => [
'file' => 'assets/app-versioned.js',
'src' => 'resources/js/app.js',
'css' => [
'assets/app-versioned.css',
],
],
], $buildDir);

$result = app(Vite::class)(['resources/js/app.js', 'resources/js/Pages/Welcome.vue'], $buildDir);

$this->assertSame(
'<link rel="preload" as="style" href="https://example.com/'.$buildDir.'/assets/app-versioned.css" />'
.'<link rel="modulepreload" href="https://example.com/'.$buildDir.'/assets/app-versioned.js" />'
.'<link rel="modulepreload" href="https://example.com/'.$buildDir.'/assets/Welcome-versioned.js" />'
.'<link rel="stylesheet" href="https://example.com/'.$buildDir.'/assets/app-versioned.css" />'
.'<script type="module" src="https://example.com/'.$buildDir.'/assets/app-versioned.js"></script>'
.'<script type="module" src="https://example.com/'.$buildDir.'/assets/Welcome-versioned.js"></script>',
$result->toHtml());

$this->assertSame([
"https://example.com/$buildDir/assets/app-versioned.css" => [
'rel="preload"',
'as="style"',
],
"https://example.com/$buildDir/assets/app-versioned.js" => [
'rel="modulepreload"',
],
"https://example.com/$buildDir/assets/Welcome-versioned.js" => [
'rel="modulepreload"',
],
], ViteFacade::preloadedAssets());

$this->cleanViteManifest($buildDir);
}

protected function makeViteManifest($contents = null, $path = 'build')
{
app()->singleton('path.public', fn () => __DIR__);
Expand Down

0 comments on commit bfab0d5

Please sign in to comment.