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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,15 +45,15 @@ php artisan importmap:install | |
Next, we need to add the following component to our view or layout file: | ||
|
||
```blade | ||
<x-importmap-tags /> | ||
<x-importmap::tags /> | ||
``` | ||
|
||
Add that between your `<head>` tags. The `entrypoint` should be the "main" file, commonly the `resources/js/app.js` file, which will be mapped to the `app` module (use the module name, not the file). | ||
|
||
By default the `x-importmap-tags` component assumes your entrypoint module is `app`, which matches the existing `resources/js/app.js` file from Laravel's default scaffolding. You may want to customize the entrypoint, which you can do with the `entrypoint` prop: | ||
By default the `x-importmap::tags` component assumes your entrypoint module is `app`, which matches the existing `resources/js/app.js` file from Laravel's default scaffolding. You may want to customize the entrypoint, which you can do with the `entrypoint` prop: | ||
|
||
```blade | ||
<x-importmap-tags entrypoint="admin" /> | ||
<x-importmap::tags entrypoint="admin" /> | ||
``` | ||
|
||
The package will automatically map the `resources/js` folder to your `public/js` folder using Laravel's symlink feature. All you have to do after installing the package is run: | ||
|
@@ -173,7 +173,7 @@ The version is added as a comment to your pin so you know which version was impo | |
|
||
### Preloading Modules | ||
|
||
To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we support [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload). Pinned modules can be preloaded by appending `preload: true` to the pin, like so: | ||
To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we use [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload) by default. If you don't want to preload a dependency, because you want to load it on-demand for efficiency, append `preload: false` to the pin. | ||
|
||
```php | ||
Importmap::pinAllFrom("resources/js/", to: "js/", preload: true); | ||
|
@@ -186,6 +186,9 @@ Which will add the correct `links` tags to your head tag in the HTML document, l | |
<link rel="modulepreload" href="https://unpkg.com/[email protected]/dist/module.esm.js"> | ||
``` | ||
|
||
You may add the `AddLinkHeadersForPreloadedPins` middleware to the `web` routes group so these preloaded links are sent as a `Link` header. | ||
Add the `Tonysm\ImportmapLaravel\Http\Middleware\AddLinkHeadersForPreloadedPins` to the `web` route group so the preloaded modules are sent as the Link headers, which are used in [HTTP/2 Server Push](https://datatracker.ietf.org/doc/html/rfc7540#section-8.2) and [Resource Hints](https://html.spec.whatwg.org/#linkTypes) to push resources to the client as early as possible. Some web servers can pick up this `Link` header and convert them to [Early Hints](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/103) responses. | ||
|
||
## Dependency Maintenance Commands | ||
|
||
Maintaining a healthy dependency list can be tricky. Here are a couple of commands to help you with this task. | ||
|
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