Unverified Commit f7915775 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure redirect from GS(S)P doesn't render (#18749)

This ensures we don't render the page un-necessarily when a redirect is returned from `getStaticProps` or `getServerSideProps`.

Possibly related: https://github.com/vercel/next.js/issues/18735
Closes: https://github.com/vercel/next.js/issues/18747
parent b87b2d46
...@@ -862,9 +862,9 @@ export async function renderToHTML( ...@@ -862,9 +862,9 @@ export async function renderToHTML(
) )
} }
// We only need to do this if we want to support calling // Avoid rendering page un-necessarily for getServerSideProps data request
// _app's getInitialProps for getServerSideProps if not this can be removed // and getServerSideProps/getStaticProps redirects
if (isDataReq && !isSSG) return props if (isDataReq && (!isSSG || props.pageProps.__N_REDIRECT)) return props
// We don't call getStaticProps or getServerSideProps while generating // We don't call getStaticProps or getServerSideProps while generating
// the fallback so make sure to set pageProps to an empty object // the fallback so make sure to set pageProps to an empty object
......
...@@ -3,6 +3,13 @@ import { useRouter } from 'next/router' ...@@ -3,6 +3,13 @@ import { useRouter } from 'next/router'
export default function Post(props) { export default function Post(props) {
const router = useRouter() 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) { if (typeof window !== 'undefined' && !window.initialHref) {
window.initialHref = window.location.href window.initialHref = window.location.href
} }
......
import { useRouter } from 'next/router'
export default function Post(props) { 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 ( return (
<> <>
<p id="gssp">getServerSideProps</p> <p id="gssp">getServerSideProps</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment