refactor: add `PageDataPayload` type (#742)

pull/763/head
CHOYSEN 3 years ago committed by GitHub
parent 421f641a76
commit 8bf4182453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,6 +15,7 @@ import { usePrefetch } from './composables/preFetch'
import { dataSymbol, initData } from './data'
import { Content } from './components/Content'
import { ClientOnly } from './components/ClientOnly'
import { PageDataPayload } from '../shared'
const NotFound = Theme.NotFound || (() => '404 Not Found')
@ -119,7 +120,7 @@ function handleHMR(router: Router): void {
// update route.data on HMR updates of active page
if (import.meta.hot) {
// hot reload pageData
import.meta.hot!.on('vitepress:pageData', (payload) => {
import.meta.hot!.on('vitepress:pageData', (payload: PageDataPayload) => {
if (shouldHotReload(payload)) {
router.route.data = payload.pageData
}
@ -127,7 +128,7 @@ function handleHMR(router: Router): void {
}
}
function shouldHotReload(payload: any): boolean {
function shouldHotReload(payload: PageDataPayload): boolean {
const payloadPath = payload.path.replace(/(\bindex)?\.md$/, '')
const locationPath = location.pathname.replace(/(\bindex)?\.html$/, '')

@ -6,6 +6,7 @@ import { DIST_CLIENT_PATH, APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
import { slash } from './utils/slash'
import { OutputAsset, OutputChunk } from 'rollup'
import { staticDataPlugin } from './staticDataPlugin'
import { PageDataPayload } from './shared'
type Awaited<T> = T extends Promise<infer P> ? P : never
@ -262,14 +263,16 @@ export async function createVitePressPlugin(
config.publicDir
)
const payload: PageDataPayload = {
path: `/${slash(path.relative(srcDir, file))}`,
pageData
}
// notify the client to update page data
server.ws.send({
type: 'custom',
event: 'vitepress:pageData',
data: {
path: `/${slash(path.relative(srcDir, file))}`,
pageData
}
data: payload
})
// overwrite src so vue plugin can handle the HMR

@ -6,7 +6,8 @@ export type {
HeadConfig,
LocaleConfig,
Header,
DefaultTheme
DefaultTheme,
PageDataPayload
} from '../../types/shared'
export const EXTERNAL_URL_RE = /^https?:/i

5
types/shared.d.ts vendored

@ -72,3 +72,8 @@ export interface LocaleConfig {
label?: string
selectText?: string
}
export interface PageDataPayload {
path: string
pageData: PageData
}

Loading…
Cancel
Save