Unverified Commit 9639fe70 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

Ensure we don't poll page in development when notFound: true is returned (#34352)

Fixes: #34342

Visiting the following page will call gSSP indefinitely in a loop and logs errors from `on-demand-entries-client`:
```js
const Home = () => null
export default Home
        
export function getServerSideProps() {
  console.log("gssp called")
  return { notFound: true }
}
```

We should not keep fetching the page if it returns 404 as  it can introduce unnecessary data requests.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 7e93a89b
...@@ -11,7 +11,17 @@ export default async (page) => { ...@@ -11,7 +11,17 @@ export default async (page) => {
} else { } else {
Router.ready(() => { Router.ready(() => {
setInterval(() => { setInterval(() => {
sendMessage(JSON.stringify({ event: 'ping', page: Router.pathname })) // when notFound: true is returned we should use the notFoundPage
// as the Router.pathname will point to the 404 page but we want
// to ping the source page that returned notFound: true instead
const notFoundSrcPage = self.__NEXT_DATA__.notFoundSrcPage
const pathname =
(Router.pathname === '/404' || Router.pathname === '/_error') &&
notFoundSrcPage
? notFoundSrcPage
: Router.pathname
sendMessage(JSON.stringify({ event: 'ping', page: pathname }))
}, 2500) }, 2500)
}) })
} }
......
...@@ -1550,6 +1550,9 @@ export default abstract class Server { ...@@ -1550,6 +1550,9 @@ export default abstract class Server {
res.body('{"notFound":true}').send() res.body('{"notFound":true}').send()
return null return null
} else { } else {
if (this.renderOpts.dev) {
query.__nextNotFoundSrcPage = pathname
}
await this.render404( await this.render404(
req, req,
res, res,
......
...@@ -528,9 +528,11 @@ export async function renderToHTML( ...@@ -528,9 +528,11 @@ export async function renderToHTML(
const headTags = (...args: any) => callMiddleware('headTags', args) const headTags = (...args: any) => callMiddleware('headTags', args)
const isFallback = !!query.__nextFallback const isFallback = !!query.__nextFallback
const notFoundSrcPage = query.__nextNotFoundSrcPage
delete query.__nextFallback delete query.__nextFallback
delete query.__nextLocale delete query.__nextLocale
delete query.__nextDefaultLocale delete query.__nextDefaultLocale
delete query.__nextIsNotFound
const isSSG = !!getStaticProps const isSSG = !!getStaticProps
const isBuildTimeSSG = isSSG && renderOpts.nextExport const isBuildTimeSSG = isSSG && renderOpts.nextExport
...@@ -1374,6 +1376,7 @@ export async function renderToHTML( ...@@ -1374,6 +1376,7 @@ export async function renderToHTML(
defaultLocale, defaultLocale,
domainLocales, domainLocales,
isPreview: isPreview === true ? true : undefined, isPreview: isPreview === true ? true : undefined,
notFoundSrcPage: notFoundSrcPage && dev ? notFoundSrcPage : undefined,
}, },
buildManifest: filteredBuildManifest, buildManifest: filteredBuildManifest,
docComponentsRendered, docComponentsRendered,
......
...@@ -53,6 +53,7 @@ export function addRequestMeta<K extends keyof RequestMeta>( ...@@ -53,6 +53,7 @@ export function addRequestMeta<K extends keyof RequestMeta>(
} }
type NextQueryMetadata = { type NextQueryMetadata = {
__nextNotFoundSrcPage?: string
__nextDefaultLocale?: string __nextDefaultLocale?: string
__nextFallback?: 'true' __nextFallback?: 'true'
__nextLocale?: string __nextLocale?: string
......
...@@ -107,6 +107,7 @@ export type NEXT_DATA = { ...@@ -107,6 +107,7 @@ export type NEXT_DATA = {
domainLocales?: DomainLocale[] domainLocales?: DomainLocale[]
scriptLoader?: any[] scriptLoader?: any[]
isPreview?: boolean isPreview?: boolean
notFoundSrcPage?: string
rsc?: boolean rsc?: boolean
} }
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { waitFor } from 'next-test-utils'
import webdriver from 'next-webdriver'
describe('getServerSideProps returns notFound: true', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
const Home = () => null
export default Home
export function getServerSideProps() {
console.log("gssp called")
return { notFound: true }
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
it('should not poll indefinitely', async () => {
const browser = await webdriver(next.appPort, '/')
await waitFor(3000)
await browser.close()
const logOccurrences = next.cliOutput.split('gssp called').length - 1
expect(logOccurrences).toBe(1)
})
})
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