From 0b4c01cff8fd31fcb003641a9b71e163605b230c Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Tue, 6 Apr 2021 22:07:28 +0200 Subject: [PATCH 1/2] Don't include URL args in docs mode --- lib/api/src/modules/url.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/api/src/modules/url.ts b/lib/api/src/modules/url.ts index 9e28859cb7f6..b38bc7d8a92f 100644 --- a/lib/api/src/modules/url.ts +++ b/lib/api/src/modules/url.ts @@ -151,11 +151,13 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r const initModule = () => { // Sets `args` parameter in URL, omitting any args that have their initial value or cannot be unserialized safely. const updateArgsParam = (args?: Story['args']) => { + const { path, viewMode } = fullAPI.getUrlState(); + if (viewMode !== 'story') return; const currentStory = fullAPI.getCurrentStoryData(); const initialArgs = (isStory(currentStory) && currentStory.initialArgs) || {}; const argsString = buildArgsParam(initialArgs, args); const argsParam = argsString.length ? `&args=${argsString}` : ''; - queryNavigate(`${fullAPI.getUrlState().path}${argsParam}`, { replace: true }); + queryNavigate(`${path}${argsParam}`, { replace: true }); api.setQueryParams({ args: argsString }); }; From 6d548051278b45c7f24d935ef0674eb5284fc1fa Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Wed, 7 Apr 2021 11:53:49 +0200 Subject: [PATCH 2/2] Restore args param when switching stories or viewMode --- lib/api/src/modules/url.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/api/src/modules/url.ts b/lib/api/src/modules/url.ts index b38bc7d8a92f..8a0ae66e13dd 100644 --- a/lib/api/src/modules/url.ts +++ b/lib/api/src/modules/url.ts @@ -8,7 +8,6 @@ import { window } from 'global'; import { ModuleArgs, ModuleFn } from '../index'; import { PanelPositions } from './layout'; import { isStory } from '../lib/stories'; -import type { Story } from '../lib/stories'; interface Additions { isFullscreen?: boolean; @@ -150,11 +149,14 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r const initModule = () => { // Sets `args` parameter in URL, omitting any args that have their initial value or cannot be unserialized safely. - const updateArgsParam = (args?: Story['args']) => { + const updateArgsParam = () => { const { path, viewMode } = fullAPI.getUrlState(); if (viewMode !== 'story') return; + const currentStory = fullAPI.getCurrentStoryData(); - const initialArgs = (isStory(currentStory) && currentStory.initialArgs) || {}; + if (!isStory(currentStory)) return; + + const { args, initialArgs } = currentStory; const argsString = buildArgsParam(initialArgs, args); const argsParam = argsString.length ? `&args=${argsString}` : ''; queryNavigate(`${path}${argsParam}`, { replace: true }); @@ -164,13 +166,13 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r fullAPI.on(SET_CURRENT_STORY, () => updateArgsParam()); let handleOrId: any; - fullAPI.on(STORY_ARGS_UPDATED, ({ args }) => { + fullAPI.on(STORY_ARGS_UPDATED, () => { if ('requestIdleCallback' in window) { if (handleOrId) window.cancelIdleCallback(handleOrId); - handleOrId = window.requestIdleCallback(() => updateArgsParam(args), { timeout: 1000 }); + handleOrId = window.requestIdleCallback(updateArgsParam, { timeout: 1000 }); } else { if (handleOrId) clearTimeout(handleOrId); - setTimeout(updateArgsParam, 100, args); + setTimeout(updateArgsParam, 100); } });