Skip to content

Commit

Permalink
refactor: use render results for fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Jul 31, 2024
1 parent d466056 commit 26679d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
17 changes: 9 additions & 8 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export default abstract class Server<
req: ServerRequest,
parsedUrl: NextUrlWithParsedQuery
): void
protected abstract getFallback(page: string): Promise<string>
protected abstract getFallback(page: string): Promise<RenderResult>
protected abstract hasPage(pathname: string): Promise<boolean>

protected abstract sendRenderResult(
Expand Down Expand Up @@ -2687,9 +2687,9 @@ export default abstract class Server<
value: {
kind: 'PAGE',
html: result,
pageData: metadata.pageData ?? metadata.flightData,
pageData: metadata.pageData,
headers,
status: isAppPath ? res.statusCode : undefined,
status: undefined,
} satisfies CachedPageValue,
revalidate: metadata.revalidate,
}
Expand Down Expand Up @@ -2795,18 +2795,19 @@ export default abstract class Server<
if (!isNextDataRequest) {
// Production already emitted the fallback as static HTML.
if (isProduction) {
const html = await this.getFallback(
const result = await this.getFallback(
locale ? `/${locale}${pathname}` : pathname
)

return {
value: {
kind: 'PAGE',
html: RenderResult.fromStatic(html),
html: result,
pageData: Object.create(null),
headers: result.metadata.headers,
status: undefined,
headers: undefined,
pageData: {},
},
} satisfies CachedPageValue,
revalidate: result.metadata.revalidate,
}
}
// We need to generate the fallback on-demand for development.
Expand Down
13 changes: 7 additions & 6 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
MiddlewareNotFoundError,
} from '../shared/lib/utils'
import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin'
import type RenderResult from './render-result'
import RenderResult from './render-result'
import type { FetchEventResult } from './web/types'
import type { PrerenderManifest } from '../build'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
Expand Down Expand Up @@ -805,13 +805,14 @@ export default class NextNodeServer extends BaseServer<
) as NextFontManifest
}

protected getFallback(page: string): Promise<string> {
protected async getFallback(page: string): Promise<RenderResult> {
page = normalizePagePath(page)
const cacheFs = this.getCacheFilesystem()
return cacheFs.readFile(
join(this.serverDistDir, 'pages', `${page}.html`),
'utf8'
)

// For Pages, we just have to return the fallback HTML.
const filename = join(this.serverDistDir, 'pages', page)
const html = await cacheFs.readFile(`${filename}.html`, 'utf8')
return RenderResult.fromStatic(html)
}

protected handleNextImageRequest: NodeRouteHandler = async (
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WebNextRequest, WebNextResponse } from './base-http/web'
import type RenderResult from './render-result'
import RenderResult from './render-result'
import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta'
import type { Params } from '../client/components/params'
import type { LoadComponentsReturnType } from './load-components'
Expand Down Expand Up @@ -358,7 +358,7 @@ export default class NextWebServer extends BaseServer<
}

protected async getFallback() {
return ''
return RenderResult.fromStatic('')
}

protected getFontManifest() {
Expand Down

0 comments on commit 26679d8

Please sign in to comment.