fix(vue-app): do not preserve state when registering client-side dynamic module (#8486)

This commit is contained in:
Daniel Roe 2020-12-14 09:52:02 +00:00 committed by GitHub
parent dd7f767d13
commit 1269773d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -76,10 +76,14 @@ const defaultTransition = <%=
<% if (store) { %>
const originalRegisterModule = Vuex.Store.prototype.registerModule
const baseStoreOptions = { preserveState: process.client }
function registerModule (path, rawModule, options = {}) {
return originalRegisterModule.call(this, path, rawModule, { ...baseStoreOptions, ...options })
const preserveState = process.client && (
Array.isArray(path)
? !!path.reduce((namespacedState, path) => namespacedState && namespacedState[path], this.state)
: path in this.state
)
return originalRegisterModule.call(this, path, rawModule, { preserveState, ...options })
}
<% } %>

View File

@ -55,6 +55,7 @@ describe('basic browser', () => {
test('/store-module', async () => {
await page.nuxt.navigate('/store-module')
expect(await page.$text('h1')).toBe('mutated')
expect(await page.evaluate(() => window.__NUXT__.state.clientsideModule.initialised)).toBeTruthy()
})
test('/css', async () => {

View File

@ -15,4 +15,13 @@ export default function ({ store }) {
}
}
})
if (process.server) { return }
store.registerModule('clientsideModule', {
namespaced: true,
state: () => ({
initialised: true
})
})
}