localStorage undefined on client-side
Answered
Peterbald posted this in #help-forum
PeterbaldOP
I am required to use localStorage, since it only works in browser. I used "use client" on top. It works fine when running in dev but when building for production it gives me this error:
Generating static pages (0/13) [= ]ReferenceError: localStorage is not defined
at <Component>Answered by Ray
try to use next/dynamic importing that client component with ssr disable
https://nextjs-faq.com/browser-api-client-component#using-nextdynamic-with-ssr-false
https://nextjs-faq.com/browser-api-client-component#using-nextdynamic-with-ssr-false
3 Replies
@Peterbald I am required to use localStorage, since it only works in browser. I used "use client" on top. It works fine when running in dev but when building for production it gives me this error:
Generating static pages (0/13) [= ]ReferenceError: localStorage is not defined
at <Component>
try to use next/dynamic importing that client component with ssr disable
https://nextjs-faq.com/browser-api-client-component#using-nextdynamic-with-ssr-false
https://nextjs-faq.com/browser-api-client-component#using-nextdynamic-with-ssr-false
Answer
cool, please mark solution