Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr-handlers): fix globalThis is not defined #1175

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/core/package.json
Expand Up @@ -40,6 +40,7 @@
"homepage": "https://github.com/vueuse/vueuse#readme",
"peerDependencies": {
"@vue/composition-api": "^1.1.0",
"globalthis": "^1.0.2",
"vue": "^2.6.0 || ^3.2.0"
},
"peerDependenciesMeta": {
Expand All @@ -52,6 +53,10 @@
},
"dependencies": {
"@vueuse/shared": "workspace:*",
"globalthis": "^1.0.2",
"vue-demi": "*"
},
"devDependencies": {
"@types/globalthis": "^1.0.1"
}
}
6 changes: 3 additions & 3 deletions packages/core/ssr-handlers.ts
@@ -1,4 +1,5 @@
import type { Awaitable } from '@vueuse/shared'
import globalThis from 'globalthis'

export interface StorageLikeAsync {
getItem(key: string): Awaitable<string | null>
Expand All @@ -21,12 +22,11 @@ export interface SSRHandlersMap {
updateHTMLAttrs: (selector: string, attribute: string, value: string) => void
}

const _global = globalThis || this
const globalKey = '__vueuse_ssr_handlers__'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of extra deps, use

const _global = typeof globalThis === 'undefined' ? this : globalThis

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the build output this will transform to undefined

// @ts-expect-error inject global
_global[globalKey] = _global[globalKey] || {}
globalThis[globalKey] = globalThis[globalKey] || {}
// @ts-expect-error inject global
const handlers: Partial<SSRHandlersMap> = _global[globalKey]
const handlers: Partial<SSRHandlersMap> = globalThis[globalKey]

export function getSSRHandler<T extends keyof SSRHandlersMap>(key: T, fallback: SSRHandlersMap[T]): SSRHandlersMap[T]
export function getSSRHandler<T extends keyof SSRHandlersMap>(key: T, fallback: SSRHandlersMap[T] | undefined): SSRHandlersMap[T] | undefined
Expand Down
13 changes: 9 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.