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 runphp artisan ziggy:generate when they’re modified.
Vite Plugin
For projects using Vite, the vite-plugin-ziggy package provides automatic regeneration.
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: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:Git Hooks
Automatically regenerate routes before committing or after pulling changes.Pre-commit Hook
Ensure the config file is always up-to-date:Post-merge Hook
Regenerate after pulling changes: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:npm run dev will:
- Watch route files for changes
- Automatically regenerate Ziggy config
- Run Vite dev server with hot reload
Troubleshooting
Routes not regenerating
Routes not regenerating
Check that your watcher is actually running:Verify file paths are correct:
Permission denied errors
Permission denied errors
Ensure the output directory is writable:And that Laravel can write to it:
Changes not detected
Changes not detected
The watcher might not be detecting changes in subdirectories. Make sure your watch pattern includes them:
Performance issues
Performance issues
If watching causes slowdowns:
-
Exclude unnecessary directories:
-
Add debouncing:
-
Disable in production:
Best Practices
- Only watch in development: Disable automatic regeneration in production
- Version control: Commit the generated file so it’s available immediately after clone
- CI/CD integration: Always regenerate as part of your build process
- Git hooks: Use pre-commit hooks to ensure the file is always current
- 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