Skip to content

Commit 1df701e

Browse files
authored
feat(route): add cleanUrlsRedirect option (#3571)
1 parent 1af6d99 commit 1df701e

22 files changed

Lines changed: 624 additions & 14 deletions

File tree

‎e2e/fixtures/auto-nav-sidebar/index.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test.describe('Auto nav and sidebar test', async () => {
1818
});
1919

2020
test('Should render nav and sidebar correctly', async ({ page }) => {
21-
await page.goto(`http://localhost:${appPort}/guide/`, {
21+
await page.goto(`http://localhost:${appPort}/guide/index.html`, {
2222
waitUntil: 'networkidle',
2323
});
2424

@@ -295,11 +295,11 @@ test.describe('Auto nav and sidebar test', async () => {
295295
test('Sidebar not have same name md/mdx will not navigate', async ({
296296
page,
297297
}) => {
298-
await page.goto(`http://localhost:${appPort}/guide/`, {
298+
await page.goto(`http://localhost:${appPort}/guide/index.html`, {
299299
waitUntil: 'networkidle',
300300
});
301301
await page.click('.rp-doc-layout__sidebar .rp-sidebar-group');
302-
expect(page.url()).toBe(`http://localhost:${appPort}/guide/`);
302+
expect(page.url()).toBe(`http://localhost:${appPort}/guide/index.html`);
303303
});
304304

305305
test('Should load nested subpage API Overview correctly', async ({
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Folder
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Introduction
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Home
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Folder
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Introduction
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Home
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { expect, test } from '@playwright/test';
2+
import { getPort, killProcess, runDevCommand } from '../../utils/runCommands';
3+
4+
test.describe('route.cleanUrlsRedirect', () => {
5+
let cleanUrlApp: Awaited<ReturnType<typeof runDevCommand>>;
6+
let cleanUrlPort: number;
7+
let disabledApp: Awaited<ReturnType<typeof runDevCommand>>;
8+
let disabledPort: number;
9+
let htmlUrlApp: Awaited<ReturnType<typeof runDevCommand>>;
10+
let htmlUrlPort: number;
11+
12+
test.beforeAll(async () => {
13+
const appDir = import.meta.dirname;
14+
15+
htmlUrlPort = await getPort();
16+
htmlUrlApp = await runDevCommand(appDir, htmlUrlPort);
17+
18+
cleanUrlPort = await getPort();
19+
cleanUrlApp = await runDevCommand(
20+
appDir,
21+
cleanUrlPort,
22+
'rspress.clean-urls.config.ts',
23+
);
24+
25+
disabledPort = await getPort();
26+
disabledApp = await runDevCommand(
27+
appDir,
28+
disabledPort,
29+
'rspress.disabled.config.ts',
30+
);
31+
});
32+
33+
test.afterAll(async () => {
34+
await Promise.all([
35+
killProcess(htmlUrlApp),
36+
killProcess(cleanUrlApp),
37+
killProcess(disabledApp),
38+
]);
39+
});
40+
41+
test('normalizes matched routes when cleanUrls is false', async ({
42+
page,
43+
}) => {
44+
const cases = [
45+
[
46+
'/docs/zh/guide/start/introduction',
47+
'/docs/zh/guide/start/introduction.html',
48+
],
49+
[
50+
'/docs/zh/guide/start/introduction/',
51+
'/docs/zh/guide/start/introduction.html',
52+
],
53+
[
54+
'/docs/zh/guide/start/introduction/index.html',
55+
'/docs/zh/guide/start/introduction.html',
56+
],
57+
['/docs/zh/folder/', '/docs/zh/folder/index.html'],
58+
] as const;
59+
60+
for (const [incomingUrl, expectedUrl] of cases) {
61+
await page.goto(`http://localhost:${htmlUrlPort}${incomingUrl}`, {
62+
waitUntil: 'networkidle',
63+
});
64+
await expect(page).toHaveURL(
65+
`http://localhost:${htmlUrlPort}${expectedUrl}`,
66+
);
67+
await expect(page.locator('.rspress-doc')).toBeVisible();
68+
}
69+
});
70+
71+
test('normalizes matched routes when cleanUrls is true', async ({ page }) => {
72+
const cases = [
73+
[
74+
'/docs/zh/guide/start/introduction.html',
75+
'/docs/zh/guide/start/introduction',
76+
],
77+
[
78+
'/docs/zh/guide/start/introduction/',
79+
'/docs/zh/guide/start/introduction',
80+
],
81+
[
82+
'/docs/zh/guide/start/introduction/index.html',
83+
'/docs/zh/guide/start/introduction',
84+
],
85+
['/docs/zh/folder/index.html', '/docs/zh/folder/'],
86+
] as const;
87+
88+
for (const [incomingUrl, expectedUrl] of cases) {
89+
await page.goto(`http://localhost:${cleanUrlPort}${incomingUrl}`, {
90+
waitUntil: 'networkidle',
91+
});
92+
await expect(page).toHaveURL(
93+
`http://localhost:${cleanUrlPort}${expectedUrl}`,
94+
);
95+
await expect(page.locator('.rspress-doc')).toBeVisible();
96+
}
97+
});
98+
99+
test('does not normalize matched routes when disabled', async ({ page }) => {
100+
const pathname = '/docs/zh/guide/start/introduction.html';
101+
102+
await page.goto(`http://localhost:${disabledPort}${pathname}`, {
103+
waitUntil: 'networkidle',
104+
});
105+
106+
await expect(page).toHaveURL(`http://localhost:${disabledPort}${pathname}`);
107+
await expect(page.locator('.rspress-doc')).toBeVisible();
108+
});
109+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@rspress-fixture/clean-urls-redirect",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"build": "rspress build",
8+
"dev": "rspress dev",
9+
"preview": "rspress preview"
10+
},
11+
"dependencies": {
12+
"@rspress/core": "workspace:*"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^22.8.1"
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as path from 'node:path';
2+
import { defineConfig } from '@rspress/core';
3+
4+
export default defineConfig({
5+
root: path.join(import.meta.dirname, 'doc'),
6+
base: '/docs/',
7+
lang: 'en',
8+
locales: [{ lang: 'en' }, { lang: 'zh' }],
9+
route: {
10+
cleanUrls: true,
11+
localeRedirect: 'never',
12+
},
13+
});

0 commit comments

Comments
 (0)