-
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.
Add handling for redirects from getStaticProps/getServerSideProps (#1…
…6642) Co-authored-by: Tim Neutkens <timneutkens@me.com>
- Loading branch information
1 parent
6935a93
commit bc80fb4
Showing
13 changed files
with
517 additions
and
9 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,13 @@ | ||
# Redirect During getStaticProps Prerendering | ||
|
||
#### Why This Error Occurred | ||
|
||
The `redirect` value was returned from `getStaticProps` during prerendering which is invalid. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Remove any paths that result in a redirect from being prerendered in `getStaticPaths` and enable `fallback: true` to handle redirecting for these pages. | ||
|
||
### Useful Links | ||
|
||
- [Data Fetching Documentation](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) |
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 @@ | ||
# Invalid Redirect getStaticProps/getServerSideProps | ||
|
||
#### Why This Error Occurred | ||
|
||
The `redirect` value returned from your `getStaticProps` or `getServerSideProps` function had invalid values. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Make sure you return the proper values for the `redirect` value. | ||
|
||
```js | ||
export const getStaticProps = ({ params }) => { | ||
if (params.slug === 'deleted-post') { | ||
return { | ||
redirect: { | ||
permanent: true // or false | ||
destination: '/some-location' | ||
} | ||
} | ||
} | ||
|
||
return { | ||
props: { | ||
// data | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [Data Fetching Documentation](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) |
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,3 @@ | ||
export default function NotFound() { | ||
return <p>oops not found</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Another() { | ||
return <p id="another">another 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Post(props) { | ||
const router = useRouter() | ||
|
||
if (typeof window !== 'undefined' && !window.initialHref) { | ||
window.initialHref = window.location.href | ||
} | ||
|
||
if (router.isFallback) return <p>Loading...</p> | ||
|
||
return ( | ||
<> | ||
<p id="gsp">getStaticProps</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
</> | ||
) | ||
} | ||
|
||
export const getStaticProps = ({ params }) => { | ||
if (params.post.startsWith('redir')) { | ||
let destination = '/404' | ||
|
||
if (params.post.includes('dest-')) { | ||
destination = params.post.split('dest-').pop().replace(/_/g, '/') | ||
} | ||
|
||
return { | ||
unstable_redirect: { | ||
destination, | ||
permanent: params.post.includes('permanent'), | ||
}, | ||
} | ||
} | ||
|
||
return { | ||
props: { | ||
params, | ||
}, | ||
} | ||
} | ||
|
||
export const getStaticPaths = () => { | ||
return { | ||
paths: ['first', 'second'].map((post) => ({ params: { post } })), | ||
fallback: true, | ||
} | ||
} |
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,31 @@ | ||
export default function Post(props) { | ||
return ( | ||
<> | ||
<p id="gssp">getServerSideProps</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
</> | ||
) | ||
} | ||
|
||
export const getServerSideProps = ({ params }) => { | ||
if (params.post.startsWith('redir')) { | ||
let destination = '/404' | ||
|
||
if (params.post.includes('dest-')) { | ||
destination = params.post.split('dest-').pop().replace(/_/g, '/') | ||
} | ||
|
||
return { | ||
unstable_redirect: { | ||
destination, | ||
permanent: params.post.includes('permanent'), | ||
}, | ||
} | ||
} | ||
|
||
return { | ||
props: { | ||
params, | ||
}, | ||
} | ||
} |
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 Index() { | ||
return <p id="index">Index Page</p> | ||
} |
Oops, something went wrong.