Laravel - Compiling Assets - Versioning And Cache Busting

Many developers suffix their compiled assets with a timestamp or unique token to force browsers to load the fresh assets instead of serving stale copies of the code. Mix can automatically handle this for you using the version method.

The version method will append a unique hash to the filenames of all compiled files, allowing for more convenient cache busting:

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

After generating the versioned file, you won't know the exact filename. So, you should use Laravel's global mix function within your views to load the appropriately hashed asset. The mix function will automatically determine the current name of the hashed file:

    
    
	

Because versioned files are usually unnecessary in development, you may instruct the versioning process to only run during npm run prod:

    
    mix.js('resources/js/app.js', 'public/js');
    
    if (mix.inProduction()) {
        mix.version();
    }