Skip to content

Commit

Permalink
feat: expose isNotFound on PageData, deperecate Theme.NotFound
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 7, 2023
1 parent 631e471 commit 74caccd
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 19 deletions.
29 changes: 28 additions & 1 deletion docs/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@ Returns page-specific data. The returned object has the following type:

```ts
interface VitePressData<T = any> {
/**
* Site-level data
*/
site: Ref<SiteData<T>>
/**
* themeConfig from .vitepress/config.js
*/
theme: Ref<T>
/**
* Page-level data
*/
page: Ref<PageData>
theme: Ref<T> // themeConfig from .vitepress/config.js
/**
* Page frontmatter
*/
frontmatter: Ref<PageData['frontmatter']>
/**
* Dynamic route params
*/
params: Ref<PageData['params']>
title: Ref<string>
description: Ref<string>
Expand All @@ -24,6 +39,18 @@ interface VitePressData<T = any> {
dir: Ref<string>
localeIndex: Ref<string>
}

interface PageData {
title: string
titleTemplate?: string | boolean
description: string
relativePath: string
headers: Header[]
frontmatter: Record<string, any>
params?: Record<string, any>
isNotFound?: boolean
lastUpdated?: number
}
```

**Example:**
Expand Down
2 changes: 1 addition & 1 deletion src/client/app/components/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Content = defineComponent({
const route = useRoute()
return () =>
h(props.as, { style: { position: 'relative' } }, [
route.component ? h(route.component) : null
route.component ? h(route.component) : '404 Page Not Found'
])
}
})
17 changes: 16 additions & 1 deletion src/client/app/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ import {
export const dataSymbol: InjectionKey<VitePressData> = Symbol()

export interface VitePressData<T = any> {
/**
* Site-level info
*/
site: Ref<SiteData<T>>
page: Ref<PageData>
/**
* themeConfig from .vitepress/config.js
*/
theme: Ref<T>
/**
* Page-level info
*/
page: Ref<PageData>
/**
* page frontmatter data
*/
frontmatter: Ref<PageData['frontmatter']>
/**
* dynamic route params
*/
params: Ref<PageData['params']>
title: Ref<string>
description: Ref<string>
Expand Down
7 changes: 1 addition & 6 deletions src/client/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { ClientOnly } from './components/ClientOnly.js'
import { useCopyCode } from './composables/copyCode.js'
import { useCodeGroups } from './composables/codeGroups.js'

const NotFound = Theme.NotFound || (() => '404 Not Found')

const VitePressApp = defineComponent({
name: 'VitePressApp',
setup() {
Expand Down Expand Up @@ -59,9 +57,6 @@ export async function createApp() {
const data = initData(router.route)
app.provide(dataSymbol, data)

// provide this to avoid circular dependency in VPContent
app.provide('NotFound', NotFound)

// install global components
app.component('Content', Content)
app.component('ClientOnly', ClientOnly)
Expand Down Expand Up @@ -127,7 +122,7 @@ function newRouter(): Router {
}

return import(/*@vite-ignore*/ pageFilePath)
}, NotFound)
}, Theme.NotFound)
}

if (inBrowser) {
Expand Down
6 changes: 5 additions & 1 deletion src/client/app/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export interface EnhanceAppContext {

export interface Theme {
Layout: Component
NotFound?: Component
enhanceApp?: (ctx: EnhanceAppContext) => Awaitable<void>
setup?: () => void

/**
* @deprecated Render not found page by checking `useData().page.value.isNotFound` in Layout instead.
*/
NotFound?: Component
}
10 changes: 4 additions & 6 deletions src/client/theme-default/components/VPContent.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<script setup lang="ts">
import { useRoute } from 'vitepress'
import { useData } from '../composables/data.js'
import { useSidebar } from '../composables/sidebar.js'
import VPPage from './VPPage.vue'
import VPHome from './VPHome.vue'
import VPDoc from './VPDoc.vue'
import { inject } from 'vue'
import NotFound from '../NotFound.vue'
const route = useRoute()
const { frontmatter } = useData()
const { page, frontmatter } = useData()
const { hasSidebar } = useSidebar()
const NotFound = inject('NotFound')
console.log(page.value)
</script>

<template>
Expand All @@ -23,7 +21,7 @@ const NotFound = inject('NotFound')
'is-home': frontmatter.layout === 'home'
}"
>
<NotFound v-if="route.component === NotFound" />
<NotFound v-if="page.isNotFound" />

<VPPage v-else-if="frontmatter.layout === 'page'" />

Expand Down
2 changes: 0 additions & 2 deletions src/client/theme-default/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import './styles/components/vp-sponsor.css'
import type { Theme } from 'vitepress'
import VPBadge from './components/VPBadge.vue'
import Layout from './Layout.vue'
import NotFound from './NotFound.vue'

export { default as VPHomeHero } from './components/VPHomeHero.vue'
export { default as VPHomeFeatures } from './components/VPHomeFeatures.vue'
Expand All @@ -24,7 +23,6 @@ export { default as VPTeamMembers } from './components/VPTeamMembers.vue'

const theme: Theme = {
Layout,
NotFound,
enhanceApp: ({ app }) => {
app.component('Badge', VPBadge)
}
Expand Down
3 changes: 2 additions & 1 deletion src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const notFoundPageData: PageData = {
description: 'Not Found',
headers: [],
frontmatter: { sidebar: false, layout: 'page' },
lastUpdated: 0
lastUpdated: 0,
isNotFound: true
}

export function isActive(
Expand Down
1 change: 1 addition & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface PageData {
headers: Header[]
frontmatter: Record<string, any>
params?: Record<string, any>
isNotFound?: boolean
lastUpdated?: number
}

Expand Down

0 comments on commit 74caccd

Please sign in to comment.