Skip to content

Commit e1646c2

Browse files
LessUpCopilot
andcommitted
Fix VitePress Pages routing
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1f88fe0 commit e1646c2

3 files changed

Lines changed: 46 additions & 44 deletions

File tree

docs/.vitepress/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export default withMermaid(defineConfig({
137137
title: siteTitle,
138138
description: siteDescription,
139139
base,
140-
cleanUrls: true,
140+
cleanUrls: false,
141141
lastUpdated: true,
142142
head: [
143143
['link', { rel: 'icon', type: 'image/svg+xml', href: assetPath('/logo.svg') }],

docs/.vitepress/scripts/build-pages.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { fileURLToPath } from 'node:url'
55
const __dirname = path.dirname(fileURLToPath(import.meta.url))
66
const docsRoot = path.resolve(__dirname, '..', '..')
77
const vitepressCli = path.resolve(docsRoot, 'node_modules', 'vitepress', 'dist', 'node', 'cli.js')
8+
const pagesBase = process.env.VITEPRESS_BASE || '/cpp-high-performance-guide/'
89

910
const result = spawnSync(process.execPath, [vitepressCli, 'build'], {
1011
cwd: docsRoot,
1112
stdio: 'inherit',
1213
env: {
1314
...process.env,
14-
VITEPRESS_BASE: '/cpp-high-performance-guide/',
15+
VITEPRESS_BASE: pagesBase,
1516
},
1617
})
1718

docs/.vitepress/tests/pages-build.test.mjs

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,74 @@ import { fileURLToPath } from 'node:url'
88
const __dirname = path.dirname(fileURLToPath(import.meta.url))
99
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json')
1010
const buildPagesScriptPath = path.resolve(__dirname, '..', 'scripts', 'build-pages.mjs')
11+
const configPath = path.resolve(__dirname, '..', 'config.ts')
1112
const docsRoot = path.resolve(__dirname, '..', '..')
1213
const distRoot = path.resolve(docsRoot, '.vitepress', 'dist')
1314

14-
let cachedLandingPages
15+
const cachedLandingPages = new Map()
1516

16-
function readBuiltLandingPages() {
17-
if (!cachedLandingPages) {
17+
function readBuiltLandingPages(base = '/cpp-high-performance-guide/') {
18+
if (!cachedLandingPages.has(base)) {
1819
const result = spawnSync(process.execPath, [buildPagesScriptPath], {
1920
cwd: docsRoot,
2021
encoding: 'utf8',
22+
env: {
23+
...process.env,
24+
VITEPRESS_BASE: base,
25+
},
2126
})
2227

2328
assert.equal(result.status, 0, result.stderr || result.stdout || 'build:pages failed')
2429

25-
cachedLandingPages = {
30+
cachedLandingPages.set(base, {
2631
en: fs.readFileSync(path.join(distRoot, 'en', 'index.html'), 'utf8'),
2732
zh: fs.readFileSync(path.join(distRoot, 'zh', 'index.html'), 'utf8'),
2833
exercisesIndexExists: fs.existsSync(path.join(distRoot, 'en', 'exercises', 'index.html')),
2934
exercisesReadmeExists: fs.existsSync(path.join(distRoot, 'en', 'exercises', 'README.html')),
30-
}
35+
})
3136
}
3237

33-
return cachedLandingPages
38+
return cachedLandingPages.get(base)
3439
}
3540

36-
test('build:pages uses a cross-platform Node wrapper for the GitHub Pages base path', () => {
41+
test('docs config keeps clean URLs disabled for static hosting', () => {
42+
const config = fs.readFileSync(configPath, 'utf8')
43+
44+
assert.match(config, /cleanUrls:\s*false/)
45+
assert.doesNotMatch(config, /cleanUrls:\s*true/)
46+
})
47+
48+
test('build:pages uses a cross-platform Node wrapper while preserving external base paths', () => {
3749
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
3850
const buildPagesScript = fs.readFileSync(buildPagesScriptPath, 'utf8')
3951

4052
assert.equal(packageJson.scripts['build:pages'], 'node .vitepress/scripts/build-pages.mjs')
4153
assert.match(buildPagesScript, /VITEPRESS_BASE/)
4254
assert.match(buildPagesScript, /\/cpp-high-performance-guide\//)
4355
assert.match(buildPagesScript, /vitepress/)
56+
assert.match(buildPagesScript, /process\.env\.VITEPRESS_BASE\s*\|\|/)
4457
})
4558

46-
test('build:pages prefixes homepage landing links with the GitHub Pages base', () => {
59+
test('build:pages prefixes homepage landing links with the GitHub Pages base and .html leaf routes', () => {
4760
const { en, zh } = readBuiltLandingPages()
4861

4962
for (const href of [
5063
'/cpp-high-performance-guide/en/reference/',
51-
'/cpp-high-performance-guide/en/getting-started/quickstart',
52-
'/cpp-high-performance-guide/en/guides/learning-path',
53-
'/cpp-high-performance-guide/en/exercises/module-02-memory',
54-
'/cpp-high-performance-guide/en/exercises/module-04-simd',
55-
'/cpp-high-performance-guide/en/exercises/module-05-concurrency',
56-
'/cpp-high-performance-guide/en/getting-started/prerequisites',
57-
'/cpp-high-performance-guide/en/guides/profiling-guide',
58-
'/cpp-high-performance-guide/en/guides/optimization-decision-tree',
59-
'/cpp-high-performance-guide/en/guides/validation',
60-
'/cpp-high-performance-guide/en/guides/best-practices',
64+
'/cpp-high-performance-guide/en/getting-started/quickstart.html',
65+
'/cpp-high-performance-guide/en/guides/learning-path.html',
66+
'/cpp-high-performance-guide/en/guides/validation.html',
67+
'/cpp-high-performance-guide/en/contributing/ai-workflow.html',
6168
'/cpp-high-performance-guide/zh/',
6269
]) {
6370
assert.match(en, new RegExp(`href="${href.replaceAll('/', '\\/')}"`))
6471
}
6572

6673
for (const href of [
6774
'/cpp-high-performance-guide/zh/reference/',
68-
'/cpp-high-performance-guide/zh/getting-started/quickstart',
69-
'/cpp-high-performance-guide/zh/guides/learning-path',
70-
'/cpp-high-performance-guide/en/exercises/module-02-memory',
71-
'/cpp-high-performance-guide/en/exercises/module-04-simd',
72-
'/cpp-high-performance-guide/en/exercises/module-05-concurrency',
73-
'/cpp-high-performance-guide/zh/getting-started/prerequisites',
74-
'/cpp-high-performance-guide/zh/guides/profiling-guide',
75-
'/cpp-high-performance-guide/zh/guides/optimization-decision-tree',
76-
'/cpp-high-performance-guide/zh/guides/validation',
77-
'/cpp-high-performance-guide/zh/guides/best-practices',
75+
'/cpp-high-performance-guide/zh/getting-started/quickstart.html',
76+
'/cpp-high-performance-guide/zh/guides/learning-path.html',
77+
'/cpp-high-performance-guide/zh/guides/validation.html',
78+
'/cpp-high-performance-guide/zh/contributing/ai-workflow.html',
7879
'/cpp-high-performance-guide/en/',
7980
]) {
8081
assert.match(zh, new RegExp(`href="${href.replaceAll('/', '\\/')}"`))
@@ -84,14 +85,8 @@ test('build:pages prefixes homepage landing links with the GitHub Pages base', (
8485
'href="/en/reference/"',
8586
'href="/en/getting-started/quickstart"',
8687
'href="/en/guides/learning-path"',
87-
'href="/en/exercises/module-02-memory"',
88-
'href="/en/exercises/module-04-simd"',
89-
'href="/en/exercises/module-05-concurrency"',
90-
'href="/en/getting-started/prerequisites"',
91-
'href="/en/guides/profiling-guide"',
92-
'href="/en/guides/optimization-decision-tree"',
9388
'href="/en/guides/validation"',
94-
'href="/en/guides/best-practices"',
89+
'href="/en/contributing/ai-workflow"',
9590
'href="/zh/"',
9691
]) {
9792
assert.doesNotMatch(en, new RegExp(rawHref.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')))
@@ -101,20 +96,26 @@ test('build:pages prefixes homepage landing links with the GitHub Pages base', (
10196
'href="/zh/reference/"',
10297
'href="/zh/getting-started/quickstart"',
10398
'href="/zh/guides/learning-path"',
104-
'href="/zh/getting-started/prerequisites"',
105-
'href="/zh/guides/profiling-guide"',
106-
'href="/zh/guides/optimization-decision-tree"',
10799
'href="/zh/guides/validation"',
108-
'href="/zh/guides/best-practices"',
100+
'href="/zh/contributing/ai-workflow"',
109101
'href="/en/"',
110-
'href="/en/exercises/module-02-memory"',
111-
'href="/en/exercises/module-04-simd"',
112-
'href="/en/exercises/module-05-concurrency"',
113102
]) {
114103
assert.doesNotMatch(zh, new RegExp(rawHref.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')))
115104
}
116105
})
117106

107+
test('build:pages preserves an externally supplied base path', () => {
108+
const { en, zh } = readBuiltLandingPages('/custom-base/')
109+
110+
assert.match(en, /href="\/custom-base\/en\/getting-started\/quickstart\.html"/)
111+
assert.match(en, /href="\/custom-base\/zh\/"/)
112+
assert.doesNotMatch(en, /href="\/cpp-high-performance-guide\/en\/getting-started\/quickstart\.html"/)
113+
114+
assert.match(zh, /href="\/custom-base\/zh\/getting-started\/quickstart\.html"/)
115+
assert.match(zh, /href="\/custom-base\/en\/"/)
116+
assert.doesNotMatch(zh, /href="\/cpp-high-performance-guide\/zh\/getting-started\/quickstart\.html"/)
117+
})
118+
118119
test('build:pages emits the exercises overview as a clean index route', () => {
119120
const { exercisesIndexExists, exercisesReadmeExists } = readBuiltLandingPages()
120121

0 commit comments

Comments
 (0)