kirill-konshin / next-redux-wrapper

Redux wrapper for Next.js
MIT License
2.66k stars 265 forks source link

adding `ctx` to initialStore state causes circular ref in `getInitialProps` #147

Closed andycmaj closed 5 years ago

andycmaj commented 5 years ago

this line:

                const store = initStore({
                    ctx: appCtx.ctx, // <= causes circular reference 
                });

...

        return {
          isServer,
          initialState: store.getState(), // because initialState includes ctx
          initialProps,
        };

this is the warning i see (nexjs 9)

(node:98926) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at /Workspace/github/andycmaj/musigraph-next/node_modules/webpack-hot-middleware/middleware.js:111:38
    at /Workspace/github/andycmaj/musigraph-next/node_modules/webpack-hot-middleware/middleware.js:66:7
    at Array.forEach (<anonymous>)
    at everyClient (/Workspace/github/andycmaj/musigraph-next/node_modules/webpack-hot-middleware/middleware.js:65:26)
    at Object.publish (/Workspace/github/andycmaj/musigraph-next/node_modules/webpack-hot-middleware/middleware.js:110:7)
    at Function.middleware.publish (/Workspace/github/andycmaj/musigraph-next/node_modules/webpack-hot-middleware/middleware.js:48:17)
    at HotReloader.send.args [as send] (/Workspace/github/andycmaj/musigraph-next/node_modules/next/dist/server/hot-reloader.js:6:335)
    at /Workspace/github/andycmaj/musigraph-next/node_modules/next/dist/server/hot-reloader.js:16:376
    at typePromise.then.typeMessages (/Workspace/github/andycmaj/musigraph-next/node_modules/next/dist/build/output/index.js:2:133)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:98926) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 11)

when i comment out

        const store = initStore({
          // ctx: appCtx.ctx,
        });

the warning goes away

kirill-konshin commented 5 years ago

you can't comment this out because this is needed for wrapper to work. check your state/req/res objects that it does not have circular structures.