Repository navigation
fix(workspace): finish guarding browser storage reads - #827
Ishaan Gangwani (ishaan1124) merged 2 commits into
Conversation
synthetic-sciences#771 makes the workspace, artifact and session-tab stores read `localStorage` defensively, but the blank page it sets out to prevent is still reachable. With storage blocked, three reads run before any of that code executes. The theme provider is the one that matters. `getStoredColorScheme` reads `localStorage` with no guard at all, `ThemeProvider` calls it from its synchronous `init`, and `app.tsx` mounts `ThemeProvider` as an ancestor of `ErrorBoundary`. Nothing above the boundary can catch the throw, so the app still dies on first render. This is why the changelog entry in synthetic-sciences#771 overstates what that PR delivers. The rest are the same mistake synthetic-sciences#771 already identified, in the form where a `typeof` guard sits outside the `try` that was meant to cover it. A `typeof` check invokes the very getter that throws, so it cannot protect the read: - `pages/session.tsx` read the sidebar collapse state and width, called at render. - `atlas/SkillsPage.tsx` read `sessionStorage` and `localStorage`. - `components/prompt-input.tsx` read `localStorage` in a component body. `atlas/right-pane-layout.ts` has a subtler version: `localStorage` is a default parameter, and default parameters are evaluated at the call site, before the function's own `try` can catch anything. The read path was already called inside a `try` by `RightPane`; only the write path was exposed. That file's sibling `artifact-view.ts` documents the correct pattern in a comment, so this follows it. Net effect: with storage blocked the workspace now boots and persists nothing, instead of rendering a blank page. Single parent, rebased onto current `main`, so a rebase carries it. This is a separate branch from synthetic-sciences#771 on purpose: that branch is being rebased, and these files do not overlap with it. Refs synthetic-sciences#771
|
ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel. A member of the Team first needs to authorize it. |
Two CI corrections to the storage sweep. `theme-lock.test.ts` pins its expectations against the *source text* of theme/context.tsx, including the exact line localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_LIGHT) Routing that block through `storageOrNothing` renamed the call and broke the assertion. The legacy-key cleanup is now guarded in place with a try/catch, which keeps the pinned text and matches the idiom the same file already uses for the CSS cache a few lines above. The reads still go through the helper, because no test pins those. Dropping the `= localStorage` default also shortened the readPaneWidth signature below the print width, so Prettier reflows it onto one line.
|
Second commit pushed to fix the two checks that failed on the first one. What CI caught
expect(theme).toContain("localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_LIGHT)")Routing the legacy-key cleanup through my Rather than edit the test, I kept the pinned text and guarded that block in place with a try {
localStorage.setItem(STORAGE_KEYS.THEME_ID, lockedTheme)
localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_LIGHT)
localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_DARK)
} catch {}The reads still go through Why this is worth knowing beyond this PRThat test is a source-text assertion, so it is sensitive to renames rather than to behaviour. It passed while Current stateAll 8 checks green on
Both commits have a single parent, so a rebase carries them. |
01f0ced
into
synthetic-sciences:main
Completes the storage-guard sweep started in #771. Refs #771.
Why this is a separate PR
#771 makes the workspace, artifact and session-tab stores read
localStoragedefensively. That is correct, but the blank page it sets out to prevent is still reachable, because three reads run before any of that code executes. The remaining sites do not overlap with #771's files, so this is a separate branch rather than more commits on a PR that is currently being rebased.The blocker: the theme provider sits above the error boundary
This is the one that matters, and it is not a
typeof-guard mistake at all, it is a completely unguarded read.getStoredColorSchemeinfrontend/ui/src/theme/context.tsx:35reads the global with notryof any kind.ThemeProvidercalls it from its synchronousinitat:84,createSimpleContextruns thatinitin the provider body, andapp.tsxmounts the tree as:ErrorBoundaryis a descendant of the provider that throws, so there is nothing above it to catch the error. With storage blocked the app still renders a blank page, just earlier and from a different file than #771 addresses. The same file already wraps its write at:45-47intry/catch, so the read was simply missed.Net effect after this change: with storage blocked the workspace boots and persists nothing, instead of dying on first render.
The rest: a
typeofguard sitting outside thetryA
typeofcheck invokes the very getter that throws, so it cannot protect the read. #771 identified this pattern; these sites had it:pages/session.tsx:108-115, 123-131createSignalatlas/SkillsPage.tsx:83-104sessionStorage, the only two occurrences in the repoatlas/SkillsPage.tsx:247localStoragein a component bodycomponents/prompt-input.tsx:948localStoragein a component bodyIn each case the existence check moves inside the
try, which is whatentry.tsx:27-31already does, with a comment explaining exactly why.A subtler one: a default argument escapes its own
tryatlas/right-pane-layout.ts:50-79tookstorage = localStorageas a default parameter. Default parameters are evaluated at the call site, before the function body runs, so thetryinside the function could never catch a throwing read.RightPanecalls both functions without the argument, so the guard was inert in practice.This contradicts a convention the repo already documents, in
atlas/files/artifact-view.ts:17-21: resolve storage inside the caller'stry, never as a default. Both functions now takestorage?and resolve it inside the guard.Scope
CHANGELOG.mdentry on purpose. fix(workspace): read the localStorage global defensively #771 owns the blocked-storage entry, and both edits would land in the same region. Whoever merges these two should reconcile the wording; as it stands fix(workspace): read the localStorage global defensively #771's "The workspace starts when browser storage is blocked" becomes accurate once this lands, rather than before.ThemeProvidercannot be exercised without a Solid render harness. I did not want to add a test that only re-asserts thetryis present. The honest coverage is CI plus a manual check, and I would rather say so than pad the diff. Ablocked-storage.test.tsaddition in fix(workspace): read the localStorage global defensively #771 that installs a throwing getter and renders the provider would be the right follow-up.frontend/workspace/public/openscience-theme-preload.jshas three unguarded reads, but it is a classic load-time script, so a throw only skips the theme preload and themeta[name=theme-color]update. It cannot stop the app booting. Worth a separate one-linetry, not worth blocking this on.Verification
This is a single-parent commit on
f1bdf252, deliberately. #771's original fix was lost because it rode in on a merge commit that a later rebase dropped; this branch carries a plain single parent so a rebase keeps it.