Skip to content

Commit

Permalink
Fix NextUrl trailing slash normalize for data route (#41311)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
ijjk committed Oct 10, 2022
1 parent 35308c6 commit acb46e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
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

0 comments on commit acb46e1

Please sign in to comment.