footo23
footo23

Reputation: 11

Laravel Mix: Using Mix() to cache bust around another helper

I have a helper defined below:

function cdn($file)
{
    return env('CDN_URI', '').'/'.ltrim($file, '/');
}

In my blade, I am serving the bundled Vue js file with the helper:

<script type="text/javascript" src="{{ cdn('/dist/js/app.js') }}"></script>

I am having caching issues now so I want to start using versioning. I read in Laravel Mix docs that to imoport the versioned files, put them in the mix() . Would I be able to wrap that around my other helper as follows:

<script type="text/javascript" src="{{ mix(cdn('/dist/js/app.js')) }}"></script>```

Upvotes: 1

Views: 839

Answers (1)

user633440
user633440

Reputation:

To enable versioning, you need to add the following in your webpack.mix.js when you are compiling your assets.

mix.version();

Something along the lines of:

const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .version();

Then after you add this to your template:

<script src="{{ mix('js/app.js') }}"></script>

Compile your assets with npm run prod, and if you view the source, you will see something like the following where you put your JavaScript.

<script src="/js/app.js?id=476befa31a13c804b084"></script>

Finally, you can add your cdn() helper, make sure to wrap the helper around the mix() function.

<script src="{{ cdn(mix('js/app.js')) }}"></script>

Upvotes: 1

Related Questions