Skip to content

Commit c88edbf

Browse files
author
amandaesmith3
committed
check if window is defined before accessing localStorage
1 parent 1339c2c commit c88edbf

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/components/global/Playground/index.tsx

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export default function Playground({
187187
* Otherwise, if there is a saved mode from previously clicking
188188
* the mode button, use that.
189189
*/
190-
const storedMode = localStorage.getItem(MODE_STORAGE_KEY);
190+
const storedMode = window !== undefined ? localStorage.getItem(MODE_STORAGE_KEY) : null;
191191
if (storedMode) return storedMode;
192192

193193
/**
@@ -201,7 +201,7 @@ export default function Playground({
201201
* If there is a saved target from previously clicking the
202202
* framework buttons, and there is code for it, use that.
203203
*/
204-
const storedTarget = localStorage.getItem(USAGE_TARGET_STORAGE_KEY);
204+
const storedTarget = window !== undefined ? localStorage.getItem(USAGE_TARGET_STORAGE_KEY) : null;
205205
if (storedTarget && code[storedTarget] !== undefined) {
206206
return storedTarget;
207207
}
@@ -242,43 +242,49 @@ export default function Playground({
242242
const [resetCount, setResetCount] = useState(0);
243243

244244
const setAndSaveMode = (mode: Mode) => {
245-
localStorage.setItem(MODE_STORAGE_KEY, mode);
246245
setIonicMode(mode);
247246

248-
/**
249-
* Tell other playgrounds on the page that the mode has
250-
* updated, so they can sync up.
251-
*/
252-
window.dispatchEvent(
253-
new CustomEvent(MODE_UPDATED_EVENT, {
254-
detail: mode,
255-
})
256-
);
247+
if (window !== undefined) {
248+
localStorage.setItem(MODE_STORAGE_KEY, mode);
249+
250+
/**
251+
* Tell other playgrounds on the page that the mode has
252+
* updated, so they can sync up.
253+
*/
254+
window.dispatchEvent(
255+
new CustomEvent(MODE_UPDATED_EVENT, {
256+
detail: mode,
257+
})
258+
);
259+
}
257260
};
258261

259262
const setAndSaveUsageTarget = (target: UsageTarget, tab: HTMLElement) => {
260-
localStorage.setItem(USAGE_TARGET_STORAGE_KEY, target);
261263
setUsageTarget(target);
262264

263-
/**
264-
* This prevents the scroll position from jumping around if
265-
* there is a playground above this one with code that changes
266-
* in length between frameworks.
267-
*
268-
* Note that we don't need this when changing the mode because
269-
* the two mode iframes are always the same height.
270-
*/
271-
blockElementScrollPositionUntilNextRender(tab);
265+
if (window !== undefined) {
266+
localStorage.setItem(USAGE_TARGET_STORAGE_KEY, target);
272267

273-
/**
274-
* Tell other playgrounds on the page that the framework
275-
* has updated, so they can sync up.
276-
*/
277-
window.dispatchEvent(
278-
new CustomEvent(USAGE_TARGET_UPDATED_EVENT, {
279-
detail: target,
280-
})
281-
);
268+
/**
269+
* This prevents the scroll position from jumping around if
270+
* there is a playground above this one with code that changes
271+
* in length between frameworks.
272+
*
273+
* Note that we don't need this when changing the mode because
274+
* the two mode iframes are always the same height.
275+
*/
276+
blockElementScrollPositionUntilNextRender(tab);
277+
278+
/**
279+
* Tell other playgrounds on the page that the framework
280+
* has updated, so they can sync up.
281+
*/
282+
window.dispatchEvent(
283+
new CustomEvent(USAGE_TARGET_UPDATED_EVENT, {
284+
detail: target,
285+
})
286+
);
287+
}
282288
};
283289

284290
/**

0 commit comments

Comments
 (0)