generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds header for preloading Tailwind assets
- Loading branch information
Showing
7 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Tonysm\TailwindCss\Http\Middleware; | ||
|
||
use Tonysm\TailwindCss\Manifest; | ||
|
||
class AddLinkHeaderForPreloadedAssets | ||
{ | ||
public function __construct(private Manifest $manifest) | ||
{ | ||
} | ||
|
||
public function handle($request, $next) | ||
{ | ||
return tap($next($request), function ($response) { | ||
if (count($assets = $this->manifest->assetsForPreloading()) > 0) { | ||
$response->header('Link', trim(implode(', ', array_filter([ | ||
$response->headers->get('Link', null), | ||
...collect($assets) | ||
->map(fn ($attributes, $asset) => implode('; ', [ | ||
"<$asset>", | ||
...collect(array_merge(['rel' => 'preload', 'as' => 'style'], $attributes)) | ||
->map(fn ($value, $key) => "{$key}={$value}") | ||
->all(), | ||
])) | ||
->all(), | ||
])))); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Tonysm\TailwindCss\Tests; | ||
|
||
use Illuminate\Http\Request; | ||
use Tonysm\TailwindCss\Http\Middleware\AddLinkHeaderForPreloadedAssets; | ||
|
||
class PreloadingHeaderTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function no_link_header_when_not_preloading() | ||
{ | ||
config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); | ||
|
||
$tailwindcss = tailwindcss('css/app.css', preload: false); | ||
|
||
$response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( | ||
Request::create('/'), | ||
fn () => response('hello world'), | ||
); | ||
|
||
$this->assertEquals('http://localhost/css/app-123.css', (string) $tailwindcss); | ||
$this->assertEquals('hello world', $response->content()); | ||
$this->assertNull($response->headers->get('Link', null)); | ||
} | ||
|
||
/** @test */ | ||
public function adds_link_header_when_preloading() | ||
{ | ||
config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); | ||
|
||
$tailwindcss = tailwindcss('css/app.css', preload: true); | ||
|
||
$response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( | ||
Request::create('/'), | ||
fn () => response('hello world'), | ||
); | ||
|
||
$this->assertEquals($asset = 'http://localhost/css/app-123.css', (string) $tailwindcss); | ||
$this->assertEquals('hello world', $response->content()); | ||
$this->assertEquals("<{$asset}>; rel=preload; as=style", $response->headers->get('Link', null)); | ||
} | ||
|
||
/** @test */ | ||
public function keeps_existing_preloading_link_header() | ||
{ | ||
config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); | ||
|
||
$tailwindcss = tailwindcss('css/app.css', preload: true); | ||
|
||
$response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( | ||
Request::create('/'), | ||
fn () => response('hello world')->withHeaders([ | ||
'Link' => '</js/app.js>; rel=modulepreload' | ||
]), | ||
); | ||
|
||
$this->assertEquals($asset = 'http://localhost/css/app-123.css', (string) $tailwindcss); | ||
$this->assertEquals('hello world', $response->content()); | ||
$this->assertEquals("</js/app.js>; rel=modulepreload, <{$asset}>; rel=preload; as=style", $response->headers->get('Link', null)); | ||
} | ||
|
||
/** @test */ | ||
public function adds_link_header_when_preloading_custom_attributes() | ||
{ | ||
config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); | ||
|
||
$tailwindcss = tailwindcss('css/app.css', ['crossorigin' => 'anonymous']); | ||
|
||
$response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( | ||
Request::create('/'), | ||
fn () => response('hello world'), | ||
); | ||
|
||
$this->assertEquals($asset = 'http://localhost/css/app-123.css', (string) $tailwindcss); | ||
$this->assertEquals('hello world', $response->content()); | ||
$this->assertEquals("<{$asset}>; rel=preload; as=style; crossorigin=anonymous", $response->headers->get('Link', null)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"/css/app.css": "/css/app-123.css" | ||
} |