-
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 internal route redirection with absolute urls outside basePath #64604
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f876385
Fix internal route redirection with absolute urls outside basePath
cfrank 0c31a33
Fix issue with redirect() duplicating basePath in absolute URLs
cfrank 0f2e79c
Merge branch 'canary' into fix-64413
ijjk cb7f7bf
Merge branch 'canary' into fix-64413
ijjk 59f705e
enable app-basepath for deploy tests
ijjk 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
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 |
---|---|---|
|
@@ -809,25 +809,61 @@ describe('app-dir action handling', () => { | |
}, 'Prefix: HELLO, WORLD') | ||
}) | ||
|
||
it('should handle redirect to a relative URL in a single pass', async () => { | ||
const browser = await next.browser('/client/edge') | ||
it.each(['relative', 'absolute'])( | ||
`should handle calls to redirect() with a %s URL in a single pass`, | ||
async (redirectType) => { | ||
const initialPagePath = '/client/redirects' | ||
const destinationPagePath = '/redirect-target' | ||
|
||
await waitFor(3000) | ||
const browser = await next.browser(initialPagePath) | ||
|
||
let requests = [] | ||
const requests: Request[] = [] | ||
const responses: Response[] = [] | ||
|
||
browser.on('request', (req: Request) => { | ||
requests.push(new URL(req.url()).pathname) | ||
}) | ||
browser.on('request', (req: Request) => { | ||
const url = req.url() | ||
|
||
await browser.elementByCss('#redirect').click() | ||
if ( | ||
url.includes(initialPagePath) || | ||
url.includes(destinationPagePath) | ||
) { | ||
Comment on lines
+826
to
+829
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These conditionals are here to avoid having to use Depending on the opinions of the core team we can either go with this pattern or revert back to the |
||
requests.push(req) | ||
} | ||
}) | ||
|
||
// no other requests should be made | ||
expect(requests).toEqual(['/client/edge']) | ||
}) | ||
browser.on('response', (res: Response) => { | ||
const url = res.url() | ||
|
||
it('should handle regular redirects', async () => { | ||
const browser = await next.browser('/client/edge') | ||
if ( | ||
url.includes(initialPagePath) || | ||
url.includes(destinationPagePath) | ||
) { | ||
responses.push(res) | ||
} | ||
}) | ||
|
||
await browser.elementById(`redirect-${redirectType}`).click() | ||
await check(() => browser.url(), `${next.url}${destinationPagePath}`) | ||
|
||
expect(await browser.waitForElementByCss('#redirected').text()).toBe( | ||
'redirected' | ||
) | ||
|
||
// no other requests should be made | ||
expect(requests).toHaveLength(1) | ||
expect(responses).toHaveLength(1) | ||
|
||
const request = requests[0] | ||
const response = responses[0] | ||
|
||
expect(request.url()).toEqual(`${next.url}${initialPagePath}`) | ||
expect(request.method()).toEqual('POST') | ||
expect(response.status()).toEqual(303) | ||
} | ||
) | ||
|
||
it('should handle calls to redirect() with external URLs', async () => { | ||
const browser = await next.browser('/client/redirects') | ||
|
||
await browser.elementByCss('#redirect-external').click() | ||
|
||
|
@@ -876,36 +912,57 @@ describe('app-dir action handling', () => { | |
await check(() => browser.elementByCss('#count').text(), '2') | ||
}) | ||
|
||
it('should handle redirect to a relative URL in a single pass', async () => { | ||
let responseCode: number | ||
const browser = await next.browser('/client', { | ||
beforePageLoad(page) { | ||
page.on('response', async (res: Response) => { | ||
const headers = await res.allHeaders() | ||
if (headers['x-action-redirect']) { | ||
responseCode = res.status() | ||
} | ||
}) | ||
}, | ||
}) | ||
it.each(['relative', 'absolute'])( | ||
`should handle calls to redirect() with a %s URL in a single pass`, | ||
async (redirectType) => { | ||
const initialPagePath = '/client/redirects' | ||
const destinationPagePath = '/redirect-target' | ||
|
||
await waitFor(3000) | ||
const browser = await next.browser(initialPagePath) | ||
|
||
let requests = [] | ||
const requests: Request[] = [] | ||
const responses: Response[] = [] | ||
|
||
browser.on('request', (req: Request) => { | ||
requests.push(new URL(req.url()).pathname) | ||
}) | ||
browser.on('request', (req: Request) => { | ||
const url = req.url() | ||
|
||
await browser.elementByCss('#redirect').click() | ||
if ( | ||
url.includes(initialPagePath) || | ||
url.includes(destinationPagePath) | ||
) { | ||
requests.push(req) | ||
} | ||
}) | ||
|
||
// no other requests should be made | ||
expect(requests).toEqual(['/client']) | ||
await check(() => responseCode, 303) | ||
}) | ||
browser.on('response', (res: Response) => { | ||
const url = res.url() | ||
|
||
if ( | ||
url.includes(initialPagePath) || | ||
url.includes(destinationPagePath) | ||
) { | ||
responses.push(res) | ||
} | ||
}) | ||
|
||
await browser.elementById(`redirect-${redirectType}`).click() | ||
await check(() => browser.url(), `${next.url}${destinationPagePath}`) | ||
|
||
// no other requests should be made | ||
expect(requests).toHaveLength(1) | ||
expect(responses).toHaveLength(1) | ||
|
||
const request = requests[0] | ||
const response = responses[0] | ||
|
||
expect(request.url()).toEqual(`${next.url}${initialPagePath}`) | ||
expect(request.method()).toEqual('POST') | ||
expect(response.status()).toEqual(303) | ||
} | ||
) | ||
|
||
it('should handle regular redirects', async () => { | ||
const browser = await next.browser('/client') | ||
it('should handle calls to redirect() with external URLs', async () => { | ||
const browser = await next.browser('/client/redirects') | ||
|
||
await browser.elementByCss('#redirect-external').click() | ||
|
||
|
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
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
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,40 @@ | ||
'use client' | ||
|
||
import { redirectAction } from '../actions' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<form> | ||
<button | ||
id="redirect-relative" | ||
formAction={() => redirectAction('/redirect-target')} | ||
> | ||
redirect relative | ||
</button> | ||
</form> | ||
<form> | ||
<button | ||
id="redirect-external" | ||
formAction={() => | ||
redirectAction( | ||
'https://next-data-api-endpoint.vercel.app/api/random?page' | ||
) | ||
} | ||
> | ||
redirect external | ||
</button> | ||
</form> | ||
<form> | ||
<button | ||
id="redirect-absolute" | ||
formAction={() => | ||
redirectAction(location.origin + '/redirect-target') | ||
} | ||
> | ||
redirect internal with domain | ||
</button> | ||
</form> | ||
</div> | ||
) | ||
} |
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,9 @@ | ||
'use server' | ||
|
||
import 'server-only' | ||
|
||
import { redirect } from 'next/navigation' | ||
|
||
export async function redirectAction(path) { | ||
redirect(path) | ||
} |
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,36 @@ | ||
'use client' | ||
|
||
import { redirectAction } from './action' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<form> | ||
<button | ||
id="redirect-relative" | ||
formAction={() => redirectAction('/another')} | ||
> | ||
redirect internal with relative path | ||
</button> | ||
</form> | ||
<form> | ||
<button | ||
id="redirect-absolute-internal" | ||
formAction={() => redirectAction(location.origin + '/base/another')} | ||
> | ||
redirect internal with domain (including basePath) | ||
</button> | ||
</form> | ||
<form> | ||
<button | ||
id="redirect-absolute-external" | ||
formAction={() => | ||
redirectAction(location.origin + '/outsideBasePath') | ||
} | ||
> | ||
redirect external with domain (excluding basePath) | ||
</button> | ||
</form> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.
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.
getAppRelativeRedirectUrl
is now responsible for including thebasePath
for both relative and absolute redirect urls.