-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix internal route redirection with absolute urls outside basePath
When performing a redirect() with an absolute path, action-handler attempts to detect whether the resource is hosted by NextJS. If we believe it is, we then attempt to stream it. Previously we were not accounting for basePath which caused absolute redirects to resources on the same host, but not underneath the basePath, to be resolved by NextJS. Since the resource is outside the basePath we resolve a 404 page which returns back as `text/x-component` and is thus streamed back to the client within the original POST request. This PR adds a check for the presence of the basePath within absolute redirect URLs. This fixes the above problem. Signed-off-by: Chris Frank <chris@cfrank.org>
- Loading branch information
Showing
11 changed files
with
327 additions
and
78 deletions.
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
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.