❓ Why Doesn’t Server-Side Rendering (SSR) Have Access to localStorage?

The key reason is simple:
localStorage
is a browser API — and SSR happens on the server, not in the browser.
Let’s understand this more deeply 👇
🧠 What is localStorage
?
localStorage
is part of the Web Storage API.It exists only in the browser (i.e., on the client-side).
It’s used to store data like preferences, tokens, or cart items between page reloads.
localStorage.setItem('theme', 'dark');
This code only runs in the browser, because the browser provides window.localStorage
.
🖥️ What Happens During Server-Side Rendering?
When using Server Components or SSR, the page is rendered on the server (like a Node.js process).
There is:
❌ No
window
❌ No
document
❌ No
localStorage
❌ No DOM
The server has no idea about your user's browser or storage, because the code runs before the browser even receives the page.
💥 So What If You Try to Use localStorage
on the Server?
You’ll get an error like:
ReferenceError: localStorage is not defined
Because it doesn’t exist in that environment — just like you can’t use document.querySelector()
on the server either.
🧩 So How Can You Use localStorage
in a Next.js App?
You have two options:
✅ Option 1: Use a Client Component
Only access localStorage
inside a component that runs in the browser.
'use client';
import { useEffect } from 'react';
export default function ThemeLoader() {
useEffect(() => {
const theme = localStorage.getItem('theme');
console.log(theme);
}, []);
return null;
}
✅ Runs safely in the browser
✅ Can access localStorage
freely
✅ Option 2: Hydrate Server Data With Client Code
If you fetch some data on the server and want to enhance it using browser info like localStorage
, you:
Render the base page on the server
Then use a client-side component to pull in additional context (like auth tokens, preferences, etc.)
🧠 Final Analogy
Think of localStorage
like your browser’s notebook.
When the server is drawing your page, it doesn’t have access to your notebook — it’s on a different machine.
Only once the page reaches your browser, your notebook becomes visible and usable.
✅ Summary
Question | Answer |
Is localStorage available in SSR? | ❌ No, because it's a browser API |
Why not? | SSR runs in Node.js, not the browser |
How to use it correctly? | Use it inside Client Components only |
Subscribe to my newsletter
Read articles from Muhammad Sufiyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Muhammad Sufiyan
Muhammad Sufiyan
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021. With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences. In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS. I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.