question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross-request state pollution ?

See original GitHub issue

I run into the classic cross-request state pollution scenario that is most often encountered when not using the stateFactory option. This is custom vue ssr project, modeled after the vue hackernews 2.0 repo.

I create my modules like this:

@Module({ stateFactory: true, namespaced: true, name: 'user' })
export class UserStoreModule extends VuexModule {
    public user: IUserModuleState['user'] = {}
}

This is my createStore function:

const createStore = () => {
    
    return new Vuex.Store({
        modules: {
            user: UserStoreModule
        }
    })
}
export default createStore

Module access is like this:

const userModule = getModule(UserStoreModule, this.$store)

I have setup a test environment to check if userModule.user.id is set just after initializing the store and module, and userModule.user.id is indeed set with the value from the previous request. When I modify the file dist/cjs/index.js and change: modOpt.store = modOpt.store || store; to modOpt.store = store || modOpt.store;

and

function getModule(moduleClass, store) {
    if (moduleClass._statics) {
        return moduleClass._statics;
    }

to

function getModule(moduleClass, store) {
    if (moduleClass._statics && !store) {
        return moduleClass._statics;
    } 

This issue goes away, it looks the previous state is stored and reused somewhere and forcing to use the store provided to getModule() helps prevent this issue.

Strangely enough, I can’t reproduce this with a clean nuxt project and the nuxt way of setting it up as seen in the readme. Does nuxt do some magic by creating a new module, other than my user: UserStoreModule initializing ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
Sharsiecommented, Jul 29, 2019

PR made, @AliLozano in the meantime, you can use npm i Sharsie/vuex-module-decorators#hotfix.ssr.1

7reactions
Sharsiecommented, Jul 28, 2019

Here is my working solution: https://github.com/championswimmer/vuex-module-decorators/compare/master...Sharsie:hotfix/ssrSupport Sharsie/vuex-module-decorators#hotfix.ssr.1

@championswimmer Is this something you would be willing to merge? Shall I PR the changes? I’ve done my best to preserve backward compatibility using _statics when store is not provided.

I can think of only one way this would break and that’s when someone would mix usage of getModule(module) and getModule(module, store), then two instances of the module would be created… but then again, this might be a desired behaviour, e.g. using two different stores: getModule(module, store1) and getModule(module, store2)

The behaviour of this would change with the PR, as now it always uses 1 module state… after the PR each module would use state from a different store.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-Side Rendering (SSR) - Vue.js
We call this cross-request state pollution. We can technically re-initialize all the JavaScript modules on each request, just like we do in browsers....
Read more >
How to avoid cross-request state pollution problems when ...
In the production environment I can use node:vm to implement a sandbox to avoid cross-request state pollution but the development ...
Read more >
Cross-State Air Pollution Rule (CSAPR) | US EPA
The Cross-State Air Pollution Rule (CSAPR), requires states to significantly improve air quality by reducing power plant emissions that ...
Read more >
Administration of Cross-State Air Pollution Rule Trading ...
The Environmental Protection Agency (EPA) is providing notice of the availability of data on the administration of the assurance provisions ...
Read more >
Cross-State Air Pollution Rule (CSAPR) - State of Michigan
On July 6, 2011, the US Environmental Protection Agency (EPA) finalized the Cross-State Air Pollution Rule (CSAPR) to address air pollution from upwind ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found