Skip to content

Commit 5d94d1b

Browse files
Caltsicsapphi-red
andauthored
fix(html): handle trailing slash paths in transformIndexHtml (#22480)
Co-authored-by: sapphi-red <[email protected]>
1 parent 6064fe1 commit 5d94d1b

7 files changed

Lines changed: 70 additions & 5 deletions

File tree

‎packages/vite/src/node/server/middlewares/indexHtml.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ function isBareRelative(url: string) {
128128
return wordCharRE.test(url[0]) && !url.includes(':')
129129
}
130130

131+
function getHtmlDirnameForRelativeUrl(htmlPath: string): string {
132+
return htmlPath.endsWith('/') ? htmlPath : path.posix.dirname(htmlPath)
133+
}
134+
131135
const processNodeUrl = (
132136
url: string,
133137
useSrcSetReplacer: boolean,
@@ -165,7 +169,7 @@ const processNodeUrl = (
165169
} else if (url[0] === '.' || isBareRelative(url)) {
166170
preTransformUrl = path.posix.join(
167171
config.base,
168-
path.posix.dirname(htmlPath),
172+
getHtmlDirnameForRelativeUrl(htmlPath),
169173
url,
170174
)
171175
}

‎playground/ssr-html/__tests__/serve.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33

44
import path from 'node:path'
55
import kill from 'kill-port'
6-
import { hmrPorts, ports, rootDir } from '~utils'
6+
import { createInMemoryLogger, hmrPorts, ports, rootDir } from '~utils'
77

88
export const port = ports['ssr-html']
9+
export const serverLogs: string[] = []
910

1011
export async function serve(): Promise<{ close(): Promise<void> }> {
1112
await kill(port)
1213

1314
const { createServer } = await import(path.resolve(rootDir, 'server.js'))
14-
const { app, vite } = await createServer(rootDir, hmrPorts['ssr-html'])
15+
const { app, vite } = await createServer(
16+
rootDir,
17+
hmrPorts['ssr-html'],
18+
createInMemoryLogger(serverLogs),
19+
)
1520

1621
return new Promise((resolve, reject) => {
1722
try {

‎playground/ssr-html/__tests__/ssr-html.spec.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { execFile } from 'node:child_process'
22
import { promisify } from 'node:util'
33
import path from 'node:path'
44
import { fileURLToPath } from 'node:url'
5+
import { setTimeout } from 'node:timers/promises'
56
import { describe, expect, test } from 'vitest'
6-
import { port } from './serve'
7+
import { port, serverLogs } from './serve'
78
import { editFile, isServe, page } from '~utils'
89

910
const url = `http://localhost:${port}`
@@ -43,6 +44,32 @@ describe.runIf(isServe)('injected inline scripts', () => {
4344
})
4445
})
4546

47+
describe.runIf(isServe)('trailing slash html paths', () => {
48+
test('pre-transforms relative module scripts from the trailing slash directory', async () => {
49+
serverLogs.length = 0
50+
51+
const response = await fetch(`${url}/trailing-slash/dir/`)
52+
expect(response.status).toBe(200)
53+
await response.text()
54+
55+
await setTimeout(100) // wait for pre-transform to happen
56+
expect(serverLogs).not.toEqual(
57+
expect.arrayContaining([expect.stringContaining('Pre-transform error')]),
58+
)
59+
})
60+
61+
test('loads relative module scripts from the trailing slash directory', async () => {
62+
await page.goto(`${url}/trailing-slash/dir/`)
63+
64+
await expect
65+
.poll(() => page.textContent('.relative-script'))
66+
.toBe('relative module loaded')
67+
await expect
68+
.poll(() => page.textContent('.relative-parent-script'))
69+
.toBe('relative parent module loaded')
70+
})
71+
})
72+
4673
describe.runIf(isServe)('hmr', () => {
4774
test('handle virtual module updates', async () => {
4875
await page.goto(url)

‎playground/ssr-html/server.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ const DYNAMIC_STYLES = `
2121
</style>
2222
`
2323

24-
export async function createServer(root = process.cwd(), hmrPort) {
24+
export async function createServer(
25+
root = process.cwd(),
26+
hmrPort,
27+
customLogger,
28+
) {
2529
const resolve = (p) => path.resolve(import.meta.dirname, p)
2630

2731
const app = express()
@@ -47,6 +51,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
4751
},
4852
},
4953
appType: 'custom',
54+
customLogger,
5055
plugins: [
5156
{
5257
name: 'virtual-file',
@@ -69,6 +74,13 @@ export async function createServer(root = process.cwd(), hmrPort) {
6974
app.use('*all', async (req, res, next) => {
7075
try {
7176
let [url] = req.originalUrl.split('?')
77+
78+
if (url === '/trailing-slash/dir/') {
79+
const template = fs.readFileSync(resolve(`.${url}index.html`), 'utf-8')
80+
const html = await vite.transformIndexHtml(url, template)
81+
return res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
82+
}
83+
7284
if (url.endsWith('/')) url += 'index.html'
7385

7486
if (url.startsWith('/favicon.ico')) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
document.querySelector('.relative-script').textContent =
2+
'relative module loaded'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Trailing slash</title>
6+
</head>
7+
<body>
8+
<div class="relative-script"></div>
9+
<div class="relative-parent-script"></div>
10+
<script type="module" src="./filename.js"></script>
11+
<script type="module" src="../other.js"></script>
12+
</body>
13+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
document.querySelector('.relative-parent-script').textContent =
2+
'relative parent module loaded'

0 commit comments

Comments
 (0)