-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix server action edge redirect with middleware rewrite #67148
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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`) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aligning with redirect logic here using
!isEdgeRendering
.I think we might able to delete these re-construction here later as web/adapater should always for edge runtime? but this time keep the fix simple cc @ijjk @ztanner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is only necessary because we use this adapter in
next.js/packages/next/src/build/templates/middleware.ts
Line 24 in f9d12d1
So I guess we just abstracted too early and
templates/middleware.ts
should use a different adapter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked the
__BUILD_MANIFEST
is not avialble in middleware, I guess that's what it was trying to do there. Probably can give a different flag later to identify which is middleware and which are other normal edge functions