From 556b02e59e86da1ffde7878f6b4a00e117910299 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Wed, 20 Sep 2023 15:30:00 -0700 Subject: [PATCH 1/2] fix: avoid relying on document for preview file used in the compose/testing api --- .../src/modules/preview-web/UrlStore.ts | 32 ++++++++++--------- .../src/modules/preview-web/WebView.ts | 32 ++++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/code/lib/preview-api/src/modules/preview-web/UrlStore.ts b/code/lib/preview-api/src/modules/preview-web/UrlStore.ts index 16d66ba7f6b6..0e31b9fef810 100644 --- a/code/lib/preview-api/src/modules/preview-web/UrlStore.ts +++ b/code/lib/preview-api/src/modules/preview-web/UrlStore.ts @@ -22,7 +22,7 @@ const getQueryString = ({ selection?: Selection; extraParams?: qs.ParsedQs; }) => { - const { search = '' } = document.location; + const search = (document && document.location && document.location.search) || ''; const { path, selectedKind, selectedStory, ...rest } = qs.parse(search, { ignoreQueryPrefix: true, }); @@ -65,20 +65,22 @@ const getFirstString = (v: ValueOf): string | void => { }; export const getSelectionSpecifierFromPath: () => SelectionSpecifier | null = () => { - const query = qs.parse(document.location.search, { ignoreQueryPrefix: true }); - const args = typeof query.args === 'string' ? parseArgsParam(query.args) : undefined; - const globals = typeof query.globals === 'string' ? parseArgsParam(query.globals) : undefined; - - let viewMode = getFirstString(query.viewMode) as ViewMode; - if (typeof viewMode !== 'string' || !viewMode.match(/docs|story/)) { - viewMode = 'story'; - } - - const path = getFirstString(query.path); - const storyId = path ? pathToId(path) : getFirstString(query.id); - - if (storyId) { - return { storySpecifier: storyId, args, globals, viewMode }; + if (document && document.location && document.location.search) { + const query = qs.parse(document.location.search, { ignoreQueryPrefix: true }); + const args = typeof query.args === 'string' ? parseArgsParam(query.args) : undefined; + const globals = typeof query.globals === 'string' ? parseArgsParam(query.globals) : undefined; + + let viewMode = getFirstString(query.viewMode) as ViewMode; + if (typeof viewMode !== 'string' || !viewMode.match(/docs|story/)) { + viewMode = 'story'; + } + + const path = getFirstString(query.path); + const storyId = path ? pathToId(path) : getFirstString(query.id); + + if (storyId) { + return { storySpecifier: storyId, args, globals, viewMode }; + } } return null; diff --git a/code/lib/preview-api/src/modules/preview-web/WebView.ts b/code/lib/preview-api/src/modules/preview-web/WebView.ts index 6f02568934c3..0f1aa060f967 100644 --- a/code/lib/preview-api/src/modules/preview-web/WebView.ts +++ b/code/lib/preview-api/src/modules/preview-web/WebView.ts @@ -46,22 +46,24 @@ export class WebView implements View { constructor() { // Special code for testing situations - // eslint-disable-next-line @typescript-eslint/naming-convention - const { __SPECIAL_TEST_PARAMETER__ } = qs.parse(document.location.search, { - ignoreQueryPrefix: true, - }); - switch (__SPECIAL_TEST_PARAMETER__) { - case 'preparing-story': { - this.showPreparingStory(); - this.testing = true; - break; - } - case 'preparing-docs': { - this.showPreparingDocs(); - this.testing = true; - break; + if (document && document.location && document.location.search) { + // eslint-disable-next-line @typescript-eslint/naming-convention + const { __SPECIAL_TEST_PARAMETER__ } = qs.parse(document.location.search, { + ignoreQueryPrefix: true, + }); + switch (__SPECIAL_TEST_PARAMETER__) { + case 'preparing-story': { + this.showPreparingStory(); + this.testing = true; + break; + } + case 'preparing-docs': { + this.showPreparingDocs(); + this.testing = true; + break; + } + default: // pass; } - default: // pass; } } From 830890c3013b01c92749f5e0828794a373e7060f Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Tue, 26 Mar 2024 10:28:13 +0100 Subject: [PATCH 2/2] Fix document undefined error in UrlStore and WebView --- .../preview-api/src/modules/preview-web/UrlStore.ts | 4 ++-- .../lib/preview-api/src/modules/preview-web/WebView.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/code/lib/preview-api/src/modules/preview-web/UrlStore.ts b/code/lib/preview-api/src/modules/preview-web/UrlStore.ts index 0e31b9fef810..b7890cc3687a 100644 --- a/code/lib/preview-api/src/modules/preview-web/UrlStore.ts +++ b/code/lib/preview-api/src/modules/preview-web/UrlStore.ts @@ -22,7 +22,7 @@ const getQueryString = ({ selection?: Selection; extraParams?: qs.ParsedQs; }) => { - const search = (document && document.location && document.location.search) || ''; + const search = typeof document !== 'undefined' ? document.location.search : ''; const { path, selectedKind, selectedStory, ...rest } = qs.parse(search, { ignoreQueryPrefix: true, }); @@ -65,7 +65,7 @@ const getFirstString = (v: ValueOf): string | void => { }; export const getSelectionSpecifierFromPath: () => SelectionSpecifier | null = () => { - if (document && document.location && document.location.search) { + if (typeof document !== 'undefined') { const query = qs.parse(document.location.search, { ignoreQueryPrefix: true }); const args = typeof query.args === 'string' ? parseArgsParam(query.args) : undefined; const globals = typeof query.globals === 'string' ? parseArgsParam(query.globals) : undefined; diff --git a/code/lib/preview-api/src/modules/preview-web/WebView.ts b/code/lib/preview-api/src/modules/preview-web/WebView.ts index 0f1aa060f967..d960aae1c423 100644 --- a/code/lib/preview-api/src/modules/preview-web/WebView.ts +++ b/code/lib/preview-api/src/modules/preview-web/WebView.ts @@ -38,7 +38,7 @@ const ansiConverter = new AnsiToHtml({ }); export class WebView implements View { - private currentLayoutClass?: typeof layoutClassMap[keyof typeof layoutClassMap] | null; + private currentLayoutClass?: (typeof layoutClassMap)[keyof typeof layoutClassMap] | null; private testing = false; @@ -46,7 +46,7 @@ export class WebView implements View { constructor() { // Special code for testing situations - if (document && document.location && document.location.search) { + if (typeof document !== 'undefined') { // eslint-disable-next-line @typescript-eslint/naming-convention const { __SPECIAL_TEST_PARAMETER__ } = qs.parse(document.location.search, { ignoreQueryPrefix: true, @@ -116,8 +116,10 @@ export class WebView implements View { checkIfLayoutExists(layout: keyof typeof layoutClassMap) { if (!layoutClassMap[layout]) { logger.warn( - dedent`The desired layout: ${layout} is not a valid option. - The possible options are: ${Object.keys(layoutClassMap).join(', ')}, none.` + dedent` + The desired layout: ${layout} is not a valid option. + The possible options are: ${Object.keys(layoutClassMap).join(', ')}, none. + ` ); } }