Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cache busting using Laravel Mix and CDN (Laraco...

Cache busting using Laravel Mix and CDN (Laracon EU Amsterdam 2019)

Laravel Mix is an easy-to-use tool for building assets without writing Webpack config. It works out of the box for many use cases.

However, in real-world applications which have high traffic or global audience, you need to serve assets from CDN for the purpose of performance gain. But Laravel Mix doesn’t support uploading and serving assets from CDN by default.

You can achieve this by customizing Laravel Mix configuration. But it's not straightforward to make it work with CDN caching mechanism.

In this presentation, I’ll walk you through how to serve assets from CDN with proper cache busting strategy using Laravel Mix.

Ryuta Hamasaki

August 30, 2019
Tweet

More Decks by Ryuta Hamasaki

Other Decks in Technology

Transcript

  1. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); mix.js('resources/js/app.js', 'public/js')

    .sass('resources/sass/app.scss', 'public/css'); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] });
  2. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); mix.js('resources/js/app.js', 'public/js')

    .sass('resources/sass/app.scss', 'public/css'); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] });
  3. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); mix.js('resources/js/app.js', 'public/js')

    .sass('resources/sass/app.scss', 'public/css'); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] });
  4. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); mix.js('resources/js/app.js', 'public/js')

    .sass('resources/sass/app.scss', 'public/css'); if (mix.inProduction()) { mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] }); }
  5. new s3Plugin({ include: /.*\.(css|js)$/, s3Options: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,

    region: process.env.AWS_DEFAULT_REGION, }, s3UploadOptions: { Bucket: process.env.AWS_BUCKET, CacheControl: 'public, max-age=31536000' }, directory: 'public' })
  6. new s3Plugin({ include: /.*\.(css|js)$/, s3Options: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,

    region: process.env.AWS_DEFAULT_REGION, }, s3UploadOptions: { Bucket: process.env.AWS_BUCKET, CacheControl: 'public, max-age=31536000' }, directory: 'public' })
  7. new s3Plugin({ include: /.*\.(css|js)$/, s3Options: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,

    region: process.env.AWS_DEFAULT_REGION, }, s3UploadOptions: { Bucket: process.env.AWS_BUCKET, CacheControl: 'public, max-age=31536000' }, directory: 'public' })
  8. new s3Plugin({ include: /.*\.(css|js)$/, s3Options: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,

    region: process.env.AWS_DEFAULT_REGION, }, s3UploadOptions: { Bucket: process.env.AWS_BUCKET, CacheControl: 'public, max-age=31536000' }, directory: 'public' })
  9. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); mix.js('resources/js/app.js', 'public/js')

    .sass('resources/sass/app.scss', 'public/css'); if (mix.inProduction()) { mix.version(); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] }); }
  10. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); mix.js('resources/js/app.js', 'public/js')

    .sass('resources/sass/app.scss', 'public/css'); if (mix.inProduction()) { mix.version(); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] }); }
  11. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); require('laravel-mix-versionhash'); mix.js('resources/js/app.js',

    'public/js') .sass('resources/sass/app.scss', 'public/css'); if (mix.inProduction()) { mix.version(); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] }); }
  12. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); require('laravel-mix-versionhash'); mix.js('resources/js/app.js',

    'public/js') .sass('resources/sass/app.scss', 'public/css'); if (mix.inProduction()) { mix.version(); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] }); }
  13. const mix = require('laravel-mix'); const s3Plugin = require('webpack-s3-plugin'); require('laravel-mix-versionhash'); mix.js('resources/js/app.js',

    'public/js') .sass('resources/sass/app.scss', 'public/css'); if (mix.inProduction()) { mix.versionHash(); mix.webpackConfig({ plugins: [ new s3Plugin({ // }) ] }); }
  14. JS Framework CLIs - Standalone SPA - Filename based versioning

    by default - Testing, Linting, TypeScript, PWA, etc
  15. WRAP UP - If you use a CDN, stick to

    filename based versioning - Query string versioning vs Filename based versioning
  16. WRAP UP - Other options to achieve filename based versioning

    → Webpack or JS Framework CLIs - If you use a CDN, stick to filename based versioning - Query string versioning vs Filename based versioning