generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from tonysm/preload-default
Preload default
- Loading branch information
Showing
12 changed files
with
126 additions
and
46 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
9 changes: 9 additions & 0 deletions
9
resources/views/tags.blade.php → resources/views/components/tags.blade.php
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,31 @@ | ||
<?php | ||
|
||
namespace Tonysm\ImportmapLaravel\Http\Middleware; | ||
|
||
use Tonysm\ImportmapLaravel\AssetResolver; | ||
use Tonysm\ImportmapLaravel\Facades\Importmap; | ||
|
||
class AddLinkHeadersForPreloadedPins | ||
{ | ||
public function __construct(private AssetResolver $assetsResolver = new AssetResolver()) | ||
{ | ||
} | ||
|
||
/** | ||
* Sets the Link header for preloaded pins. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handle($request, $next) | ||
{ | ||
return tap($next($request), function ($response) { | ||
if ($preloaded = Importmap::preloadedModulePaths($this->assetsResolver)) { | ||
$response->header('Link', collect($preloaded) | ||
->map(fn ($url) => "<{$url}>; rel=\"modulepreload\"") | ||
->join(', ')); | ||
} | ||
}); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
<?php | ||
|
||
namespace Tonysm\ImportmapLaravel\Tests; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Tonysm\ImportmapLaravel\AssetResolver; | ||
use Tonysm\ImportmapLaravel\Http\Middleware\AddLinkHeadersForPreloadedPins; | ||
use Tonysm\ImportmapLaravel\Importmap; | ||
|
||
class PreloadingWithLinkHeadersTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function doesnt_set_link_header_when_no_pins_are_preloaded(): void | ||
{ | ||
$this->swap(Importmap::class, $map = new Importmap(rootPath: __DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR)); | ||
|
||
$map->pin('app', preload: false); | ||
$map->pin('editor', to: 'js/rich_text.js', preload: false); | ||
$map->pinAllFrom('resources/js/', under: 'controllers', to: 'js/', preload: false); | ||
|
||
$response = (new AddLinkHeadersForPreloadedPins())->handle(new Request(), function () { | ||
return new Response('Hello World'); | ||
}); | ||
|
||
$this->assertNull($response->headers->get('Link')); | ||
} | ||
|
||
/** @test */ | ||
public function sets_link_header_when_pins_are_preloaded(): void | ||
{ | ||
$this->swap(Importmap::class, $map = new Importmap(rootPath: __DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR)); | ||
|
||
$map->pin('app', preload: true); | ||
$map->pin('editor', to: 'js/rich_text.js', preload: false); | ||
$map->pinAllFrom('resources/js/', under: 'controllers', to: 'js/', preload: true); | ||
|
||
$resolver = new class () extends AssetResolver { | ||
public function __invoke($module) | ||
{ | ||
return 'http://localhost/'.str_replace(['.js'], ['-123123.js'], $module); | ||
} | ||
}; | ||
|
||
$response = (new AddLinkHeadersForPreloadedPins($resolver))->handle(new Request(), function () { | ||
return new Response('Hello World'); | ||
}); | ||
|
||
$this->assertEquals( | ||
'<http://localhost/js/app-123123.js>; rel="modulepreload", <http://localhost/js/app-123123.js>; rel="modulepreload", <http://localhost/js/controllers/hello_controller-123123.js>; rel="modulepreload", <http://localhost/js/controllers/index-123123.js>; rel="modulepreload", <http://localhost/js/controllers/utilities/md5_controller-123123.js>; rel="modulepreload", <http://localhost/js/helpers/requests/index-123123.js>; rel="modulepreload", <http://localhost/js/libs/vendor/alpine-123123.js>; rel="modulepreload", <http://localhost/js/spina/controllers/another_controller-123123.js>; rel="modulepreload", <http://localhost/js/spina/controllers/deeper/again_controller-123123.js>; rel="modulepreload"', | ||
$response->headers->get('Link'), | ||
); | ||
} | ||
} |
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