Unverified Commit acb46e10 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix NextUrl trailing slash normalize for data route (#41311)

x-ref: [slack
thread](https://vercel.slack.com/archives/C045FKE5P51/p1665074590321179)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
parent 35308c66
...@@ -17,6 +17,10 @@ export function formatNextPathnameInfo(info: ExtendedInfo) { ...@@ -17,6 +17,10 @@ export function formatNextPathnameInfo(info: ExtendedInfo) {
info.ignorePrefix info.ignorePrefix
) )
if (info.buildId || !info.trailingSlash) {
pathname = removeTrailingSlash(pathname)
}
if (info.buildId) { if (info.buildId) {
pathname = addPathSuffix( pathname = addPathSuffix(
addPathPrefix(pathname, `/_next/data/${info.buildId}`), addPathPrefix(pathname, `/_next/data/${info.buildId}`),
...@@ -25,8 +29,8 @@ export function formatNextPathnameInfo(info: ExtendedInfo) { ...@@ -25,8 +29,8 @@ export function formatNextPathnameInfo(info: ExtendedInfo) {
} }
pathname = addPathPrefix(pathname, info.basePath) pathname = addPathPrefix(pathname, info.basePath)
return info.trailingSlash return !info.buildId && info.trailingSlash
? !info.buildId && !pathname.endsWith('/') ? !pathname.endsWith('/')
? addPathSuffix(pathname, '/') ? addPathSuffix(pathname, '/')
: pathname : pathname
: removeTrailingSlash(pathname) : removeTrailingSlash(pathname)
......
...@@ -263,6 +263,39 @@ it('correctly parses a prefetch url', async () => { ...@@ -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 () => { it('correctly parses a prefetch index url', async () => {
const url = new NextURL( const url = new NextURL(
'/_next/data/development/index.json', '/_next/data/development/index.json',
......
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