Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bunkdev's avatar

Inertia + SSR + Ziggy

I investigated the issue on a new Laravel installation. Here's the thing: I can use route('dashboard') within <template> tag, but when used inside a <script setup>, I get an error: ReferenceError: route is not defined

Here is my listing for ssr.js:

this is default. Also I've published php artisan ziggy:generate. Nothing helps

0 likes
3 replies
LaryAI's avatar
Level 58

The issue you're experiencing is a common one when using Ziggy with Inertia.js and Vue 3, especially with SSR (Server-Side Rendering). The root cause is that the route helper provided by Ziggy is only available in the template context (via global properties), but not automatically in the <script setup> context.

Why does route work in <template>, but not in <script setup>?

  • In the <template>, Ziggy's route helper is injected as a global property (e.g., app.config.globalProperties.$route), so you can use it directly.
  • In <script setup>, you must explicitly import or inject it, because global properties are not automatically available.

Solution

1. Import route from Ziggy

In your <script setup>, import route directly from Ziggy:

import { route } from 'ziggy-js';

Now you can use route('dashboard') in your <script setup>.


2. If you need Ziggy's config (for SSR)

If you need to use the Ziggy config (for example, for named routes or URL generation that depends on the current location), you can also import Ziggy:

import { route, Ziggy } from 'ziggy-js';

If you need to pass the Ziggy config from the server (SSR), you can access it via your page props:

const ziggyConfig = defineProps(['ziggy']);

And then:

route('dashboard', undefined, undefined, ziggyConfig);

But in most cases, just importing route is enough.


3. Example Usage

<script setup>
import { route } from 'ziggy-js';

const dashboardUrl = route('dashboard');
</script>

4. Why doesn't use(ZiggyVue) make it available in <script setup>?

The use(ZiggyVue) plugin only injects the helper into the global Vue instance, which is accessible in templates or via getCurrentInstance().appContext.config.globalProperties in classic scripts. The <script setup> syntax is more isolated and does not automatically inherit global properties.


Summary

  • In <template>: You can use route('dashboard') directly.
  • In <script setup>: You must import { route } from 'ziggy-js' and use it.

References:

Let me know if you need a more detailed example!

bunkdev's avatar

found 2 variants to make it work:

  1. custom helper function in composable
  2. properties in script setup contains route names in object, or final url from backend

In this way it works

Please or to participate in this conversation.