Skip to content

Commit 81dd0ce

Browse files
committed
test: add test case for next/navigation#redirect
1 parent 19bf8b6 commit 81dd0ce

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { redirect } from 'next/navigation'
2+
3+
const Page = async () => {
4+
return redirect(`/app-redirect/dest`)
5+
}
6+
7+
export const generateStaticParams = async () => {
8+
return [{ slug: 'prerendered' }]
9+
}
10+
11+
export default Page
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function Page() {
2+
return (
3+
<main>
4+
<h1>Hello next/navigation#redirect target</h1>
5+
</main>
6+
)
7+
}
8+
9+
export const dynamic = 'force-static'

tests/integration/simple-app.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ test<FixtureTestContext>('Test that the simple next app is working', async (ctx)
108108
shouldHaveAppRouterNotFoundInPrerenderManifest() ? '/_not-found' : undefined,
109109
'/api/cached-permanent',
110110
'/api/cached-revalidate',
111+
'/app-redirect/dest',
112+
'/app-redirect/prerendered',
111113
'/config-redirect',
112114
'/config-redirect/dest',
113115
'/config-rewrite',
@@ -445,6 +447,31 @@ test.skipIf(hasDefaultTurbopackBuilds())<FixtureTestContext>(
445447
},
446448
)
447449

450+
// below version check is not exact (not located exactly when, but Next.js had a bug for prerender pages that should redirect would not work correctly)
451+
// currently only know that 13.5.1 and 14.2.35 doesn't have correct response (either not a 307 or completely missing 'location' header), while 15.5.9 has 307 response with location header
452+
test.skipIf(nextVersionSatisfies('<15.0.0'))<FixtureTestContext>(
453+
`app router page that uses next/navigation#redirect works when page is prerendered`,
454+
async (ctx) => {
455+
await createFixture('simple', ctx)
456+
await runPlugin(ctx)
457+
458+
const response = await invokeFunction(ctx, { url: `/app-redirect/prerendered` })
459+
460+
expect(response.statusCode).toBe(307)
461+
expect(response.headers['location']).toBe('/app-redirect/dest')
462+
},
463+
)
464+
465+
test<FixtureTestContext>(`app router page that uses next/navigation#redirect works when page is NOT prerendered`, async (ctx) => {
466+
await createFixture('simple', ctx)
467+
await runPlugin(ctx)
468+
469+
const response = await invokeFunction(ctx, { url: `/app-redirect/non-prerendered` })
470+
471+
expect(response.statusCode).toBe(307)
472+
expect(response.headers['location']).toBe('/app-redirect/dest')
473+
})
474+
448475
describe('next patching', async () => {
449476
const { cp: originalCp, appendFile } = (await vi.importActual(
450477
'node:fs/promises',

0 commit comments

Comments
 (0)