Skip to content

Commit 2d95d11

Browse files
authored
fix(docs|unconfig-vitepress): hydration mismatches issues in some pages, missing components, and bad layout slots renderer (#111)
Signed-off-by: Neko Ayaka <[email protected]>
1 parent f5a3dde commit 2d95d11

File tree

11 files changed

+104
-69
lines changed

11 files changed

+104
-69
lines changed

.github/workflows/ci-build.yaml

+13-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,20 @@ jobs:
6565
- name: Install dependencies
6666
run: pnpm install --frozen-lockfile
6767

68+
- name: Build packages
69+
run: pnpm packages:build
70+
env:
71+
# As suggested in Verbose Build option to be able to track down errors https://github.com/vuejs/vitepress/issues/422
72+
# vitepress build command does not have --debug option, so we need to set it manually where the debug package is used.
73+
DEBUG: '*'
74+
6875
- name: Build docs
69-
run: |
70-
pnpm packages:build
71-
pnpm docs:build
76+
run: pnpm docs:build
77+
env:
78+
# As suggested in Verbose Build option to be able to track down errors https://github.com/vuejs/vitepress/issues/422
79+
# vitepress build command does not have --debug option, so we need to set it manually where the debug package is used.
80+
DEBUG: 'vitepress:*'
81+
VUE_PROD_HYDRATION_MISMATCH_DETAILS_FLAG: '1'
7282

7383
- name: Upload artifact
7484
uses: actions/upload-artifact@v4

docs/.vitepress/config.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cwd } from 'node:process'
1+
import { cwd, env } from 'node:process'
22
import { join, relative } from 'node:path'
33
import { type DefaultTheme, defineConfig } from 'vitepress'
44
import { BiDirectionalLinks } from '@nolebase/markdown-it-bi-directional-links'
@@ -83,8 +83,22 @@ export const sidebars: Record<string, DefaultTheme.Sidebar> = {
8383
],
8484
}
8585

86+
function getVueProdHydrationMismatchDetailsFlag() {
87+
if (!env) {
88+
console.warn('WARNING: env is not available when trying to get Vue Prod Hydration Mismatch Details Flag')
89+
throw new Error('env is not available')
90+
}
91+
92+
return !!env.VUE_PROD_HYDRATION_MISMATCH_DETAILS_FLAG
93+
}
94+
8695
// https://vitepress.dev/reference/site-config
8796
export default defineConfig({
97+
vite: {
98+
define: {
99+
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: getVueProdHydrationMismatchDetailsFlag(),
100+
},
101+
},
88102
lastUpdated: true,
89103
themeConfig: {
90104
outline: 'deep',

docs/vite-env.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Env Variables and Modes | Vite
2+
// https://vitejs.dev/guide/env-and-mode#intellisense-for-typescript
3+
4+
interface ImportMetaEnv {
5+
readonly VUE_PROD_HYDRATION_MISMATCH_DETAILS_FLAG?: '1'
6+
}
7+
8+
interface ImportMeta {
9+
readonly env: ImportMetaEnv
10+
}

docs/vite.config.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
import { join, resolve } from 'node:path'
1+
import { dirname, join, resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import { env } from 'node:process'
24
import { defineConfig } from 'vite'
35
import UnoCSS from 'unocss/vite'
46
import Inspect from 'vite-plugin-inspect'
57
import { GitChangelog, GitChangelogMarkdownSection } from '@nolebase/vitepress-plugin-git-changelog/vite'
68
import { PageProperties, PagePropertiesMarkdownSection } from '@nolebase/vitepress-plugin-page-properties/vite'
79

10+
function getVueProdHydrationMismatchDetailsFlag() {
11+
if (!env) {
12+
console.warn('WARNING: env is not available when trying to get Vue Prod Hydration Mismatch Details Flag')
13+
throw new Error('env is not available')
14+
}
15+
16+
return !!env.VUE_PROD_HYDRATION_MISMATCH_DETAILS_FLAG
17+
}
18+
19+
const __filename = fileURLToPath(import.meta.url)
20+
const __dirname = dirname(__filename)
21+
822
export default defineConfig({
923
assetsInclude: [
1024
'**/*.mov',
1125
'**/*.mp4',
1226
'**/*.riv',
1327
],
28+
define: {
29+
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: getVueProdHydrationMismatchDetailsFlag(),
30+
},
1431
resolve: {
1532
alias: {
1633
'@nolebase/ui': resolve(__dirname, '../packages/ui/src/'),

packages/vitepress-plugin-git-changelog/package.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,17 @@
5454
"build": "unbuild"
5555
},
5656
"peerDependencies": {
57-
"dayjs": ">=1.11.1",
58-
"md5": ">=2.3.0",
59-
"simple-git": ">=3.21.0",
6057
"vitepress": ">=1.0.0-rc.44"
6158
},
6259
"dependencies": {
6360
"@iconify-json/octicon": "^1.1.52",
6461
"@nolebase/ui": "workspace:^",
65-
"dayjs": "^1.11.10",
66-
"md5": "^2.3.0"
67-
},
68-
"devDependencies": {
69-
"builtin-modules": "^3.3.0",
7062
"date-fns": "^3.3.1",
63+
"md5": "^2.3.0",
7164
"simple-git": "^3.22.0"
65+
},
66+
"devDependencies": {
67+
"@types/md5": "^2.3.5",
68+
"builtin-modules": "^3.3.0"
7269
}
7370
}

packages/vitepress-plugin-git-changelog/src/client/components/Changelog.vue

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, inject, ref } from 'vue'
2+
import { computed, inject, onMounted, ref } from 'vue'
33
import { differenceInDays, toDate } from 'date-fns'
44
import { useData } from 'vitepress'
55
@@ -23,27 +23,25 @@ const { t } = useI18n()
2323
const rawPath = useRawPath()
2424
const commits = useCommits(Changelog.commits, rawPath)
2525
26+
const lastChangeDate = ref<Date>(toDate(commits.value[0]?.date_timestamp))
27+
2628
const locale = computed<Locale>(() => {
2729
if (!options.locales || typeof options.locales === 'undefined')
2830
return defaultLocales[lang.value] || defaultEnLocale || {}
2931
3032
return options.locales[lang.value] || defaultEnLocale || {}
3133
})
3234
33-
const lastChangeDate = computed(() => {
34-
const dateTimestamp: number = commits.value[0]?.date_timestamp || 0
35-
if (!dateTimestamp)
36-
return new Date()
37-
38-
return toDate(dateTimestamp)
39-
})
40-
4135
const isFreshChange = computed(() => {
4236
if (!lastChangeDate.value)
4337
return false
4438
4539
return differenceInDays(new Date(), lastChangeDate.value) < 1
4640
})
41+
42+
onMounted(() => {
43+
lastChangeDate.value = toDate(commits.value[0]?.date_timestamp)
44+
})
4745
</script>
4846

4947
<template>
@@ -102,9 +100,11 @@ const isFreshChange = computed(() => {
102100
<a :href="commit.release_tag_url" target="_blank">
103101
<code class="font-bold">{{ commit.tag }}</code>
104102
</a>
105-
<span class="text-xs opacity-50">
106-
{{ t('committedOn', { props: { date: new Date(commit.date).toLocaleDateString() } }) }}
107-
</span>
103+
<ClientOnly>
104+
<span class="text-xs opacity-50" :title="toDate(commit.date_timestamp).toString()">
105+
{{ t('committedOn', { props: { date: toDate(commit.date_timestamp).toLocaleDateString() } }) }}
106+
</span>
107+
</ClientOnly>
108108
</div>
109109
</template>
110110
<template v-else>
@@ -122,9 +122,11 @@ const isFreshChange = computed(() => {
122122
<span>-</span>
123123
<span>
124124
<span class="text-sm <sm:text-xs" v-html="renderCommitMessage(commit.repo_url || 'https://github.com/example/example', commit.message)" />
125-
<span class="text-xs opacity-50">
126-
{{ t('committedOn', { props: { date: new Date(commit.date).toLocaleDateString() } }) }}
127-
</span>
125+
<ClientOnly>
126+
<span class="text-xs opacity-50" :title="toDate(commit.date_timestamp).toString()">
127+
{{ t('committedOn', { props: { date: toDate(commit.date_timestamp).toLocaleDateString() } }) }}
128+
</span>
129+
</ClientOnly>
128130
</span>
129131
</div>
130132
</template>

packages/vitepress-plugin-git-changelog/src/client/components/Contributors.vue

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useRawPath } from '../composables/path'
88
import { useCommits } from '../composables/commits'
99
import { useI18n } from '../composables/i18n'
1010
import { InjectionKey } from '../constants'
11+
import type { Commit } from '../../types'
1112
1213
interface ContributorInfo {
1314
name: string

packages/vitepress-plugin-git-changelog/src/client/utils.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { formatDistanceToNow, toDate } from 'date-fns'
2+
import type { Locale } from 'date-fns'
23
import * as DateFnsLocales from 'date-fns/locale'
34

45
export function renderMarkdown(markdownText = '') {
@@ -24,10 +25,10 @@ export function renderCommitMessage(repoLink: string, msg: string) {
2425
.replace(/\#([0-9]+)/g, `<a href=\'${repoLink}/issues/$1\'>#$1</a>`)
2526
}
2627

27-
export function formatDistanceToNowFromValue(value: string | number | Date, localeName = 'enUS') {
28+
export function formatDistanceToNowFromValue(value: Date, localeName = 'enUS') {
2829
try {
2930
return formatDistanceToNow(toDate(value), {
30-
locale: DateFnsLocales[localeName] || 'enUS',
31+
locale: (DateFnsLocales as Record<string, Locale>)[localeName] || 'enUS',
3132
addSuffix: true,
3233
})
3334
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
interface Commit {
2-
path: string[][]
3-
tag?: string
4-
release_tag_url?: string
5-
hash: string
6-
hash_url?: string
7-
date: string
8-
message: string
9-
refs?: string
10-
body?: string
11-
author_name: string
12-
author_email: string
13-
author_avatar: string
14-
repo_url?: string
15-
}
1+
declare module 'virtual:nolebase-git-changelog' {
2+
import type { Commit } from '../types'
163

17-
interface Changelog {
18-
commits: Commit[]
19-
}
4+
interface Changelog {
5+
commits: Commit[]
6+
}
207

21-
declare module 'virtual:nolebase-git-changelog' {
228
const changelog: Changelog
239
export default changelog
2410
}

packages/vitepress-plugin-git-changelog/src/types/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface Commit {
55
hash: string
66
hash_url?: string
77
date: string
8-
date_timestamp?: number
8+
date_timestamp: number
99
message: string
1010
refs?: string
1111
body?: string

pnpm-lock.yaml

+16-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)