Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions packages/client/src/composables/clientData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,37 @@ import { inject } from 'vue'

import type { ClientData } from '../types/index.js'

const CLIENT_DATA_SYMBOL_GLOBAL_KEY = '__VUEPRESS_CLIENT_DATA_SYMBOL__'

/**
* Injection key for client data
*
* Use a global Symbol to keep the same reference across module reloads.
* This is required for HMR in dev mode: when a markdown page with local
* components is edited, the main module (and its transitive deps that host
* this symbol) may be re-executed by Vite, which would otherwise create a
* new Symbol instance and break `inject` (see vuepress/core#1715).
*/
const resolveClientDataSymbol = (): InjectionKey<ClientData> => {
const existing = globalThis[
CLIENT_DATA_SYMBOL_GLOBAL_KEY
] as InjectionKey<ClientData> | null

if (existing) return existing

const symbol: InjectionKey<ClientData> = Symbol('clientData')
globalThis[CLIENT_DATA_SYMBOL_GLOBAL_KEY] = symbol
return symbol
}

/**
* The problem only occurs during hot updates in the development environment,
* It only needs to be processed in the development environment.
* (see vuepress/core#1715)
*/
export const clientDataSymbol: InjectionKey<ClientData> = Symbol(
__VUEPRESS_DEV__ ? 'clientData' : '',
)
export const clientDataSymbol: InjectionKey<ClientData> = __VUEPRESS_DEV__
? resolveClientDataSymbol()
: Symbol('')

/**
* Returns client data
Expand Down
Loading