Skip to main content
When building a single-page application (SPA) or managing your frontend project separately from your Laravel backend, you can still use Ziggy’s route() function through the NPM package.

Overview

For JavaScript projects managed separately from Laravel (without Composer or a vendor directory), Ziggy provides an NPM package that gives you access to the same powerful routing functionality.

Installation

1

Install the NPM package

Install Ziggy’s JavaScript package in your frontend project:
2

Choose your config delivery method

You have two options for making your Laravel routes available to your frontend:
  • Option A: Generate a static config file and add it to your frontend project
  • Option B: Create an API endpoint that serves the config dynamically
See the sections below for details on each approach.
3

Import and use the route function

Import the route() function in your JavaScript/TypeScript files:

Option A: Using a Generated Config File

Generating the Config

On your Laravel backend, run the Ziggy generate command:
This creates a resources/js/ziggy.js file with your routes configuration:

Copying to Your Frontend Project

Copy this file to your frontend project and import it where needed:

Option B: API Endpoint Configuration

For dynamic configuration that doesn’t require copying files between projects, create an API endpoint that serves Ziggy’s config.

Creating the Endpoint

Add a route to your Laravel API:

Fetching Config from the Frontend

Fetch the config when your app initializes:

Framework-Specific Usage

React Example

Vue Example

Then in your components:

Making Config Available Globally

To avoid passing the Ziggy config to every route() call, you can make it globally available:
Now you can use the route() function without passing the config:

Troubleshooting

Routes Not Found

If the route() function can’t find your routes:
  1. Verify the config is loaded: Check that your Ziggy config object contains the expected routes
  2. Check route names: Ensure you’re using the exact route name as defined in Laravel
  3. Regenerate config: If using a static file, regenerate it after adding new routes

CORS Issues with API Endpoint

If fetching config from an API endpoint fails with CORS errors:
  1. Configure CORS in Laravel:
  1. Verify endpoint is accessible: Test the endpoint directly in your browser

Incorrect Base URLs

If generated URLs use the wrong domain:
  1. Check APP_URL: Ensure your .env file has the correct APP_URL
  2. Verify Ziggy config URL: Check the url property in your Ziggy config matches your API domain

TypeScript Configuration

For TypeScript projects, configure path aliases in tsconfig.json:
And optionally generate TypeScript definitions:

Best Practices

  • Set up automated regeneration when routes change (see Auto-Regeneration Guide)
  • Or use the API endpoint approach for always up-to-date config
  • Consider caching the API endpoint response in production
  • Use Ziggy’s filtering to exclude admin or internal routes
  • Only expose public API routes to your frontend
  • Use different base URLs for development and production
  • Consider environment variables for API endpoints