-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix server action edge redirect with middleware rewrite (#67148)
### What Add isEdgeRendering check condition for rewrite logic in edge adpator Fixes #66837 Closes NEXT-3545 ### Why From headers `x-middleware-rewrite`, it's still relative url `/rewrite`, reconstructing the url will lead to crash as it doesn't contain host.
- Loading branch information
Showing
9 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/app/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use server' | ||
|
||
import { redirect } from 'next/navigation' | ||
|
||
export async function relativeRedirect() { | ||
return redirect('./subpage') | ||
} | ||
|
||
export async function absoluteRedirect() { | ||
return redirect('/subpage') | ||
} |
11 changes: 11 additions & 0 deletions
11
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default function RootLayout({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactNode | ||
}>) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/app/redirect/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <div id="redirected">Redirected</div> | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/app/server-action/_action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use server' | ||
|
||
import { redirect } from 'next/navigation' | ||
|
||
export const redirectAction = async () => { | ||
redirect('/redirect') | ||
} |
13 changes: 13 additions & 0 deletions
13
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/app/server-action/edge/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client' | ||
|
||
import { redirectAction } from '../_action' | ||
|
||
export default function Page() { | ||
return ( | ||
<form action={redirectAction}> | ||
<button type="submit">Submit</button> | ||
</form> | ||
) | ||
} | ||
|
||
export const runtime = 'edge' |
13 changes: 13 additions & 0 deletions
13
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/app/server-action/node/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client' | ||
|
||
import { redirectAction } from '../_action' | ||
|
||
export default function Page() { | ||
return ( | ||
<form action={redirectAction}> | ||
<button type="submit">Submit</button> | ||
</form> | ||
) | ||
} | ||
|
||
export const runtime = 'nodejs' |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/server-actions-redirect-middleware-rewrite/middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export default function middleware(request: NextRequest) { | ||
return NextResponse.rewrite(request.url) | ||
} |
33 changes: 33 additions & 0 deletions
33
...er-actions-redirect-middleware-rewrite/server-actions-redirect-middleware-rewrite.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry } from 'next-test-utils' | ||
|
||
describe('app-dir - server-actions-redirect-middleware-rewrite.test', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should redirect correctly in nodejs runtime with middleware rewrite', async () => { | ||
const browser = await next.browser('/server-action/node') | ||
await browser.waitForElementByCss('button').click() | ||
|
||
await retry(async () => { | ||
expect(await browser.waitForElementByCss('#redirected').text()).toBe( | ||
'Redirected' | ||
) | ||
}) | ||
expect(await browser.url()).toBe(`${next.url}/redirect`) | ||
}) | ||
|
||
it('should redirect correctly in edge runtime with middleware rewrite', async () => { | ||
const browser = await next.browser('/server-action/edge') | ||
await browser.waitForElementByCss('button').click() | ||
|
||
await retry(async () => { | ||
expect(await browser.waitForElementByCss('#redirected').text()).toBe( | ||
'Redirected' | ||
) | ||
|
||
expect(await browser.url()).toBe(`${next.url}/redirect`) | ||
}) | ||
}) | ||
}) |