Skip to content

Commit 1e2c379

Browse files
authored
Fix API stripping JSON incorrectly (#82062)
Backports API stripping JSON fix to 15.4 branch x-ref: #82061
1 parent fe5db65 commit 1e2c379

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

packages/next/src/shared/lib/page-path/normalize-data-path.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { pathHasPrefix } from '../router/utils/path-has-prefix'
2+
13
/**
24
* strip _next/data/<build-id>/ prefix and .json suffix
35
*/
46
export function normalizeDataPath(pathname: string) {
7+
if (!pathHasPrefix(pathname || '/', '/_next/data')) {
8+
return pathname
9+
}
510
pathname = pathname
611
.replace(/\/_next\/data\/[^/]{1,}/, '')
712
.replace(/\.json$/, '')

test/integration/api-support/test/index.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ let mode
2525
let app
2626

2727
function runTests(dev = false) {
28+
it('should not strip .json from API route', async () => {
29+
const res = await fetchViaHTTP(appPort, '/api/hello.json')
30+
expect(res.status).toBe(200)
31+
expect(await res.json()).toEqual({ post: 'hello.json' })
32+
})
33+
2834
it('should handle proxying to self correctly', async () => {
2935
const res1 = await fetchViaHTTP(appPort, '/api/proxy-self')
3036
expect(res1.status).toBe(200)

0 commit comments

Comments
 (0)