Skip to content

Commit

Permalink
refactor: add PageDataPayload type (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
CHOYSEN authored Jun 9, 2022
1 parent 421f641 commit 8bf4182
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/client/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -119,15 +120,15 @@ 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
}
})
}
}

function shouldHotReload(payload: any): boolean {
function shouldHotReload(payload: PageDataPayload): boolean {
const payloadPath = payload.path.replace(/(\bindex)?\.md$/, '')
const locationPath = location.pathname.replace(/(\bindex)?\.html$/, '')

Expand Down
11 changes: 7 additions & 4 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export type {
HeadConfig,
LocaleConfig,
Header,
DefaultTheme
DefaultTheme,
PageDataPayload
} from '../../types/shared'

export const EXTERNAL_URL_RE = /^https?:/i
Expand Down
5 changes: 5 additions & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ export interface LocaleConfig {
label?: string
selectText?: string
}

export interface PageDataPayload {
path: string
pageData: PageData
}

0 comments on commit 8bf4182

Please sign in to comment.