Looking to hire Laravel developers? Try LaraJobs

laravel-mix-token maintained by kerigard

Description
Tracking changes in files generated with Laravel Mix
Last update
2022/08/06 17:57 (dev-master)
License
Links
Downloads
12

Comments
comments powered by Disqus

LaravelMixToken

Tracking changes in files created with Laravel Mix for SPA applications.

Installation

Via Composer

composer require kerigard/laravel-mix-token

Usage

You should add middleware to your api middleware group within your application's app/Http/Kernel.php file:

'api' => [
    \Kerigard\MixToken\SetMixHeader::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

Add token in an HTML meta tag:

<meta name="mix-token" content="{{ mix_token() }}">

To find out if there are changes, you need to add the X-Requested-With header to all requests to the server and check if there is a difference between the token in the meta tag and the X-Mix-Token header when answering.

import axios from 'axios'

const mixToken = document.head.querySelector('meta[name=mix-token]').content
const instance = axios.create({
  headers: { 'X-Requested-With': 'XMLHttpRequest' }
})

instance.interceptors.response.use(
  (response) => {
    if (response.headers['x-mix-token'] !== undefined && response.headers['x-mix-token'] != mixToken) {
      console.log('Mix manifest has been changed. Ask the user to refresh the page')
    }

    return response
  },
)

Change log

Please see the CHANGELOG for more information on what has changed recently.

License

MIT. Please see the LICENSE FILE for more information.