From be6f72d3f3333ac57ea1e6f23977acb570630279 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 23 Jun 2024 13:24:19 +0200 Subject: [PATCH 1/4] Fix noindex is missing on static not-found page --- .../next/src/server/app-render/app-render.tsx | 32 ++++++++++++------- .../app-dir/not-found/default/app/foo/page.js | 3 ++ .../app-dir/not-found/default/app/layout.js | 7 ++++ .../app-dir/not-found/default/default.test.ts | 23 +++++++++++++ 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 test/e2e/app-dir/not-found/default/app/foo/page.js create mode 100644 test/e2e/app-dir/not-found/default/app/layout.js create mode 100644 test/e2e/app-dir/not-found/default/default.test.ts diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index 622f8c6cddf01..8dc1c9d86a891 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -298,6 +298,17 @@ function makeGetDynamicParamFromSegment( } } +function NonIndex({ ctx }: { ctx: AppRenderContext }) { + const is404Page = ctx.pagePath === '/404' + const isInvalidStatusCode = + typeof ctx.res.statusCode === 'number' && ctx.res.statusCode > 400 + + if (is404Page || isInvalidStatusCode) { + return + } + return null +} + // Handle Flight render request. This is only used when client-side navigating. E.g. when you `router.push('/dashboard')` or `router.reload()`. async function generateFlight( ctx: AppRenderContext, @@ -344,8 +355,11 @@ async function generateFlight( isFirst: true, // For flight, render metadata inside leaf page rscPayloadHead: ( - // Adding requestId as react key to make metadata remount for each render - + <> + + {/* Adding requestId as react key to make metadata remount for each render */} + + ), injectedCSS: new Set(), injectedJS: new Set(), @@ -493,10 +507,7 @@ async function ReactServerApp({ tree, ctx, asNotFound }: ReactServerAppProps) { couldBeIntercepted={couldBeIntercepted} initialHead={ <> - {typeof ctx.res.statusCode === 'number' && - ctx.res.statusCode > 400 && ( - - )} + {/* Adding requestId as react key to make metadata remount for each render */} @@ -532,9 +543,10 @@ async function ReactServerError({ }, requestStore: { url }, requestId, - res, } = ctx + console.log('ReactServerError', ctx.pagePath, errorType) + const [MetadataTree] = createMetadataComponents({ tree, metadataContext: createMetadataContext(url.pathname, ctx.renderOpts), @@ -547,11 +559,9 @@ async function ReactServerError({ const head = ( <> + {/* Adding requestId as react key to make metadata remount for each render */} - {typeof res.statusCode === 'number' && res.statusCode >= 400 && ( - - )} {process.env.NODE_ENV === 'development' && ( )} @@ -1269,7 +1279,7 @@ async function renderToHTMLOrFlightImpl( setHeader('Location', redirectUrl) } - const is404 = res.statusCode === 404 + const is404 = ctx.res.statusCode === 404 if (!is404 && !hasRedirectError && !shouldBailoutToCSR) { res.statusCode = 500 } diff --git a/test/e2e/app-dir/not-found/default/app/foo/page.js b/test/e2e/app-dir/not-found/default/app/foo/page.js new file mode 100644 index 0000000000000..9c373d74ea69a --- /dev/null +++ b/test/e2e/app-dir/not-found/default/app/foo/page.js @@ -0,0 +1,3 @@ +export default function Page() { + return

Foo

+} diff --git a/test/e2e/app-dir/not-found/default/app/layout.js b/test/e2e/app-dir/not-found/default/app/layout.js new file mode 100644 index 0000000000000..750eb927b1980 --- /dev/null +++ b/test/e2e/app-dir/not-found/default/app/layout.js @@ -0,0 +1,7 @@ +export default function Layout({ children }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/app-dir/not-found/default/default.test.ts b/test/e2e/app-dir/not-found/default/default.test.ts new file mode 100644 index 0000000000000..fd7b1606e4071 --- /dev/null +++ b/test/e2e/app-dir/not-found/default/default.test.ts @@ -0,0 +1,23 @@ +import { nextTestSetup } from 'e2e-utils' + +describe('app dir - not-found - default', () => { + const { next, isNextStart } = nextTestSetup({ + files: __dirname, + skipDeployment: true, + }) + + it('should has noindex in the head html', async () => { + const $ = await next.render$('/does-not-exist') + expect(await $('meta[name="robots"]').attr('content')).toBe('noindex') + }) + + if (isNextStart) { + it('should contain noindex contain in the page', async () => { + const html = await next.readFile('.next/server/app/_not-found.html') + const rsc = await next.readFile('.next/server/app/_not-found.rsc') + + expect(html).toContain('noindex') + expect(rsc).toContain('noindex') + }) + } +}) From d01f04c752d93a5a4ff984568be8950f2e3fa86c Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 23 Jun 2024 13:26:42 +0200 Subject: [PATCH 2/4] remove deploy tests filter --- test/deploy-tests-manifest.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/deploy-tests-manifest.json b/test/deploy-tests-manifest.json index 83464f7180faa..2e109bb77b18e 100644 --- a/test/deploy-tests-manifest.json +++ b/test/deploy-tests-manifest.json @@ -13,11 +13,6 @@ "app dir - metadata react cache should have same title and page value when navigating" ] }, - "test/e2e/app-dir/metadata-navigation/metadata-navigation.test.ts": { - "failed": [ - "app dir - metadata navigation navigation should render root not-found with default metadata" - ] - }, "test/e2e/middleware-rewrites/test/index.test.ts": { "failed": ["Middleware Rewrite should handle catch-all rewrite correctly"] } From b5b2b474b157fce5cc6eedabdaf820829d74d44b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 23 Jun 2024 13:38:43 +0200 Subject: [PATCH 3/4] drop logs --- packages/next/src/server/app-render/app-render.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index 8dc1c9d86a891..cd514d34b03f0 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -545,8 +545,6 @@ async function ReactServerError({ requestId, } = ctx - console.log('ReactServerError', ctx.pagePath, errorType) - const [MetadataTree] = createMetadataComponents({ tree, metadataContext: createMetadataContext(url.pathname, ctx.renderOpts), From 7a1a0a79870d96fbe4df98e7458757af55bf91df Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 23 Jun 2024 13:48:54 +0200 Subject: [PATCH 4/4] cover ppr test --- test/e2e/app-dir/not-found/default/default.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/e2e/app-dir/not-found/default/default.test.ts b/test/e2e/app-dir/not-found/default/default.test.ts index fd7b1606e4071..b7e936d5a9bf5 100644 --- a/test/e2e/app-dir/not-found/default/default.test.ts +++ b/test/e2e/app-dir/not-found/default/default.test.ts @@ -1,5 +1,7 @@ import { nextTestSetup } from 'e2e-utils' +const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true' + describe('app dir - not-found - default', () => { const { next, isNextStart } = nextTestSetup({ files: __dirname, @@ -14,7 +16,9 @@ describe('app dir - not-found - default', () => { if (isNextStart) { it('should contain noindex contain in the page', async () => { const html = await next.readFile('.next/server/app/_not-found.html') - const rsc = await next.readFile('.next/server/app/_not-found.rsc') + const rsc = await next.readFile( + `.next/server/app/_not-found.${isPPREnabled ? 'prefetch.' : ''}rsc` + ) expect(html).toContain('noindex') expect(rsc).toContain('noindex')