Skip to content

Commit

Permalink
Prevent overwriting Link headers in AddLinkHeadersForPreloadedAssets …
Browse files Browse the repository at this point in the history
…middleware (#53463)
  • Loading branch information
jnoordsij authored Nov 11, 2024
1 parent f405bf0 commit 378bd56
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle($request, $next)
if ($response instanceof Response && Vite::preloadedAssets() !== []) {
$response->header('Link', Collection::make(Vite::preloadedAssets())
->map(fn ($attributes, $url) => "<{$url}>; ".implode('; ', $attributes))
->join(', '));
->join(', '), false);
}
});
}
Expand Down
29 changes: 28 additions & 1 deletion tests/Http/Middleware/VitePreloadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function testItAddsPreloadLinkHeader()
});

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

Expand All @@ -79,4 +79,31 @@ public function testItDoesNotAttachHeadersToNonIlluminateResponses()

$this->assertNull($response->headers->get('Link'));
}

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

$response = (new AddLinkHeadersForPreloadedAssets)->handle(new Request, function () {
return new Response('Hello Laravel', headers: ['Link' => '<https://laravel.com/logo.png>; rel="preload"; as="image"']);
});

$this->assertSame(
[
'<https://laravel.com/logo.png>; rel="preload"; as="image"',
'<https://laravel.com/app.js>; rel="modulepreload"; foo="bar"',
],
$response->headers->all('Link'),
);
}
}

0 comments on commit 378bd56

Please # to comment.