Skip to content

Commit 8572e56

Browse files
committed
feat(nuxt): run plugin before router
1 parent f745d0a commit 8572e56

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

packages/nuxt/src/runtime/plugin.ts

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,49 @@ import * as _plugins from '#build/$rstore-plugins'
99
import { createStore, RstorePlugin } from '@rstore/vue'
1010
import { markRaw } from 'vue'
1111

12-
export default defineNuxtPlugin(async (nuxtApp) => {
13-
let plugins = Object.values({ ..._plugins }) as Plugin[]
14-
const models = _models as ModelList
15-
16-
// Devtools
17-
if (import.meta.dev) {
18-
const { devtoolsPlugin } = await import('./devtools')
19-
plugins = [
20-
...plugins,
21-
devtoolsPlugin,
22-
]
23-
}
24-
25-
const store = await createStore({
26-
plugins,
27-
models,
28-
isServer: !!import.meta.server,
29-
})
30-
31-
const cacheKey = '$srstore'
32-
33-
nuxtApp.hook('app:rendered', () => {
34-
nuxtApp.payload.state[cacheKey] = markRaw(store.$cache.getState())
35-
})
36-
37-
if (import.meta.client && nuxtApp.payload.state[cacheKey]) {
38-
store.$cache.setState(nuxtApp.payload.state[cacheKey])
39-
}
40-
41-
nuxtApp.vueApp.use(RstorePlugin, {
42-
store,
43-
})
44-
45-
// Inject $rstore
46-
return {
47-
provide: {
48-
rstore: store,
49-
},
50-
}
12+
export default defineNuxtPlugin({
13+
name: 'rstore',
14+
15+
order: -21,
16+
17+
setup: async (nuxtApp) => {
18+
let plugins = Object.values({ ..._plugins }) as Plugin[]
19+
const models = _models as ModelList
20+
21+
// Devtools
22+
if (import.meta.dev) {
23+
const { devtoolsPlugin } = await import('./devtools')
24+
plugins = [
25+
...plugins,
26+
devtoolsPlugin,
27+
]
28+
}
29+
30+
const store = await createStore({
31+
plugins,
32+
models,
33+
isServer: !!import.meta.server,
34+
})
35+
36+
const cacheKey = '$srstore'
37+
38+
nuxtApp.hook('app:rendered', () => {
39+
nuxtApp.payload.state[cacheKey] = markRaw(store.$cache.getState())
40+
})
41+
42+
if (import.meta.client && nuxtApp.payload.state[cacheKey]) {
43+
store.$cache.setState(nuxtApp.payload.state[cacheKey])
44+
}
45+
46+
nuxtApp.vueApp.use(RstorePlugin, {
47+
store,
48+
})
49+
50+
// Inject $rstore
51+
return {
52+
provide: {
53+
rstore: store,
54+
},
55+
}
56+
},
5157
})

0 commit comments

Comments
 (0)