bjoluc / next-redux-cookie-wrapper

Sync a subset of your Redux state with cookies in Next.js :cookie: :sparkles:
MIT License
114 stars 4 forks source link

Changing compress config to false throws 'SyntaxError: Unexpected token N in JSON at position 0' error #23

Closed rezasohrabi closed 2 years ago

rezasohrabi commented 2 years ago

Hello, First of all thanks a lot for this amazing wrapper. I need to persist cookie without compression, but when i change compress config to false bellow error happens:

SyntaxError: Unexpected token N in JSON at position 0 at JSON.parse () at Function._decodeState (/home/rezasohrabi/Desktop/node_modules/next-redux-cookie-wrapper/dist/next-redux-cookie-wrapper.cjs.development.js:170:17) at StateCookies.getAll (/home/rezasohrabi/Desktop/node_modules/next-redux-cookie-wrapper/dist/next-redux-cookie-wrapper.cjs.development.js:204:46) at Object.dispatch (/home/rezasohrabi/Desktop/node_modules/next-redux-cookie-wrapper/dist/next-redux-cookie-wrapper.cjs.development.js:332:40)

I am persisting user object, here is my wrapped store codes:

const makeStore = wrapMakeStore(() =>
  configureStore({
    reducer: rootReducer,
    middleware: (getDefaultMiddleware) =>
      getDefaultMiddleware()
        .prepend(
          nextReduxCookieMiddleware({
            subtrees: [
              {
                cookieName: 'userV3',
                subtree: 'auth.user',
                compress: false,
              },
            ],
          })
        )
        .concat(logger as ReturnType<typeof getDefaultMiddleware>),
  })
)
bjoluc commented 2 years ago

Hi @rezasohrabi, thanks for the kind words! I'm assuming that you didn't delete the old (compressed) cookies after setting compress to false and now JSON.parse is trying to parse the compressed JSON string? Please re-open if that's not the case. Thanks!