Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not show no index for client rendering bailout #59531

Merged
merged 12 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/src/client/on-recoverable-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../shared/lib/lazy-dynamic/no-ssr-error'
import { isBailoutCSRError } from '../shared/lib/lazy-dynamic/no-ssr-error'

export default function onRecoverableError(err: any) {
// Using default react onRecoverableError
Expand All @@ -13,7 +13,7 @@ export default function onRecoverableError(err: any) {
}

// Skip certain custom errors which are not expected to be reported on client
if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) return
if (isBailoutCSRError(err)) return

defaultOnRecoverableError(err)
}
4 changes: 2 additions & 2 deletions packages/next/src/export/helpers/is-dynamic-usage-error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DYNAMIC_ERROR_CODE } from '../../client/components/hooks-server-context'
import { isNotFoundError } from '../../client/components/not-found'
import { isRedirectError } from '../../client/components/redirect'
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../../shared/lib/lazy-dynamic/no-ssr-error'
import { isBailoutCSRError } from '../../shared/lib/lazy-dynamic/no-ssr-error'

export const isDynamicUsageError = (err: any) =>
err.digest === DYNAMIC_ERROR_CODE ||
isNotFoundError(err) ||
err.digest === NEXT_DYNAMIC_NO_SSR_CODE ||
isBailoutCSRError(err) ||
isRedirectError(err)
6 changes: 3 additions & 3 deletions packages/next/src/export/routes/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
NEXT_DATA_SUFFIX,
SERVER_PROPS_EXPORT_ERROR,
} from '../../lib/constants'
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../../shared/lib/lazy-dynamic/no-ssr-error'
import { isBailoutCSRError } from '../../shared/lib/lazy-dynamic/no-ssr-error'
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import { FileType, fileExists } from '../../lib/file-exists'
import { lazyRenderPagesPage } from '../../server/future/route-modules/pages/module.render'
Expand Down Expand Up @@ -106,7 +106,7 @@ export async function exportPages(
renderOpts
)
} catch (err: any) {
if (err.digest !== NEXT_DYNAMIC_NO_SSR_CODE) {
if (!isBailoutCSRError(err)) {
throw err
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export async function exportPages(
renderOpts
)
} catch (err: any) {
if (err.digest !== NEXT_DYNAMIC_NO_SSR_CODE) {
if (!isBailoutCSRError(err)) {
throw err
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import { parseAndValidateFlightRouterState } from './parse-and-validate-flight-r
import { validateURL } from './validate-url'
import { createFlightRouterStateFromLoaderTree } from './create-flight-router-state-from-loader-tree'
import { handleAction } from './action-handler'
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../../shared/lib/lazy-dynamic/no-ssr-error'
import { isBailoutCSRError } from '../../shared/lib/lazy-dynamic/no-ssr-error'
import { warn, error } from '../../build/output/log'
import { appendMutableCookies } from '../web/spec-extension/adapters/request-cookies'
import { createServerInsertedHTML } from './server-inserted-html'
Expand Down Expand Up @@ -863,7 +863,8 @@ async function renderToHTMLOrFlightImpl(
throw err
}

if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) {
const isBailoutCSR = isBailoutCSRError(err)
if (isBailoutCSR) {
warn(
`Entire page ${pagePath} deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`,
pagePath
Expand Down Expand Up @@ -894,7 +895,7 @@ async function renderToHTMLOrFlightImpl(
}

const is404 = res.statusCode === 404
if (!is404 && !hasRedirectError) {
if (!is404 && !hasRedirectError && !isBailoutCSR) {
res.statusCode = 500
}

Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/shared/lib/lazy-dynamic/no-ssr-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export function throwWithNoSSR() {
;(error as any).digest = NEXT_DYNAMIC_NO_SSR_CODE
throw error
}

export function isBailoutCSRError(err: any) {
return err?.digest === NEXT_DYNAMIC_NO_SSR_CODE
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/hooks/app/hooks/use-pathname/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function Layout({ children }) {
return children
}

export const dynamic = 'force-dynamic'
2 changes: 2 additions & 0 deletions test/e2e/app-dir/hooks/app/hooks/use-pathname/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export default function Page() {
</>
)
}

export const dynamic = 'force-dynamic'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function Layout({ children }) {
return children
}

export const dynamic = 'force-dynamic'
16 changes: 16 additions & 0 deletions test/e2e/app-dir/hooks/app/hooks/use-search-params/static/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use client'

import { useSearchParams } from 'next/navigation'

export default function Page() {
const params = useSearchParams()

return (
<>
<p id="params-first">{params.get('first') ?? 'N/A'}</p>
<p id="params-second">{params.get('second') ?? 'N/A'}</p>
<p id="params-third">{params.get('third') ?? 'N/A'}</p>
<p id="params-not-real">{params.get('notReal') ?? 'N/A'}</p>
</>
)
}
2 changes: 0 additions & 2 deletions test/e2e/app-dir/hooks/app/layout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react'

export const revalidate = 0

export default function Layout({ children }) {
return (
<html>
Expand Down
18 changes: 16 additions & 2 deletions test/e2e/app-dir/hooks/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ createNextDescribe(
{
files: __dirname,
},
({ next, isNextDeploy }) => {
({ next, isNextStart, isNextDeploy }) => {
describe('from pages', () => {
it.each([
{ pathname: '/adapter-hooks/static' },
Expand Down Expand Up @@ -54,12 +54,26 @@ createNextDescribe(
describe('useSearchParams', () => {
it('should have the correct search params', async () => {
const $ = await next.render$(
'/hooks/use-search-params?first=value&second=other%20value&third'
'/hooks/use-search-params/dynamic?first=value&second=other%20value&third'
)
expect($('#params-first').text()).toBe('value')
expect($('#params-second').text()).toBe('other value')
expect($('#params-third').text()).toBe('')
expect($('#params-not-real').text()).toBe('N/A')

// dynamic page doesn't have bail out
expect($('html#__next_error__').length).toBe(0)
expect($('meta[content=noindex]').length).toBe(0)
})

it('should not contain noindex meta tag for static built page', async () => {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
const $ = await next.render$(
'/hooks/use-search-params/static?first=value'
)
// static built page will not have search params
expect($('#params-first').text()).toBe(isNextStart ? '' : 'value')
// should not have noindex meta tag as it's erroring on purpose, for nextjs internal use only
expect($('meta[content=noindex]').length).toBe(0)
})

// TODO-APP: correct this behavior when deployed
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/hooks/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
{
source: '/rewritten-use-search-params',
destination:
'/hooks/use-search-params?first=value&second=other%20value&third',
'/hooks/use-search-params/dynamic?first=value&second=other%20value&third',
},
{
source: '/rewritten-use-pathname',
Expand Down