-
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.
# Fixing a bug ## What? Changed the formation of the redirect url to the correct way. _It’s interesting that you had tests for this, but since only the main page was tested, it wasn’t caught._ Expanded the tests, made a separate method to assign location, and described it. ```js new URL('./relative', 'https://example.com/subdir').href // 'https://example.com/relative' new URL('./relative', 'https://example.com/subdir/').href // 'https://example.com/subdir/relative' ``` Fixes #71906 [#65893, #67966] --------- Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { addBasePath } from './add-base-path' | ||
|
||
/** | ||
* Function to correctly assign location to URL | ||
* | ||
* The method will add basePath, and will also correctly add location (including if it is a relative path) | ||
* @param location Location that should be added to the url | ||
* @param url Base URL to which the location should be assigned | ||
*/ | ||
export function assignLocation(location: string, url: URL): URL { | ||
if (location.startsWith('.')) { | ||
const urlBase = url.origin + url.pathname | ||
return new URL( | ||
// In order for a relative path to be added to the current url correctly, the current url must end with a slash | ||
// new URL('./relative', 'https://example.com/subdir').href -> 'https://example.com/relative' | ||
// new URL('./relative', 'https://example.com/subdir/').href -> 'https://example.com/subdir/relative' | ||
(urlBase.endsWith('/') ? urlBase : urlBase + '/') + location | ||
) | ||
} | ||
|
||
return new URL(addBasePath(location), url.href) | ||
} |
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
32 changes: 32 additions & 0 deletions
32
test/e2e/app-dir/server-actions-relative-redirect/app/subdir/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,32 @@ | ||
'use client' | ||
|
||
import { startTransition } from 'react' | ||
import { absoluteRedirect, relativeRedirect } from '../actions' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p>hello subdir page</p> | ||
<button | ||
onClick={async () => { | ||
startTransition(async () => { | ||
await relativeRedirect() | ||
}) | ||
}} | ||
id="relative-subdir-redirect" | ||
> | ||
relative redirect | ||
</button> | ||
<button | ||
onClick={async () => { | ||
startTransition(async () => { | ||
await absoluteRedirect() | ||
}) | ||
}} | ||
id="absolute-subdir-redirect" | ||
> | ||
absolute redirect | ||
</button> | ||
</> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/server-actions-relative-redirect/app/subdir/subpage/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 <p id="page-loaded">hello subdir nested page</p> | ||
} |
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