Skip to main content
When using the ziggy:generate command to create a static configuration file, you’ll need to regenerate it whenever your routes change. This guide shows you how to automate that process.

Overview

Automatic regeneration ensures your frontend always has access to the latest route definitions without manual intervention. This is especially important during active development when routes change frequently.

Watch for Route Changes

The key is to watch your route files for changes and run php artisan ziggy:generate when they’re modified.

Vite Plugin

For projects using Vite, the vite-plugin-ziggy package provides automatic regeneration.
1

Install the plugin

2

Configure Vite

Add the plugin to your vite.config.js:
3

Start development server

The plugin will automatically regenerate routes when files change:
The plugin watches routes/**/*.php by default and runs ziggy:generate when changes are detected.

Laravel Mix Plugin

For projects using Laravel Mix, you can create a custom plugin:
1

Install Chokidar

The plugin requires Chokidar for file watching:
2

Add the plugin code

Copy the plugin code above to your webpack.mix.js file.
3

Enable the plugin

Call .ziggy() at the end of your Mix chain:
4

Start development

Routes will regenerate automatically when route files change.

Customizing the Plugin

You can customize which files to watch and where to output the config:

Manual Watch Script

If you’re not using a bundler or prefer a simpler approach, create a Node.js watch script:
1

Create the watch script

2

Install dependencies

3

Add npm script

4

Run the watcher

Or include it in your dev script to run alongside Vite/Mix.

Git Hooks

Automatically regenerate routes before committing or after pulling changes.

Pre-commit Hook

Ensure the config file is always up-to-date:
Make the hook executable:

Post-merge Hook

Regenerate after pulling changes:
Make it executable:

CI/CD Integration

Integrate route generation into your deployment pipeline.

GitHub Actions

Laravel Forge

Add to your deployment script:

Laravel Envoyer

Add a deployment hook after “Install Composer Dependencies”:

Development Workflow

For the best development experience, combine multiple approaches:
Now running npm run dev will:
  1. Watch route files for changes
  2. Automatically regenerate Ziggy config
  3. Run Vite dev server with hot reload

Troubleshooting

Check that your watcher is actually running:
Verify file paths are correct:
Ensure the output directory is writable:
And that Laravel can write to it:
The watcher might not be detecting changes in subdirectories. Make sure your watch pattern includes them:
If watching causes slowdowns:
  1. Exclude unnecessary directories:
  2. Add debouncing:
  3. Disable in production:

Best Practices

  1. Only watch in development: Disable automatic regeneration in production
  2. Version control: Commit the generated file so it’s available immediately after clone
  3. CI/CD integration: Always regenerate as part of your build process
  4. Git hooks: Use pre-commit hooks to ensure the file is always current
  5. Cache carefully: If caching routes in Laravel, remember to regenerate Ziggy config too

Alternative: API Endpoint

If automatic regeneration becomes cumbersome, consider using an API endpoint instead. This approach:
  • Eliminates the need for regeneration
  • Always serves current routes
  • Simplifies the build process
  • Works well for SPAs and separate repos
The tradeoff is an additional HTTP request when your app loads.