localStorage on a heroku app?

Hello campers, I recently finished a project using react and deployed it to Heroku at https://tech-trivia.herokuapp.com/ (bear with the mobile version as I have to work on that next), anyway locally on my machine, localStorage works correctly and the player’s lives and score persist even if the page is refreshed (this is the desired effect). Unfortunately, when it is deployed on Heroku, I get NaN for both lives and score and thus I cannot have them persist. Any ideas on why this happens or how to work around it?

tldr; does localStorage work on Heroku?

Local storage is in your browser, it should nothing to do with heroku. The only think I can imagine is if you have some SSR or something that is refreshing, but still, I would expect LS to persist.

Do you have a repo?

I remember that I had a lot of confusion setting up env variables when I tried heroku - could there be an env variable that is coming back undefined and is interfering with one of these calculations?


I can see the localStorage in my browser.
lives: 2
score: 10

But when I refresh it change to:
lives: 5
score: 0

So I think you need to

  1. check if there is a value in localStorage
  2. if there is a value, get the value
  3. else set the default value
1 Like

The localStorage declaration can be found in src/App.js line 21-32

Sorry for the question it seems to be working now for some reason before it was giving me NaN, this may be because it was only tracking old versions that weren’t debugged yet (when I was trying to save it and parse it as a JSON object).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.