Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(web): keep the page working without localStorage (#5205)

Mitchell Klijs88faa6eec4b2
diff --git a/web/pages/_app.tsx b/web/pages/_app.tsx
index d0d805b1f2..5c10d516b4 100644
--- a/web/pages/_app.tsx
+++ b/web/pages/_app.tsx
@@ -27,6 +27,11 @@ import { useRouter } from 'next/router';
import { useSelectedLanguage } from 'next-export-i18n';
import { loadViewerLocale, shouldCleanUrlAfterFlip } from '../utils/localeLoader';
import { AntdProvider } from '../components/theme/AntdProvider';
+import { ensureLocalStorage } from '../utils/ensureLocalStorage';
+
+if (typeof window !== 'undefined') {
+ ensureLocalStorage(window);
+}
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (page: ReactElement) => ReactNode;
diff --git a/web/tests/ensureLocalStorage.test.ts b/web/tests/ensureLocalStorage.test.ts
new file mode 100644
index 0000000000..602679fe84
--- /dev/null
+++ b/web/tests/ensureLocalStorage.test.ts
@@ -0,0 +1,50 @@
+import { ensureLocalStorage } from '../utils/ensureLocalStorage';
+
+// A plain object stands in for window, so jsdom's own localStorage stays intact.
+const fakeWindow = (descriptor: PropertyDescriptor): Window => {
+ const win = {} as Window;
+ Object.defineProperty(win, 'localStorage', { configurable: true, ...descriptor });
+ return win;
+};
+
+describe('ensureLocalStorage', () => {
+ test('leaves a working localStorage alone', () => {
+ window.localStorage.setItem('kept', 'yes');
+ expect(ensureLocalStorage(window)).toBe(false);
+ expect(window.localStorage.getItem('kept')).toBe('yes');
+ window.localStorage.removeItem('kept');
+ });
+
+ test('replaces a null localStorage, as a WebView with DOM storage disabled exposes it', () => {
+ const win = fakeWindow({ value: null });
+ expect(ensureLocalStorage(win)).toBe(true);
+ win.localStorage.setItem('lang', 'nl');
+ expect(win.localStorage.getItem('lang')).toBe('nl');
+ });
+
+ test('replaces a localStorage that throws, as blocked storage does', () => {
+ const win = fakeWindow({
+ get() {
+ throw new DOMException('Access is denied for this document.', 'SecurityError');
+ },
+ });
+ expect(ensureLocalStorage(win)).toBe(true);
+ expect(win.localStorage.getItem('missing')).toBeNull();
+ });
+
+ test('the stand-in behaves like Storage', () => {
+ const win = fakeWindow({ value: null });
+ ensureLocalStorage(win);
+ const storage = win.localStorage;
+ storage.setItem('a', 1 as unknown as string);
+ storage.setItem('b', 'two');
+ expect(storage.getItem('a')).toBe('1');
+ expect(storage.length).toBe(2);
+ expect(storage.key(1)).toBe('b');
+ expect(storage.key(5)).toBeNull();
+ storage.removeItem('a');
+ expect(storage.getItem('a')).toBeNull();
+ storage.clear();
+ expect(storage.length).toBe(0);
+ });
+});
diff --git a/web/utils/ensureLocalStorage.ts b/web/utils/ensureLocalStorage.ts
new file mode 100644
index 0000000000..0329699ec7
--- /dev/null
+++ b/web/utils/ensureLocalStorage.ts
@@ -0,0 +1,51 @@
+// localStorage can be null (a WebView with DOM storage disabled) or throw on
+// access (blocked storage). next-export-i18n reads it unguarded on every page,
+// so an in-memory stand-in takes its place there.
+
+function isUsable(win: Window): boolean {
+ try {
+ const storage = win.localStorage;
+ if (!storage) {
+ return false;
+ }
+ // Read, don't write: a write can fail on a full storage that still works.
+ storage.getItem('owncast-storage-probe');
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+function createMemoryStorage(): Storage {
+ const items = new Map<string, string>();
+ return {
+ get length() {
+ return items.size;
+ },
+ clear: () => items.clear(),
+ getItem: key => (items.has(key) ? items.get(key) : null),
+ key: index => Array.from(items.keys())[index] ?? null,
+ removeItem: key => {
+ items.delete(key);
+ },
+ setItem: (key, value) => {
+ items.set(key, String(value));
+ },
+ };
+}
+
+export function ensureLocalStorage(win: Window): boolean {
+ if (isUsable(win)) {
+ return false;
+ }
+ try {
+ Object.defineProperty(win, 'localStorage', {
+ configurable: true,
+ value: createMemoryStorage(),
+ });
+ return true;
+ } catch (e) {
+ console.error(e);
+ return false;
+ }
+}