Skip to content

Commit 316f9e2

Browse files
fix(signals): expose useSessionStorage as a bare window global
window.useLocalStorage was assigned and window.useSessionStorage was not, so the two composables behaved differently depending on where they were called from. Both are in STX_RUNTIME_GLOBALS, so a <script client> block receives either through the setup destructure and the gap is invisible there — but code reading the window global directly (a plain <script>, or a bundled module the auto-import transform didn't rewrite) found useLocalStorage defined and useSessionStorage undefined. Covered in use-storage-guards.test.ts against both window and window.stx; verified the assertion fails without the assignment.
1 parent 4952707 commit 316f9e2

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

packages/stx/src/signals.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5946,6 +5946,12 @@ else {
59465946
window.nextTick = nextTick;
59475947
window.useAsync = useAsync;
59485948
window.useLocalStorage = useLocalStorage;
5949+
// Paired with the line above. Both are in STX_RUNTIME_GLOBALS, so a
5950+
// <script client> block gets either via the setup destructure — but code
5951+
// that reads the bare window global directly (a plain <script>, or a
5952+
// bundled module that wasn't rewritten) found useLocalStorage defined and
5953+
// useSessionStorage undefined.
5954+
window.useSessionStorage = useSessionStorage;
59495955
window.useEventListener = useEventListener;
59505956
window.useWebSocket = useWebSocket;
59515957
window.useColorMode = useColorMode;

packages/stx/test/signals/use-storage-guards.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,19 @@ describe('useLocalStorage guards (module impl)', () => {
323323
expect(ref.value).toBe('default')
324324
})
325325
})
326+
327+
describe('bare window globals', () => {
328+
// Both names are in STX_RUNTIME_GLOBALS, so a <script client> block gets
329+
// either through the setup destructure. Code reading the window global
330+
// directly — a plain <script>, or a bundled module that wasn't rewritten —
331+
// depends on these assignments, and useSessionStorage was missing one.
332+
it('exposes both storage composables on window', () => {
333+
expect(typeof g.window.useLocalStorage).toBe('function')
334+
expect(typeof g.window.useSessionStorage).toBe('function')
335+
})
336+
337+
it('exposes both on window.stx', () => {
338+
expect(typeof g.window.stx.useLocalStorage).toBe('function')
339+
expect(typeof g.window.stx.useSessionStorage).toBe('function')
340+
})
341+
})

0 commit comments

Comments
 (0)