From 5a9bfcef9c37efc79da98cc444728949cf76071c Mon Sep 17 00:00:00 2001 From: Enzo Notario Date: Fri, 17 Jan 2025 19:52:07 -0300 Subject: [PATCH] fix: safe access to localStorage and window (#155) --- .../theme/components/sandbox/Sandbox.vue | 2 +- docs/sandbox/index.md | 4 +++ src/components/Path/OAPath.vue | 2 +- .../Playground/OAPlaygroundParameters.vue | 4 ++- src/lib/utils.ts | 34 ++++++++++--------- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/docs/.vitepress/theme/components/sandbox/Sandbox.vue b/docs/.vitepress/theme/components/sandbox/Sandbox.vue index 31e7987..d1954dd 100644 --- a/docs/.vitepress/theme/components/sandbox/Sandbox.vue +++ b/docs/.vitepress/theme/components/sandbox/Sandbox.vue @@ -23,7 +23,7 @@ const themeConfig = useTheme() const { toast } = useToast() -const initialSpecUrl = (window && new URLSearchParams(location.search).get('specUrl')) || 'https://vitepress-openapi.vercel.app/openapi.json' +const initialSpecUrl = new URLSearchParams(location.search).get('specUrl') || 'https://vitepress-openapi.vercel.app/openapi.json' const sandboxDataQuery = new URLSearchParams(location.search).get('sandboxData') const sandboxDataQueryDecompressed = sandboxDataQuery ? JSON.parse(decompressFromURL(sandboxDataQuery)) : null diff --git a/docs/sandbox/index.md b/docs/sandbox/index.md index cda3b43..98957a5 100644 --- a/docs/sandbox/index.md +++ b/docs/sandbox/index.md @@ -7,5 +7,9 @@ layout: false import Sandbox from '../.vitepress/theme/components/sandbox/Sandbox.vue' + + + + diff --git a/src/components/Path/OAPath.vue b/src/components/Path/OAPath.vue index a797dba..15d10fe 100644 --- a/src/components/Path/OAPath.vue +++ b/src/components/Path/OAPath.vue @@ -61,7 +61,7 @@ const servers = customGetServers const defaultServer = servers.length ? servers[0]?.url : themeConfig.getOperationDefaultBaseUrl() -const selectedServer = servers?.length > 1 +const selectedServer = servers?.length > 1 && typeof localStorage !== 'undefined' ? useStorage(`--oa-operation-${props.id}-selectedServer`, defaultServer, localStorage, { mergeDefaults: true, }) diff --git a/src/components/Playground/OAPlaygroundParameters.vue b/src/components/Playground/OAPlaygroundParameters.vue index 10bbb18..a4dc92f 100644 --- a/src/components/Playground/OAPlaygroundParameters.vue +++ b/src/components/Playground/OAPlaygroundParameters.vue @@ -98,7 +98,9 @@ function setAuthorizations(schemes: Record) { type: scheme.type, scheme: scheme.scheme, name, - playgroundValue: useStorage(`--oa-authorization-${name}`, usePlayground().getSecuritySchemeDefaultValue(scheme), localStorage), + playgroundValue: typeof localStorage !== 'undefined' + ? useStorage(`--oa-authorization-${name}`, usePlayground().getSecuritySchemeDefaultValue(scheme), localStorage) + : usePlayground().getSecuritySchemeDefaultValue(scheme), } }) } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index c8cb8df..71248e6 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -12,23 +12,25 @@ export function scrollIntoOperationByOperationId({ offset?: number container?: HTMLElement }) { - if (!import.meta.env.SSR) { - const element = document.querySelector( - hash - // . escape { and } characters - .replace(/([{}])/g, '\\$1'), - ) - - if (!element) { - return - } + if (typeof window === 'undefined') { + return + } - element.scrollIntoView({ - behavior: 'smooth', - block: 'start', - inline: 'nearest', - }) + const element = document.querySelector( + hash + // . escape { and } characters + .replace(/([{}])/g, '\\$1'), + ) - window.location.hash = hash + if (!element) { + return } + + element.scrollIntoView({ + behavior: 'smooth', + block: 'start', + inline: 'nearest', + }) + + window.location.hash = hash }