How to use window.localStorage with prerender #954
-
| 
         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.
Replies: 1 comment 2 replies
-
| 
         
 if (typeof window !== 'undefined') {
    window.localStorage.setItem("key", value);
} | 
  
Beta Was this translation helpful? Give feedback.
windowis not accessible in Node, where prerendering happens. You'll need to use a window check/guard: