-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure basePath behavior with GS(S)P redirect (#18988)
This ensures we match the `basePath` handling for redirects in `next.config.js` with redirects from `getStaticProps` and `getServerSideProps` and also adds a separate test suite to ensure GS(S)P redirects with `basePath` work correctly Fixes: #18984 Closes: #18892
- Loading branch information
Showing
13 changed files
with
791 additions
and
22 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
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 @@ | ||
module.exports = { | ||
basePath: '/docs', | ||
} |
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> | ||
} |
76 changes: 76 additions & 0 deletions
76
test/integration/gssp-redirect-base-path/pages/gsp-blog/[post].js
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,76 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Post(props) { | ||
const router = useRouter() | ||
|
||
if (typeof window === 'undefined') { | ||
if (router.query.post?.startsWith('redir')) { | ||
console.log(router) | ||
throw new Error('render should not occur for redirect') | ||
} | ||
} | ||
|
||
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, '/') | ||
} | ||
|
||
let permanent = undefined | ||
let statusCode = undefined | ||
|
||
if (params.post.includes('statusCode-')) { | ||
permanent = parseInt( | ||
params.post.split('statusCode-').pop().split('-').shift(), | ||
10 | ||
) | ||
} | ||
|
||
if (params.post.includes('permanent')) { | ||
permanent = true | ||
} else if (!statusCode) { | ||
permanent = false | ||
} | ||
|
||
const redirect = { | ||
destination, | ||
permanent, | ||
statusCode, | ||
} | ||
|
||
if (params.post.includes('no-basepath-')) { | ||
redirect.basePath = false | ||
} | ||
|
||
return { redirect } | ||
} | ||
|
||
return { | ||
props: { | ||
params, | ||
}, | ||
} | ||
} | ||
|
||
export const getStaticPaths = () => { | ||
return { | ||
paths: ['first', 'second'].map((post) => ({ params: { post } })), | ||
fallback: true, | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
test/integration/gssp-redirect-base-path/pages/gssp-blog/[post].js
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,63 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Post(props) { | ||
const router = useRouter() | ||
|
||
if (typeof window === 'undefined') { | ||
if (router.query.post?.startsWith('redir')) { | ||
console.log(router) | ||
throw new Error('render should not occur for redirect') | ||
} | ||
} | ||
|
||
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, '/') | ||
} | ||
|
||
let permanent = undefined | ||
let statusCode = undefined | ||
|
||
if (params.post.includes('statusCode-')) { | ||
statusCode = parseInt( | ||
params.post.split('statusCode-').pop().split('-').shift(), | ||
10 | ||
) | ||
} | ||
|
||
if (params.post.includes('permanent')) { | ||
permanent = true | ||
} else if (!statusCode) { | ||
permanent = false | ||
} | ||
|
||
const redirect = { | ||
destination, | ||
permanent, | ||
statusCode, | ||
} | ||
|
||
if (params.post.includes('no-basepath-')) { | ||
redirect.basePath = false | ||
} | ||
|
||
return { redirect } | ||
} | ||
|
||
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,7 @@ | ||
export default function Index() { | ||
if (typeof window !== 'undefined' && !window.initialHref) { | ||
window.initialHref = window.location.href | ||
} | ||
|
||
return <p id="index">Index Page</p> | ||
} |
Oops, something went wrong.