Skip to content

Commit

Permalink
Revert: "Generate prefetch RSC payload during build for SSR paths (#5…
Browse files Browse the repository at this point in the history
…4403)" (#56059)

Investigating problems this is causing where incorrect flight data is being generated (potentially not correctly bailing on non-static data) causing navigation issues. 

Reverts #54403
  • Loading branch information
ztanner authored Sep 27, 2023
1 parent 12c800e commit 4cfb0ac
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 194 deletions.
21 changes: 0 additions & 21 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ import { eventSwcPlugins } from '../telemetry/events/swc-plugins'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import {
ACTION,
NEXT_ROUTER_PREFETCH,
RSC,
RSC_CONTENT_TYPE_HEADER,
RSC_VARY_HEADER,
Expand Down Expand Up @@ -230,7 +229,6 @@ export type RoutesManifest = {
rsc: {
header: typeof RSC
varyHeader: typeof RSC_VARY_HEADER
prefetchHeader: typeof NEXT_ROUTER_PREFETCH
}
skipMiddlewareUrlNormalize?: boolean
caseSensitive?: boolean
Expand Down Expand Up @@ -799,7 +797,6 @@ export default async function build(
rsc: {
header: RSC,
varyHeader: RSC_VARY_HEADER,
prefetchHeader: NEXT_ROUTER_PREFETCH,
contentTypeHeader: RSC_CONTENT_TYPE_HEADER,
},
skipMiddlewareUrlNormalize: config.skipMiddlewareUrlNormalize,
Expand Down Expand Up @@ -1153,7 +1150,6 @@ export default async function build(
const additionalSsgPaths = new Map<string, Array<string>>()
const additionalSsgPathsEncoded = new Map<string, Array<string>>()
const appStaticPaths = new Map<string, Array<string>>()
const appPrefetchPaths = new Map<string, string>()
const appStaticPathsEncoded = new Map<string, Array<string>>()
const appNormalizedPaths = new Map<string, string>()
const appDynamicParamPaths = new Set<string>()
Expand Down Expand Up @@ -1653,14 +1649,6 @@ export default async function build(
appDynamicParamPaths.add(originalAppPath)
}
appDefaultConfigs.set(originalAppPath, appConfig)

if (
!isStatic &&
!isAppRouteRoute(originalAppPath) &&
!isDynamicRoute(originalAppPath)
) {
appPrefetchPaths.set(originalAppPath, page)
}
}
} else {
if (isEdgeRuntime(pageRuntime)) {
Expand Down Expand Up @@ -2561,15 +2549,6 @@ export default async function build(
})
})

for (const [originalAppPath, page] of appPrefetchPaths) {
defaultMap[page] = {
page: originalAppPath,
query: {},
_isAppDir: true,
_isAppPrefetch: true,
}
}

if (i18n) {
for (const page of [
...staticPages,
Expand Down
67 changes: 1 addition & 66 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import type { NextParsedUrlQuery } from '../../server/request-meta'

import fs from 'fs/promises'
import { MockedRequest, MockedResponse } from '../../server/lib/mock-request'
import {
RSC,
NEXT_URL,
NEXT_ROUTER_PREFETCH,
} from '../../client/components/app-router-headers'
import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'
import { NEXT_CACHE_TAGS_HEADER } from '../../lib/constants'
import { hasNextSupport } from '../../telemetry/ci-info'
Expand All @@ -24,41 +19,6 @@ const render: AppPageRender = (...args) => {
)
}

export async function generatePrefetchRsc(
req: MockedRequest,
path: string,
res: MockedResponse,
pathname: string,
query: NextParsedUrlQuery,
htmlFilepath: string,
renderOpts: RenderOpts
) {
req.headers[RSC.toLowerCase()] = '1'
req.headers[NEXT_URL.toLowerCase()] = path
req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()] = '1'

renderOpts.supportsDynamicHTML = true
delete renderOpts.isRevalidate

const prefetchRenderResult = await render(
req,
res,
pathname,
query,
renderOpts
)

prefetchRenderResult.pipe(res)
await res.hasStreamed

const prefetchRscData = Buffer.concat(res.buffers)

await fs.writeFile(
htmlFilepath.replace(/\.html$/, '.prefetch.rsc'),
prefetchRscData
)
}

export async function exportAppPage(
req: MockedRequest,
res: MockedResponse,
Expand All @@ -69,29 +29,14 @@ export async function exportAppPage(
renderOpts: RenderOpts,
htmlFilepath: string,
debugOutput: boolean,
isDynamicError: boolean,
isAppPrefetch: boolean
isDynamicError: boolean
): Promise<ExportPageResult> {
// If the page is `/_not-found`, then we should update the page to be `/404`.
if (page === '/_not-found') {
pathname = '/404'
}

try {
if (isAppPrefetch) {
await generatePrefetchRsc(
req,
path,
res,
pathname,
query,
htmlFilepath,
renderOpts
)

return { fromBuildExportRevalidate: 0 }
}

const result = await render(req, res, pathname, query, renderOpts)
const html = result.toUnchunkedString()
const { metadata } = result
Expand All @@ -105,16 +50,6 @@ export async function exportAppPage(
)
}

await generatePrefetchRsc(
req,
path,
res,
pathname,
query,
htmlFilepath,
renderOpts
)

const { staticBailoutInfo = {} } = metadata

if (revalidate === 0 && debugOutput && staticBailoutInfo?.description) {
Expand Down
6 changes: 1 addition & 5 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ async function exportPageImpl(input: ExportPageInput) {
// Check if this is an `app/` page.
_isAppDir: isAppDir = false,

// Check if this is an `app/` prefix request.
_isAppPrefetch: isAppPrefetch = false,

// Check if this should error when dynamic usage is detected.
_isDynamicError: isDynamicError = false,

Expand Down Expand Up @@ -309,8 +306,7 @@ async function exportPageImpl(input: ExportPageInput) {
renderOpts,
htmlFilepath,
debugOutput,
isDynamicError,
isAppPrefetch
isDynamicError
)
}

Expand Down
27 changes: 0 additions & 27 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ import {
FLIGHT_PARAMETERS,
NEXT_RSC_UNION_QUERY,
ACTION,
NEXT_ROUTER_PREFETCH,
RSC_CONTENT_TYPE_HEADER,
} from '../client/components/app-router-headers'
import {
MatchOptions,
Expand Down Expand Up @@ -2122,31 +2120,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} else if (
components.routeModule?.definition.kind === RouteKind.APP_PAGE
) {
const isAppPrefetch = req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()]

if (
isAppPrefetch &&
ssgCacheKey &&
process.env.NODE_ENV === 'production'
) {
try {
const prefetchRsc = await this.getPrefetchRsc(ssgCacheKey)

if (prefetchRsc) {
res.setHeader(
'cache-control',
'private, no-cache, no-store, max-age=0, must-revalidate'
)
res.setHeader('content-type', RSC_CONTENT_TYPE_HEADER)
res.body(prefetchRsc).send()
return null
}
} catch (_) {
// we fallback to invoking the function if prefetch
// data is not available
}
}

const module = components.routeModule as AppPageRouteModule

// Due to the way we pass data by mutating `renderOpts`, we can't extend the
Expand Down
18 changes: 0 additions & 18 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,9 @@ createNextDescribe(
'stale-cache-serving-edge/app-page/page.js',
'stale-cache-serving-edge/app-page/page_client-reference-manifest.js',
'stale-cache-serving-edge/route-handler/route.js',
'stale-cache-serving/app-page.prefetch.rsc',
'stale-cache-serving/app-page/page.js',
'stale-cache-serving/app-page/page_client-reference-manifest.js',
'stale-cache-serving/route-handler/route.js',
'custom.prefetch.rsc',
'force-cache/page.js',
'ssg-draft-mode.html',
'(new)/custom/page.js',
Expand All @@ -525,23 +523,17 @@ createNextDescribe(
'force-static/first.html',
'force-static/second.rsc',
'ssg-draft-mode/test.rsc',
'ssr-forced.prefetch.rsc',
'blog/seb/second-post.rsc',
'blog/tim/first-post.html',
'force-static/second.html',
'ssg-draft-mode/test.html',
'blog/seb/second-post.html',
'force-static.prefetch.rsc',
'ssg-draft-mode/test-2.rsc',
'blog/styfle/first-post.rsc',
'default-cache.prefetch.rsc',
'dynamic-error/[id]/page.js',
'response-url.prefetch.rsc',
'ssg-draft-mode/test-2.html',
'blog/styfle/first-post.html',
'blog/styfle/second-post.rsc',
'fetch-no-cache.prefetch.rsc',
'force-no-store.prefetch.rsc',
'force-static/[slug]/page.js',
'hooks/use-pathname/slug.rsc',
'hooks/use-search-params.rsc',
Expand All @@ -567,11 +559,9 @@ createNextDescribe(
'react-fetch-deduping-node/page.js',
'variable-revalidate/encoding.html',
'variable-revalidate/cookie/page.js',
'gen-params-dynamic/one.prefetch.rsc',
'ssg-draft-mode/[[...route]]/page.js',
'variable-revalidate/post-method.rsc',
'dynamic-no-gen-params/[slug]/page.js',
'ssr-auto/cache-no-store.prefetch.rsc',
'static-to-dynamic-error/[id]/page.js',
'variable-revalidate/encoding/page.js',
'variable-revalidate/no-store/page.js',
Expand All @@ -585,7 +575,6 @@ createNextDescribe(
'variable-revalidate/revalidate-3.html',
'force-dynamic-prerender/[slug]/page.js',
'gen-params-dynamic-revalidate/one.html',
'react-fetch-deduping-node.prefetch.rsc',
'ssr-auto/fetch-revalidate-zero/page.js',
'variable-revalidate/authorization.html',
'force-dynamic-no-prerender/[id]/page.js',
Expand All @@ -596,7 +585,6 @@ createNextDescribe(
'partial-gen-params/[lang]/[slug]/page.js',
'variable-revalidate/headers-instance.rsc',
'variable-revalidate/revalidate-3/page.js',
'force-dynamic-catch-all/slug.prefetch.rsc',
'hooks/use-search-params/force-static.html',
'hooks/use-search-params/with-suspense.rsc',
'route-handler/revalidate-360-isr/route.js',
Expand All @@ -606,10 +594,8 @@ createNextDescribe(
'variable-revalidate/headers-instance.html',
'hooks/use-search-params/with-suspense.html',
'route-handler-edge/revalidate-360/route.js',
'variable-revalidate/no-store.prefetch.rsc',
'variable-revalidate/revalidate-360-isr.rsc',
'variable-revalidate/revalidate-360/page.js',
'ssr-auto/fetch-revalidate-zero.prefetch.rsc',
'static-to-dynamic-error-forced/[id]/page.js',
'variable-config-revalidate/revalidate-3.rsc',
'variable-revalidate/revalidate-360-isr.html',
Expand All @@ -619,7 +605,6 @@ createNextDescribe(
'variable-config-revalidate/revalidate-3.html',
'variable-revalidate-edge/post-method/page.js',
'variable-revalidate/headers-instance/page.js',
'variable-revalidate/status-code.prefetch.rsc',
'force-cache/page_client-reference-manifest.js',
'hooks/use-search-params/with-suspense/page.js',
'variable-revalidate-edge/revalidate-3/page.js',
Expand All @@ -629,10 +614,8 @@ createNextDescribe(
'variable-revalidate/revalidate-360-isr/page.js',
'blog/[author]/page_client-reference-manifest.js',
'default-cache/page_client-reference-manifest.js',
'force-dynamic-prerender/frameworks.prefetch.rsc',
'variable-config-revalidate/revalidate-3/page.js',
'variable-revalidate/post-method-request/page.js',
'variable-revalidate/revalidate-360.prefetch.rsc',
'fetch-no-cache/page_client-reference-manifest.js',
'force-dynamic-catch-all/[slug]/[[...id]]/page.js',
'force-no-store/page_client-reference-manifest.js',
Expand Down Expand Up @@ -661,7 +644,6 @@ createNextDescribe(
'partial-gen-params-no-additional-lang/fr/second.html',
'partial-gen-params-no-additional-slug/en/second.html',
'partial-gen-params-no-additional-slug/fr/second.html',
'variable-revalidate/post-method-request.prefetch.rsc',
'variable-revalidate-edge/post-method-request/page.js',
'force-static/[slug]/page_client-reference-manifest.js',
'blog/[author]/[slug]/page_client-reference-manifest.js',
Expand Down
55 changes: 0 additions & 55 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { createNextDescribe } from 'e2e-utils'
import { check, getRedboxHeader, hasRedbox, waitFor } from 'next-test-utils'
import cheerio from 'cheerio'
import stripAnsi from 'strip-ansi'
import { BrowserInterface } from 'test/lib/browsers/base'
import { Request } from 'playwright-core'

createNextDescribe(
'app dir',
Expand All @@ -12,59 +10,6 @@ createNextDescribe(
},
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
if (isNextStart) {
it('should use RSC prefetch data from build', async () => {
expect(
await next.readFile('.next/server/app/linking.prefetch.rsc')
).toBeTruthy()
expect(
await next.readFile('.next/server/app/linking/about.prefetch.rsc')
).toContain('About loading...')
expect(
await next.readFile(
'.next/server/app/dashboard/deployments/breakdown.prefetch.rsc'
)
).toBeTruthy()
expect(
await next
.readFile(
'.next/server/app/dashboard/deployments/[id].prefetch.rsc'
)
.catch(() => false)
).toBeFalsy()

const outputStart = next.cliOutput.length
const browser: BrowserInterface = await next.browser('/')
const rscReqs = []

browser.on('request', (req: Request) => {
if (req.headers()['rsc']) {
rscReqs.push(req.url())
}
})

await browser.eval('window.location.href = "/linking"')

await check(async () => {
return rscReqs.length > 3 ? 'success' : JSON.stringify(rscReqs)
}, 'success')

const trimmedOutput = next.cliOutput.substring(outputStart)

expect(trimmedOutput).not.toContain(
'rendering dashboard/(custom)/deployments/breakdown'
)
expect(trimmedOutput).not.toContain(
'rendering /dashboard/deployments/[id]'
)
expect(trimmedOutput).not.toContain('rendering linking about page')

await browser.elementByCss('#breakdown').click()
await check(
() => next.cliOutput.substring(outputStart),
/rendering .*breakdown/
)
})

it('should have correct size in build output', async () => {
expect(next.cliOutput).toMatch(
/\/dashboard\/another.*? [^0]{1,} [\w]{1,}B/
Expand Down
Loading

0 comments on commit 4cfb0ac

Please sign in to comment.