How to use window.localStorage with prerender #954
Answered
by
rschristian
PrettyCoffee
asked this question in
Q&A
-
I am trying to build a theme toggle that saves the currently selected theme into the local storage.
This is not what I would expect, but I don't know what I am doing wrong or if this is something to be expected? Reproduction:
The code can also be added within the Home component to trigger the error. |
Beta Was this translation helpful? Give feedback.
Answered by
rschristian
Apr 16, 2023
Replies: 1 comment 2 replies
-
if (typeof window !== 'undefined') {
window.localStorage.setItem("key", value);
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
PrettyCoffee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
window
is not accessible in Node, where prerendering happens. You'll need to use a window check/guard: