Serve Laravel Assets from a Content Delivery Network (CDN)
This package lets you push, sync, delete and serve assets to/from a CDN of your choice e.g. AWS Cloudfront.
It adds helper methods mix_cdn()
and asset_cdn()
.
>>> env('USE_CDN')
=> true
$ php artisan asset-cdn:sync
// head.blade.php
<link rel="stylesheet" href="{{ mix_cdn('/css/app.css') }}">
<!-- Result -->
<link rel="stylesheet" href="https://cdn.mysite.com/css/app.css?id=081861342e950012abe3">
Install this package via composer:
$ composer require arubacao/asset-cdn
Also register the service provider:
Only required for Laravel <=5.4
, for Laravel >=5.5
auto-discovery is enabled.
// config/app.php
'providers' => [
// Other Service Providers
\Arubacao\AssetCdn\AssetCdnServiceProvider::class,
],
Notes:
arubacao/asset-cdn
is functional and fully tested for Laravel5.4
-8.*
on PHP7.0
,7.1
,7.2
,7.3, 7.4
Only required if you plan to manage your assets via the provided commands: asset-cdn:push
, asset-cdn:sync
, asset-cdn:empty
arubacao/asset-cdn
utilizes Laravel's Filesystem to push, sync, delete assets to/from the CDN of your choice.
Therefore, you have to configure and define a filesystem specific for CDN purposes.
Please follow the official documentation.
If you plan to use AWS S3/Cloudfront you can use this configuration:
// config/filesystem.php
'asset-cdn' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'bucket' => env('AWS_CDN_BUCKET'),
],
$ php artisan vendor:publish --provider="Arubacao\AssetCdn\AssetCdnServiceProvider"
// config/asset-cdn.php
[
'cdn_url' => 'https://cdn.mysite.com',
'filesystem' => [
'disk' => 'asset-cdn',
],
]
Only required if you plan to manage your assets via the provided commands: asset-cdn:push
, asset-cdn:sync
, asset-cdn:empty
files
always assumes a relative path from the public
directoy
-
ignoreDotFiles
Excludes "hidden" directories and files (starting with a dot). -
ignoreVCS
Ignore version control directories. -
include
Any file that matches at least oneinclude
rule, will be included. No file is included by default.-
paths
Define paths that should be available on the CDN.
The following example will match any file in anyjs
orcss
path it can find in thepublic
directory.'include' => [ 'paths' => [ 'js', 'css' ], ] /* * This config would try to find: * '/var/www/html/public/js' * '/var/www/html/public/css' * but also any other 'js' or 'css' path e.g. * '/var/www/html/public/vendor/js' * '/var/www/html/public/vendor/css' * You could explicitly exclude paths later */
-
files
Define files that should be available on the CDN.
The following example will match any file that starts withjs/back.app.js
in thepublic
directory.'include' => [ 'files' => [ 'js/app.js', ], ], /* * This config would try to find: * '/var/www/html/public/js/app.js' * but also any other file that matches the path + filename e.g. * '/var/www/html/public/vendor/js/app.js' * You could explicitly exclude these files later */
-
extensions
Define filetypes that should be available on the CDN.
The following example will match any file of type*.css
or*.js
in thepublic
directory.'include' => [ 'extensions' => [ '*.js', '*.css', ], ],
-
patterns
Define patterns for files that should be available on the CDN.
The following example will match any file that starts with lettersa
orb
in thepublic
directory./* * Patterns can be globs, strings, or regexes */ 'include' => [ 'patterns' => [ '/^[a-b]/i', // starting with letters a-b ], ],
-
-
exclude
Any file that matches at least oneexclude
rule, will be excluded. Files that are excluded will never be included, even if they have been explicitly included. Rules are identical as described above.
filesystem.options
are passed directly to the Filesystem
which eventually calls the underlying Storage driver e.g. S3.
Please refer to the corresponding storage driver documentation for available configuration options.
The following example is recommended for AWS S3.
// config/asset-cdn.php
[
'filesystem' => [
'disk' => 'asset-cdn',
'options' => [
'ACL' => 'public-read', // File is available to the public, independent of the S3 Bucket policy
'CacheControl' => 'max-age=31536000, public', // Sets HTTP Header 'cache-control'. The client should cache the file for max 1 year
],
],
]
# .env
USE_CDN=true # Enables asset-cdn
USE_CDN=false # Disables asset-cdn (default)
Recommended
Sync assets that have been defined in the config to the CDN. Only pushes changes/new assets. Deletes locally removed files on CDN.
$ php artisan asset-cdn:sync
Pushes assets that have been defined in the config to the CDN. Pushes all assets. Does not delete files on CDN.
$ php artisan asset-cdn:push
Deletes all assets from CDN, independent from config file.
$ php artisan asset-cdn:empty
Replace mix()
with mix_cdn()
.
Replace asset()
with asset_cdn()
.
Icon from www.flaticon.com
Unmaintained git repo by Vinelab for inspiration only
- Video Tutorial: How to use S3/Cloudfront
- Write test for
ignoreVCS
finder config - Write test for
ignoreDotFiles
finder config - Extend
CombinedFinderTest