Skip to content

Commit

Permalink
fix: safe access to localStorage and window (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
enzonotario authored Jan 17, 2025
1 parent 82427d4 commit 5a9bfce
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/theme/components/sandbox/Sandbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/sandbox/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ layout: false
import Sandbox from '../.vitepress/theme/components/sandbox/Sandbox.vue'
</script>

<ClientOnly>

<Sandbox />

</ClientOnly>

2 changes: 1 addition & 1 deletion src/components/Path/OAPath.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
4 changes: 3 additions & 1 deletion src/components/Playground/OAPlaygroundParameters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ function setAuthorizations(schemes: Record<string, PlaygroundSecurityScheme>) {
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),
}
})
}
Expand Down
34 changes: 18 additions & 16 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5a9bfce

Please # to comment.