Guide
Nuxt and SSR
Use vue-route-query in Nuxt, with server rendering that already matches the URL.
Nuxt runs on vue-router, so there is nothing to install beyond the package. The first render on the server reads the request URL, so the HTML a crawler or a slow connection receives already shows the right page, tab, and filters.
This page is a Nuxt app. Open it with a query string, for example ?name=Ada, and view the page source: the demo below is rendered with the value already in place.
/guide/nuxt
Hello, anonymous visitor!
Type, then reload the page or share the link.
Auto-imports
Add the composables and codecs to the auto-imports so components can use them without imports:
nuxt.config.ts
export default defineNuxtConfig({
imports: {
presets: [
{
from: '@chronicstone/vue-route-query',
imports: [
'useQueryState',
'useQueryStates',
'dynamicQueryState',
'stringCodec',
'numberCodec',
'booleanCodec',
'enumCodec',
'dateCodec',
'arrayCodec',
'schemaCodec',
],
},
],
},
})
Data fetching
The refs are reactive and already validated, so they can drive useFetch directly. A new page or filter refetches, and the server render fetches with the values from the URL:
const filters = useQueryStates({
schema: {
q: stringCodec({ default: '' }),
page: numberCodec({ default: 1, int: true, min: 1 }),
},
})
const { data } = await useFetch('/api/packages', { query: filters })
Rules on the server
- Read the refs freely during the server render. Make writes from event handlers or client-only hooks such as
onMounted, not while the server renders. - One client is created per router, and Nuxt creates one router per request, so requests never share state.