From e6470d88b36c115706fcc81bc77bab505f73ffdf Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 16 Mar 2026 10:23:41 +0100 Subject: [PATCH] Prevent story-local viewport from persisting in URL by only syncing user globals rather than all globals --- code/core/src/manager-api/modules/globals.ts | 12 ++++---- .../src/manager-api/tests/globals.test.ts | 28 ++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/code/core/src/manager-api/modules/globals.ts b/code/core/src/manager-api/modules/globals.ts index 4c706a19ee28..d27a33da8ce3 100644 --- a/code/core/src/manager-api/modules/globals.ts +++ b/code/core/src/manager-api/modules/globals.ts @@ -130,7 +130,7 @@ export const init: ModuleFn = ({ store, fullAPI, provider }) = SET_GLOBALS, function handleSetGlobals(this: any, { globals, globalTypes }: SetGlobalsPayload) { const { ref } = getEventMetadata(this, fullAPI)!; - const currentGlobals = store.getState()?.globals; + const currentUserGlobals = store.getState()?.userGlobals; if (!ref) { store.setState({ globals, userGlobals: globals, globalTypes }); @@ -138,14 +138,12 @@ export const init: ModuleFn = ({ store, fullAPI, provider }) = logger.warn('received globals from a non-local ref. This is not currently supported.'); } - // If we have stored globals different to what the preview just inited with, - // we should update it to those values if ( - currentGlobals && - Object.keys(currentGlobals).length !== 0 && - !deepEqual(globals, currentGlobals) + currentUserGlobals && + Object.keys(currentUserGlobals).length !== 0 && + !deepEqual(globals, currentUserGlobals) ) { - api.updateGlobals(currentGlobals); + api.updateGlobals(currentUserGlobals); } } ); diff --git a/code/core/src/manager-api/tests/globals.test.ts b/code/core/src/manager-api/tests/globals.test.ts index 829e771ae4d8..264df9d676e2 100644 --- a/code/core/src/manager-api/tests/globals.test.ts +++ b/code/core/src/manager-api/tests/globals.test.ts @@ -71,7 +71,7 @@ describe('globals API', () => { }); }); - it('emits UPDATE_GLOBALS if retains a globals value different to what receives on SET_GLOBALS', () => { + it('emits UPDATE_GLOBALS if retains a user globals value different to what receives on SET_GLOBALS', () => { const channel = new EventEmitter(); const listener = vi.fn(); channel.on(UPDATE_GLOBALS, listener); @@ -83,6 +83,7 @@ describe('globals API', () => { } as unknown as ModuleArgs); store.setState({ ...state, + userGlobals: { a: 'c' }, globals: { a: 'c' }, }); @@ -103,6 +104,31 @@ describe('globals API', () => { }); }); + it('does not push story globals to preview when SET_GLOBALS fires with empty globals', () => { + const channel = new EventEmitter(); + const listener = vi.fn(); + channel.on(UPDATE_GLOBALS, listener); + + const store = createMockStore(); + const { state } = initModule({ + store, + provider: { channel }, + } as unknown as ModuleArgs); + store.setState({ + ...state, + userGlobals: {}, + storyGlobals: { viewport: 'mobile1' }, + globals: { viewport: 'mobile1' }, + }); + + channel.emit(SET_GLOBALS, { + globals: {}, + globalTypes: {}, + } satisfies SetGlobalsPayload); + + expect(listener).not.toHaveBeenCalled(); + }); + it('ignores SET_STORIES from other refs', () => { const channel = new EventEmitter(); const api = { findRef: vi.fn() };