@@ -7,6 +7,7 @@ Vite plugin to develop Vue SSR apps
77* State management
88* Teleports
99* [ Unhead] ( https://unhead.unjs.io ) support
10+ * Based on [ H3] ( https://h3.unjs.io )
1011
1112## Quick Setup
1213
@@ -80,6 +81,8 @@ const routes = [
8081export default vueSSR (App , { routes })
8182```
8283
84+ ### State management
85+
8386Pinia/Vuex is supported by using the ` app ` and ` state ` property inside the callback.
8487
8588``` typescript
@@ -96,11 +99,21 @@ export default vueSSR(App, { routes }, ({ app, state }) => {
9699})
97100```
98101
99- > The state will be persisted on ` window.__INITIAL_STATE__ ` property and serialized using ` @nuxt/devalue `
102+ > The state will be persisted on ` window.__INITIAL_STATE__ ` property and serialized using ` devalue `
103+
104+ ### Router
100105
101106It's possible to make changes to the router, use the ` router ` property in the callback.
102107
103108``` typescript
109+ const routes = [
110+ {
111+ path: ' /' ,
112+ name: ' counter' ,
113+ component: Counter ,
114+ },
115+ ]
116+
104117export default vueSSR (App , { routes }, ({ router }) => {
105118 router .beforeEach (async (to , from ) => {
106119 if (
@@ -113,28 +126,67 @@ export default vueSSR(App, { routes }, ({ router }) => {
113126})
114127```
115128
116- The Express request and response objects are accessible from the callback. Make sure to wrap them in ` import.meta.env.SSR ` .
129+ To customize the router, just return the router instance.
130+
131+ The ` routes ` parameter is omitted, because we create a fresh router instance in the method.
132+
133+ ``` typescript
134+ export default vueSSR (App , {}, async ({ app }) => {
135+ const router = createRouter ({
136+ history: import .meta .env .SSR ? createMemoryHistory (' /' ) : createWebHistory (' /' ),
137+ routes: [
138+ {
139+ path: ' /' ,
140+ name: ' counter' ,
141+ component: Counter ,
142+ },
143+ ],
144+ })
145+
146+ return {
147+ router ,
148+ }
149+ })
150+ ```
151+
152+ ### H3
153+
154+ H3 is the underlaying server. During development it injects as an middleware.
155+
156+ The ` event ` param is used to access the H3 composables.
157+
158+ > NOTE: only works in SSR
117159
118160``` typescript
119- export default vueSSR (App , { routes }, ({ request , response }) => {
161+ import { getRequestURL } from ' h3'
162+
163+ export default vueSSR (App , { routes }, ({ event }) => {
120164 if (import .meta .env .SSR ) {
121- console .log (request ?. originalUrl )
165+ console .log (getRequestURL ( event )) // "https://example.com/path"
122166 }
167+
168+ console .log (event ) // undefined
123169})
124170```
125171
126- Or use ` useSSRContext ` .
172+ In a Vue component, use the ` useH3Event() ` composable
127173
128174``` typescript
129- const { request, response } = useSSRContext ()
175+ import { useH3Event } from ' vite-plugin-vue-ssr'
176+ import { getRequestURL } from ' h3'
130177
131178if (import .meta .env .SSR ) {
132- console .log (request ?.originalUrl )
179+ const event = useH3Event ()
180+
181+ console .log (getRequestURL (event )) // "https://example.com/path"
133182}
134183```
135184
136- Using Teleport is supported, but requires a little bit of setup. Targeting ` body ` is not supported, use ` #teleports ` instead.
185+ See [ https://h3.unjs.io/utils ] ( https://h3.unjs.io/utils ) for more composables.
186+
187+ ### Teleports
137188
189+ Using ` Teleport ` is supported, but requires a little bit of setup. Targeting ` body ` is not supported (in SSR), use ` #teleports ` instead.
138190
139191``` html
140192<template >
0 commit comments