🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsSetting up live captions stuck in Windows 11(14.09.2026 um 16:31 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsWindows 11's latest update broke my speakers, and I'm not alone(14.09.2026 um 18:54 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsSetting up live captions stuck in Windows 11(14.09.2026 um 16:31 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsWindows 11's latest update broke my speakers, and I'm not alone(14.09.2026 um 18:54 Uhr)

🔧 Programmierung 🕛 vor 3 Jahren 2 Min Lesezeit
0

NextJS - Access window and localStorage

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Window and document are unavailable on the server. So you'll run into errors such as ReferenceError: window is not defined if you are trying to access document or window properties such as local storage.



To avoid these undefined errors at compile and build time, you can run a simple check, and only if a user is a browser user run your code.

Since process.browser is deprecated you must use typeof window.




CODE
if (typeof window !== "undefined") {
// Write your client-side statements here.
window.localStorage.getItem("key");
window.localStorage.setItem("key", "value");
}












Optimal solution:



This is good and it'll work, but you need to explicitly check if it is a client-side rendered component or server-side every time.



To avoid this issue you need to create an mock window object.

then use mock window for server-side logic and actual window for client-side.




CODE
let WINDOW = {};

if (typeof window !== "undefined") {
// When code is on client-side. So we need to use actual methods and data.
WINDOW = window;
} else {
// When code is on server-side.

// Other component are mostly server-side and need to match their logic and check their variable with other server-side components and logics.
// So following code will be use for them to pass the logic checking.
WINDOW = {
document: {
location: {},
},
localStorage: {
getItem :() => {},
setItem :() => {}
},
};
}

export default WINDOW;






Note that: Depending on what property of window, document,... or what methods of those property you have used, your implementation details will be different.



Now use WINDOW instead of window in your code:




CODE
  WINDOW.localStorage.getItem("key");
WINDOW.localStorage.setItem("key", "value");












Example of use:



I created a custom hook for using local storage:




CODE
export const useLocalStorage = (key) => {
// returns value related to initial key.
const item = WINDOW.localStorage.getItem(key);
// return an function that will save given value to initial key
const setItem = (value) => WINDOW.localStorage.setItem(key, value);
return [item, setItem];
};












Warning:



remember, never create a function for window type checking.

it always must be an explicit type checking or it will not work



Wrong way:




CODE
const hasWindow = () => {
return typeof window !== "undefined"
}

if (hasWindow()) {
// client-side operation such as local storage.
localStorage.setItem(key, value)
}

if (!hasWindow()) {
// server-side code
}






read more and reason for this problem: and it's a copy-paste of similar post written by him.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Setting up live captions stuck in Windows 11
1 Quelle
Burn Out, Or Fade Away
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten NextJS - Access window and localStorage

Thematisch verwandte Begriffe: NextJS, Access, window, localStorage · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...