├── .github ├── FUNDING.yml └── workflows │ ├── php-cs-fixer.yml │ ├── run-tests.yml │ └── update-changelog.yml ├── .php_cs ├── .styleci.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── MixPreloadServiceProvider.php └── RenderPreloadLinks.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: spatie 2 | custom: https://spatie.be/open-source/support-us 3 | -------------------------------------------------------------------------------- /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | style: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | 13 | - name: Fix style 14 | uses: docker://oskarstark/php-cs-fixer-ga 15 | with: 16 | args: --config=.php_cs --allow-risky=yes 17 | 18 | - name: Extract branch name 19 | shell: bash 20 | run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" 21 | id: extract_branch 22 | 23 | - name: Commit changes 24 | uses: stefanzweifel/git-auto-commit-action@v2.3.0 25 | with: 26 | commit_message: Fix styling 27 | branch: ${{ steps.extract_branch.outputs.branch }} 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | - push 5 | 6 | jobs: 7 | test: 8 | runs-on: ${{ matrix.os }} 9 | 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | php: [7.3, 7.4, 8.0, 8.1, '8.2'] 14 | dependency-version: [prefer-lowest, prefer-stable] 15 | os: [ubuntu-latest, windows-latest] 16 | 17 | name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 18 | 19 | steps: 20 | - name: Checkout code 21 | uses: actions/checkout@v2 22 | 23 | - name: Cache dependencies 24 | uses: actions/cache@v2 25 | with: 26 | path: ~/.composer/cache/files 27 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 28 | 29 | - name: Setup PHP 30 | uses: shivammathur/setup-php@v2 31 | with: 32 | php-version: ${{ matrix.php }} 33 | extensions: dom, curl, libxml, mbstring, zip 34 | coverage: none 35 | 36 | - name: Install dependencies 37 | run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 38 | 39 | - name: Execute tests 40 | run: vendor/bin/phpunit 41 | -------------------------------------------------------------------------------- /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v2 14 | with: 15 | ref: main 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v4 25 | with: 26 | branch: main 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md 29 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | notPath('bootstrap/*') 5 | ->notPath('storage/*') 6 | ->notPath('vendor') 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/tests', 10 | ]) 11 | ->name('*.php') 12 | ->notName('*.blade.php') 13 | ->ignoreDotFiles(true) 14 | ->ignoreVCS(true); 15 | 16 | return PhpCsFixer\Config::create() 17 | ->setRules([ 18 | '@PSR2' => true, 19 | 'array_syntax' => ['syntax' => 'short'], 20 | 'ordered_imports' => ['sortAlgorithm' => 'alpha'], 21 | 'no_unused_imports' => true, 22 | 'not_operator_with_successor_space' => true, 23 | 'trailing_comma_in_multiline_array' => true, 24 | 'phpdoc_scalar' => true, 25 | 'unary_operator_spaces' => true, 26 | 'binary_operator_spaces' => true, 27 | 'blank_line_before_statement' => [ 28 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 29 | ], 30 | 'phpdoc_single_line_var_spacing' => true, 31 | 'phpdoc_var_without_name' => true, 32 | 'class_attributes_separation' => [ 33 | 'elements' => [ 34 | 'method', 'property', 35 | ], 36 | ], 37 | 'method_argument_space' => [ 38 | 'on_multiline' => 'ensure_fully_multiline', 39 | 'keep_multiple_spaces_after_comma' => true, 40 | ] 41 | ]) 42 | ->setFinder($finder); 43 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | - self_accessor 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-mix-preload` will be documented in this file. 4 | 5 | ## 1.4.0 - 2024-02-29 6 | 7 | ### What's Changed 8 | 9 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-mix-preload/pull/15 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-mix-preload/compare/1.3.1...1.4.0 12 | 13 | ## 1.3.1 - 2023-02-01 14 | 15 | ### What's Changed 16 | 17 | - Laravel 10.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-mix-preload/pull/14 18 | 19 | ### New Contributors 20 | 21 | - @laravel-shift made their first contribution in https://github.com/spatie/laravel-mix-preload/pull/14 22 | 23 | **Full Changelog**: https://github.com/spatie/laravel-mix-preload/compare/1.3.0...1.3.1 24 | 25 | ## 1.3.0 - 2022-02-01 26 | 27 | - Add support for PHP 8.1 28 | - Add support for Laravel 9 29 | - Drop support for PHP 7.2 30 | 31 | ## 1.2.2 - 2020-09-09 32 | 33 | - Support Laravel 8 34 | 35 | ## 1.2.0 36 | 37 | - Add font support 38 | 39 | ## 1.1.0 40 | 41 | - Add Laravel 7.0 support 42 | 43 | ## 1.0.1 - 2020-01-20 44 | 45 | - Add support for manifests versioned with query string 46 | 47 | ## 1.0.0 - 2019-10-21 48 | 49 | - First release 50 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Add preload and prefetch links based your Mix manifest 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-mix-preload.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-mix-preload) 4 | ![Build Status](https://github.com/spatie/laravel-mix-preload/workflows/run-tests/badge.svg) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-mix-preload.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-mix-preload) 6 | 7 | ```blade 8 | 9 | Preloading things 10 | 11 | @preload 12 | 13 | ``` 14 | 15 | This package exposes a `@preload` Blade directive that renders preload and prefetch links based on the contents in `mix-manifest.json`. Declaring what should be preloaded or prefetched is simple, just make sure `preload` or `prefetch` is part of the chunk name. 16 | 17 | If this is your mix manifest: 18 | 19 | ```json 20 | { 21 | "/js/app.js": "/js/app.js", 22 | "/css/app.css": "/css/app.css", 23 | "/css/prefetch-otherpagecss.css": "/css/prefetch-otherpagecss.css", 24 | "/js/preload-biglibrary.js": "/js/preload-biglibrary.js", 25 | "/js/vendors~preload-biglibrary.js": "/js/vendors~preload-biglibrary.js" 26 | } 27 | ``` 28 | 29 | The following links will be rendered: 30 | 31 | ```html 32 | 33 | 34 | 35 | ``` 36 | 37 | Not sure what this is about? Read Addy Osmani's article [Preload, Prefetch And Priorities in Chrome](https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf). 38 | 39 | ## Support us 40 | 41 | [](https://spatie.be/github-ad-click/laravel-mix-preload) 42 | 43 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 44 | 45 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 46 | 47 | ## Installation 48 | 49 | You can install the package via composer: 50 | 51 | ```bash 52 | composer require spatie/laravel-mix-preload 53 | ``` 54 | 55 | ## Usage 56 | 57 | Add a `@preload` directive to your applications layout file(s). 58 | 59 | ```blade 60 | 61 | 62 | 63 | ... 64 | @preload 65 | 66 | 67 | ... 68 | 69 | 70 | ``` 71 | 72 | You can determine which scripts need to be preloaded or prefetched by making sure `preload` or `prefetch` is part of their file names. You can set the file name by creating a new entry in Mix, or by using dynamic imports. 73 | 74 | ### Adding a second entry 75 | 76 | By default, Laravel sets up Mix with a single `app.js` entry. If you have another script outside of `app.js` that you want to have preloaded, make sure `preload` is part of the entry name. 77 | 78 | ```js 79 | mix 80 | .js('resources/js/app.js', 'public/js'); 81 | .js('resources/js/preload-maps.js', 'public/js'); 82 | ``` 83 | 84 | If you want to prefetch the script instead, make sure `prefetch` is part of the entry name. 85 | 86 | ```js 87 | mix 88 | .js('resources/js/app.js', 'public/js'); 89 | .js('resources/js/prefetch-maps.js', 'public/js'); 90 | ``` 91 | 92 | ### Using dynamic imports with custom chunk names 93 | 94 | If you want to preload a chunk of your application scripts, make sure `preload` is part of the chunk name. You can use Webpack's magic `webpackChunkName` comment to set the module's chunk name. 95 | 96 | ```js 97 | import('./maps' /* webpackChunkName: "preload-maps" */).then(maps => { 98 | maps.init(); 99 | }); 100 | ``` 101 | 102 | The same applies to prefetching. 103 | 104 | ```js 105 | import('./maps' /* webpackChunkName: "prefetch-maps" */).then(maps => { 106 | maps.init(); 107 | }); 108 | ``` 109 | 110 | ### Testing 111 | 112 | ``` bash 113 | composer test 114 | ``` 115 | 116 | ### Changelog 117 | 118 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 119 | 120 | ## Contributing 121 | 122 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 123 | 124 | ### Security 125 | 126 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 127 | 128 | ## Postcardware 129 | 130 | You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. 131 | 132 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 133 | 134 | We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards). 135 | 136 | ## Credits 137 | 138 | - [Sebastian De Deyne](https://github.com/sebastiandedeyne) 139 | - [All Contributors](../../contributors) 140 | 141 | ## License 142 | 143 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 144 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-mix-preload", 3 | "description": "Add preload and prefetch links based your Mix manifest", 4 | "keywords": [ 5 | "spatie", 6 | "laravel-mix-preload" 7 | ], 8 | "homepage": "https://github.com/spatie/laravel-mix-preload", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Sebastian De Deyne", 13 | "email": "sebastian@spatie.be", 14 | "homepage": "https://spatie.be", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.3|^8.0", 20 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^9.5.7|^10.5" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Spatie\\MixPreload\\": "src" 28 | } 29 | }, 30 | "autoload-dev": { 31 | "psr-4": { 32 | "Spatie\\MixPreload\\Tests\\": "tests" 33 | } 34 | }, 35 | "scripts": { 36 | "test": "vendor/bin/phpunit", 37 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 38 | }, 39 | "config": { 40 | "sort-packages": true 41 | }, 42 | "extra": { 43 | "laravel": { 44 | "providers": [ 45 | "Spatie\\MixPreload\\MixPreloadServiceProvider" 46 | ] 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/MixPreloadServiceProvider.php: -------------------------------------------------------------------------------- 1 | "; 14 | }); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/RenderPreloadLinks.php: -------------------------------------------------------------------------------- 1 | manifest = $manifest; 31 | } 32 | 33 | public function __invoke(): HtmlString 34 | { 35 | return $this->getManifestEntries() 36 | ->mapSpread(function (string $path, string $name) { 37 | $rel = $this->getRelAttribute($name); 38 | 39 | if (! $rel) { 40 | return null; 41 | } 42 | 43 | $as = $this->getAsAttribute($path); 44 | 45 | if (! $as) { 46 | return null; 47 | } 48 | 49 | return ""; 50 | }) 51 | ->filter() 52 | ->pipe(function (Collection $links) { 53 | return new HtmlString($links->implode("\n")); 54 | }); 55 | } 56 | 57 | protected function getManifestEntries(): Collection 58 | { 59 | return collect($this->manifest) 60 | ->map(function (string $path, string $name) { 61 | return [$path, $name]; 62 | }) 63 | ->values(); 64 | } 65 | 66 | protected function getRelAttribute(string $name): ?string 67 | { 68 | if (Str::contains($name, 'preload')) { 69 | return 'preload'; 70 | } 71 | 72 | if (Str::contains($name, 'prefetch')) { 73 | return 'prefetch'; 74 | } 75 | 76 | return null; 77 | } 78 | 79 | protected function getAsAttribute(string $path): ?string 80 | { 81 | if (Str::contains($path, '.js')) { 82 | return 'script'; 83 | } 84 | 85 | if (Str::contains($path, '.css')) { 86 | return 'style'; 87 | } 88 | 89 | if (Str::contains($path, ['.woff', '.woff2', '.ttf', '.eot', '.svg', '.ttc'])) { 90 | return 'font'; 91 | } 92 | 93 | return null; 94 | } 95 | } 96 | --------------------------------------------------------------------------------