Skip to content

Commit e4883b8

Browse files
author
Jack Kingston-Lee
committed
Reset vuex module/model state if a service plugin is reinitialized. Fixes broken service plugins in SSR setups.
1 parent d498dc1 commit e4883b8

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/service-module/make-service-plugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export default function prepareMakeServicePlugin(
8989
// (1^) Create and register the Vuex module
9090
options.namespace = makeNamespace(namespace, servicePath, nameStyle)
9191
const module = makeServiceModule(service, options)
92-
store.registerModule(options.namespace, module)
92+
// Don't preserve state if reinitialized (prevents state pollution in SSR)
93+
store.registerModule(options.namespace, module, { preserveState: false })
9394

9495
// (2a^) Monkey patch the BaseModel in globalModels
9596
const BaseModel = _get(globalModels, `[${options.serverAlias}].BaseModel`)
@@ -100,13 +101,16 @@ export default function prepareMakeServicePlugin(
100101
}
101102
// (2b^) Monkey patch the Model(s) and add to globalModels
102103
assignIfNotPresent(Model, {
103-
store,
104104
namespace: options.namespace,
105105
servicePath,
106106
instanceDefaults,
107107
setupInstance,
108108
preferUpdate
109109
})
110+
// As per 1^, don't preserve state on the model either (prevents state pollution in SSR)
111+
Object.assign(Model, {
112+
store
113+
})
110114
if (!Model.modelName || Model.modelName === 'BaseModel') {
111115
throw new Error(
112116
'The modelName property is required for Feathers-Vuex Models'

0 commit comments

Comments
 (0)