Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: add test case for next/navigation#redirect
  • Loading branch information
pieh committed Dec 17, 2025
commit 81dd0cedfef1987fda45ef921eb3b168cc2f0586
11 changes: 11 additions & 0 deletions tests/fixtures/simple/app/app-redirect/[slug]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { redirect } from 'next/navigation'

const Page = async () => {
return redirect(`/app-redirect/dest`)
}

export const generateStaticParams = async () => {
return [{ slug: 'prerendered' }]
}

export default Page
9 changes: 9 additions & 0 deletions tests/fixtures/simple/app/app-redirect/dest/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function Page() {
return (
<main>
<h1>Hello next/navigation#redirect target</h1>
</main>
)
}

export const dynamic = 'force-static'
27 changes: 27 additions & 0 deletions tests/integration/simple-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ test<FixtureTestContext>('Test that the simple next app is working', async (ctx)
shouldHaveAppRouterNotFoundInPrerenderManifest() ? '/_not-found' : undefined,
'/api/cached-permanent',
'/api/cached-revalidate',
'/app-redirect/dest',
'/app-redirect/prerendered',
'/config-redirect',
'/config-redirect/dest',
'/config-rewrite',
Expand Down Expand Up @@ -445,6 +447,31 @@ test.skipIf(hasDefaultTurbopackBuilds())<FixtureTestContext>(
},
)

// 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)
// 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
test.skipIf(nextVersionSatisfies('<15.0.0'))<FixtureTestContext>(
`app router page that uses next/navigation#redirect works when page is prerendered`,
async (ctx) => {
await createFixture('simple', ctx)
await runPlugin(ctx)

const response = await invokeFunction(ctx, { url: `/app-redirect/prerendered` })

expect(response.statusCode).toBe(307)
expect(response.headers['location']).toBe('/app-redirect/dest')
},
)

test<FixtureTestContext>(`app router page that uses next/navigation#redirect works when page is NOT prerendered`, async (ctx) => {
await createFixture('simple', ctx)
await runPlugin(ctx)

const response = await invokeFunction(ctx, { url: `/app-redirect/non-prerendered` })

expect(response.statusCode).toBe(307)
expect(response.headers['location']).toBe('/app-redirect/dest')
})

describe('next patching', async () => {
const { cp: originalCp, appendFile } = (await vi.importActual(
'node:fs/promises',
Expand Down
Loading