Unverified Commit 14a69ef7 authored by WangTao's avatar WangTao Committed by GitHub

fix: preserve trailing slash during export MPA fallback (#97613)

Keeps the trailing slash when a static export falls back to a full page
load, so you land on the canonical URL instead of bouncing through a
redirect.

Fixes https://github.com/vercel/next.js/issues/97608

---------
Co-authored-by: 's avatarJude Gao <jude.gao@vercel.com>
Co-authored-by: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
parent b2e835de
......@@ -11,6 +11,7 @@ import {
NEXT_RSC_UNION_QUERY,
} from './components/app-router-headers'
import { hasBasePath } from './has-base-path'
import { normalizePathTrailingSlash } from './normalize-trailing-slash'
import { removeBasePath } from './remove-base-path'
import type {
NormalizedPathname,
......@@ -225,9 +226,15 @@ export function urlToUrlWithoutFlightMarker(url: URL): URL {
urlWithoutFlightParameters.pathname.endsWith('.txt')
) {
const { pathname } = urlWithoutFlightParameters
const length = pathname.endsWith('/index.txt') ? 10 : 4
// Slice off `/index.txt` or `.txt` from the end of the pathname
urlWithoutFlightParameters.pathname = pathname.slice(0, -length)
// Undo the marker appended in `fetchServerResponse`, which is keyed on
// whether the requested pathname ended with a slash: `index.txt` for
// `/foo/`, `.txt` for `/foo`. Slicing off only `index.txt` keeps that
// slash, then `normalizePathTrailingSlash` applies the configured
// policy so we don't hand-roll a second one here.
const length = pathname.endsWith('/index.txt') ? 9 : 4
urlWithoutFlightParameters.pathname = normalizePathTrailingSlash(
pathname.slice(0, -length)
)
}
}
return urlWithoutFlightParameters
......
......@@ -264,6 +264,7 @@
"test/e2e/app-dir/server-source-maps/server-source-maps.test.ts",
"test/e2e/app-dir/set-cookies/set-cookies.test.ts",
"test/e2e/app-dir/shallow-routing/shallow-routing.test.ts",
"test/e2e/app-dir/static-export-skew-trailing-slash/static-export-skew-trailing-slash.test.ts",
"test/e2e/app-dir/static-generation-status/index.test.ts",
"test/e2e/app-dir/static-shell-debugging/static-shell-debugging.test.ts",
"test/e2e/app-dir/taint/process-taint.test.ts",
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<main>
<h1>Home page</h1>
<Link id="target-link" href="/target/" prefetch={false}>
Target page
</Link>
</main>
)
}
export default function TargetPage() {
return <h1 id="target-page">Target page</h1>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
output: 'export',
trailingSlash: true,
generateBuildId: async () => 'current-build-id',
}
module.exports = nextConfig
import { createReadStream } from 'node:fs'
import { createServer } from 'node:http'
import { join } from 'node:path'
import handler from 'serve-handler'
export function createExportServer(outDir, requests) {
return createServer((request, response) => {
const { pathname } = new URL(request.url, 'http://localhost')
requests.push(pathname)
if (pathname === '/target' || pathname === '/target/') {
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
createReadStream(join(outDir, 'target/index.html')).pipe(response)
return
}
return handler(request, response, { public: outDir })
})
}
import type { Server } from 'node:http'
import { readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { findPort, retry } from 'next-test-utils'
import { isNextStart, nextTestSetup } from 'e2e-utils'
import { createExportServer } from './server.mjs'
describe('static-export-skew-trailing-slash', () => {
if (!isNextStart) {
test('build test should not run during dev test run', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
disableAutoSkewProtection: true,
})
let port: number
let server: Server
const requests: string[] = []
beforeAll(async () => {
await next.build()
const targetFlightPath = join(next.testDir, 'out/target/index.txt')
const targetFlight = readFileSync(targetFlightPath, 'utf8')
const currentBuildId = '"b":"current-build-id"'
if (!targetFlight.includes(currentBuildId)) {
throw new Error('Could not find the current build ID in target RSC data')
}
writeFileSync(
targetFlightPath,
targetFlight.replace(currentBuildId, '"b":"foreign-build-id"')
)
port = await findPort()
server = createExportServer(join(next.testDir, 'out'), requests)
server.listen(port)
})
afterAll(() => {
server?.close()
})
it('preserves the trailing slash during an MPA fallback', async () => {
const browser = await next.browser('/', { baseUrl: port })
await browser.elementById('target-link').click()
await browser.waitForElementByCss('#target-page')
await retry(async () => {
expect(new URL(await browser.url()).pathname).toBe('/target/')
})
expect(requests).toContain('/target/index.txt')
expect(
requests.filter(
(pathname) => pathname === '/target' || pathname === '/target/'
)
).toEqual(['/target/'])
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment