Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NextUrl trailing slash normalize for data route #41311

Merged
merged 1 commit into from Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,6 +17,10 @@ export function formatNextPathnameInfo(info: ExtendedInfo) {
info.ignorePrefix
)

if (info.buildId || !info.trailingSlash) {
pathname = removeTrailingSlash(pathname)
}

if (info.buildId) {
pathname = addPathSuffix(
addPathPrefix(pathname, `/_next/data/${info.buildId}`),
Expand All @@ -25,8 +29,8 @@ export function formatNextPathnameInfo(info: ExtendedInfo) {
}

pathname = addPathPrefix(pathname, info.basePath)
return info.trailingSlash
? !info.buildId && !pathname.endsWith('/')
return !info.buildId && info.trailingSlash
? !pathname.endsWith('/')
? addPathSuffix(pathname, '/')
: pathname
: removeTrailingSlash(pathname)
Expand Down
33 changes: 33 additions & 0 deletions test/unit/web-runtime/next-url.test.ts
Expand Up @@ -263,6 +263,39 @@ it('correctly parses a prefetch url', async () => {
)
})

it('correctly handles trailing slash in _next/data', async () => {
const url = new NextURL('/abc/', 'http://127.0.0.1:3000')
url.buildId = '1234'

expect(url.pathname).toEqual('/abc/')
expect(url.locale).toEqual('')
expect(String(url)).toEqual('http://localhost:3000/_next/data/1234/abc.json')
})

it('correctly handles trailing slash in _next/data with config', async () => {
const url = new NextURL('/abc/', 'http://127.0.0.1:3000', {
nextConfig: { trailingSlash: true },
})
url.buildId = '1234'

expect(url.pathname).toEqual('/abc/')
expect(url.locale).toEqual('')
expect(String(url)).toEqual('http://localhost:3000/_next/data/1234/abc.json')
})

it('correctly handles trailing slash in _next/data with basePath', async () => {
const url = new NextURL('/docs/abc/', 'http://127.0.0.1:3000', {
nextConfig: { basePath: '/docs', trailingSlash: true },
})
url.buildId = '1234'

expect(url.pathname).toEqual('/abc/')
expect(url.locale).toEqual('')
expect(String(url)).toEqual(
'http://localhost:3000/docs/_next/data/1234/abc.json'
)
})

it('correctly parses a prefetch index url', async () => {
const url = new NextURL(
'/_next/data/development/index.json',
Expand Down