redux-state-sync icon indicating copy to clipboard operation
redux-state-sync copied to clipboard

Support react SSR like next-js

Open mustafa-alfar opened this issue 5 years ago • 7 comments

is there any intent to support Next-js ?

mustafa-alfar avatar Mar 27 '19 20:03 mustafa-alfar

It'd be very good to support that. I'm not familiar with next, if I got time recently, I will look into it.

aohua avatar Mar 28 '19 11:03 aohua

Actually, it's work fine with Next-js just by adding a condition, so in the With-Redux example on Next, at store.js I've written this,

export function initializeStore(initialState = exampleInitialState) { const isServer = typeof window === "undefined"; if (isServer) { const store = createStore( reducers, initialState, composeWithDevTools(applyMiddleware()) ); return store; } else { const store = createStore( reducers, initialState, composeWithDevTools(applyMiddleware(...middlewares)) ); initStateWithPrevTab(store); return store; } }

mustafa-alfar avatar Mar 31 '19 11:03 mustafa-alfar

Thanks, I will take a look on this.

aohua avatar Apr 04 '19 02:04 aohua

Actually, it's work fine with Next-js just by adding a condition, so in the With-Redux example on Next, at store.js I've written this,

export function initializeStore(initialState = exampleInitialState) { const isServer = typeof window === "undefined"; if (isServer) { const store = createStore( reducers, initialState, composeWithDevTools(applyMiddleware()) ); return store; } else { const store = createStore( reducers, initialState, composeWithDevTools(applyMiddleware(...middlewares)) ); initStateWithPrevTab(store); return store; } }

Thanks @mustafa-alfar It worked for my Next js app and resolved this issue of getting a large number of warnings on CLI.

asadfreelancer avatar Jun 09 '20 07:06 asadfreelancer

Hi everyone, I had a similar issue on redux-state-sync in my NextJs project, my store initializer function was similar to @mustafa-alfar 's solution (separating server store from client store), everything was fine till I realized that my project is making too many files after run and visit some pages, and finally it crashed and logged this:

[Error: EMFILE: too many open files, open '/.../.next/static/chunks/main.js'] {
  errno: -24,
  code: 'EMFILE',
  syscall: 'open',
  path: '.../.next/static/chunks/main.js',
  expose: false,
  statusCode: 500,
  status: 500
}

You can check the number of created files with next project after/before using this package with this command on terminal (7313 is my node server pid, replace it with yours):

ps aux | grep -i "node server" while sleep 1; do sudo lsof -p 7313 | wc -l; done

and this is why I had to stop using this amazing package (redux-state-sync) in my next js (SSR) project. can you please take a look at this issue?

OS: MacOS v11.0.1 node: v14.15.0 next js: v10.0.0 redux-state-sync: v3.1.2 redux: v4.0.5 react-redux: v7.2.2

aynzad avatar Dec 06 '20 14:12 aynzad

Hi @aynzad Thanks for reporting the issue. I try to investigate the issue when I got time.

aohua avatar Dec 07 '20 14:12 aohua

any updates on this?

Isaac1606 avatar Sep 21 '23 19:09 Isaac1606