-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix server redirects to work properly with RSC responses
Fixes #947
- Loading branch information
Showing
8 changed files
with
40 additions
and
18 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
examples/template-hydrogen-default/src/routes/products/[handle].server.jsx
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
3 changes: 1 addition & 2 deletions
3
examples/template-hydrogen-default/src/routes/redirect.server.jsx
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export default function Redirect({response}) { | ||
response.redirect('/products/snowboard'); | ||
return <div>This page is redirected</div>; | ||
return response.redirect('/products/snowboard'); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/hydrogen/src/foundation/Redirect/Redirect.client.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,20 @@ | ||
import {useEffect} from 'react'; | ||
import {useNavigate} from '../../client'; | ||
|
||
type RedirectProps = { | ||
to: string; | ||
}; | ||
|
||
export default function Redirect({to}: RedirectProps) { | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (to.startsWith('http') || to.startsWith('https')) { | ||
window.location.href = to; | ||
} else { | ||
navigate(to); | ||
} | ||
}, []); | ||
|
||
return null; | ||
} |
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
8 changes: 1 addition & 7 deletions
8
packages/playground/server-components/src/routes/redirected.server.jsx
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 |
---|---|---|
@@ -1,9 +1,3 @@ | ||
export default function Redirected({response}) { | ||
response.redirect('/about'); | ||
|
||
return ( | ||
<div> | ||
<h1>This page has been moved to /about</h1> | ||
</div> | ||
); | ||
return response.redirect('/about'); | ||
} |
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