elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

isomorphic support: localStorage is not defined #51

Closed azz0r closed 8 years ago

azz0r commented 8 years ago
Failed to retrieve initialize state from localStorage: ReferenceError: localStorage is not defined
    at /Users/azz0r/Sites/mobile-presales/node_modules/redux-localstorage/lib/persistState.js:59:38
    at /Users/azz0r/Sites/mobile-presales/node_modules/redux/lib/applyMiddleware.js:38:19
    at createStore (/Users/azz0r/Sites/mobile-presales/node_modules/redux/lib/createStore.js:59:33)

Any work arounds? This doesn't block redux-localstorage working but it leaves an initial error message in console.

amrit92 commented 8 years ago

I am facing this issue too in 1.0.0rc5 .

elgerlambert commented 8 years ago

Hi guys,

@amrit92 Could you provide a little more info? I'm currently using 1.0.0-rc5 in a node environment in combination with LevelUP without issue, but it's a different use case than isomorphic server rendering.

I did notice a mistake in rc5 which will cause an issue if you don't provide a storage argument to persistState, is that the case in your situation @amrit92?

@azz0r, does the server and client use the exact same store configuration? Correct me if I'm wrong, but in an isomorphic setting redux-localstorage doesn't actually do anything server-side, so you should in theory be able to leave persistState out server-side and only add it client side. But perhaps this is easier said than done and requires you to jump through a bunch of awkward loops.

I lack isomorphic experience so if you could share more about your setup or something you think might be relevant that would help!

vicentedealencar commented 8 years ago

I think a check for typeof localstorage === 'undefined' before using localstorage would fix that

azz0r commented 8 years ago

I fixed this by condutiobally detecting whether the server or client was calling a store and not including this on the server

moimael commented 7 years ago

It is also a problem when running tests, because localStorage is not defined either.

Sigfried commented 7 years ago

I'm also having trouble running tests. It would be nice to have an option to pass in an object or something to use as localStorage.

Sigfried commented 7 years ago

Sorry...I gotta get the other branch