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

Ensure that pages manifest contains pages of both runtimes #35243

Merged
merged 3 commits into from Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/next/build/webpack-config.ts
Expand Up @@ -1439,8 +1439,11 @@ export default async function getBaseWebpackConfig(
}),
((isServerless && isServer) || isEdgeRuntime) && new ServerlessPlugin(),
isServer &&
!isEdgeRuntime &&
new PagesManifestPlugin({ serverless: isLikeServerless, dev }),
new PagesManifestPlugin({
serverless: isLikeServerless,
dev,
isEdgeRuntime,
}),
// MiddlewarePlugin should be after DefinePlugin so NEXT_PUBLIC_*
// replacement is done before its process.env.* handling
(!isServer || isEdgeRuntime) &&
Expand Down
41 changes: 37 additions & 4 deletions packages/next/build/webpack/plugins/pages-manifest-plugin.ts
Expand Up @@ -4,16 +4,29 @@ import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint'

export type PagesManifest = { [page: string]: string }

let edgeServerPages = {}
let nodeServerPages = {}

// This plugin creates a pages-manifest.json from page entrypoints.
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
// It's also used by next export to provide defaultPathMap
export default class PagesManifestPlugin implements webpack.Plugin {
serverless: boolean
dev: boolean
isEdgeRuntime: boolean

constructor({ serverless, dev }: { serverless: boolean; dev: boolean }) {
constructor({
serverless,
dev,
isEdgeRuntime,
}: {
serverless: boolean
dev: boolean
isEdgeRuntime: boolean
}) {
this.serverless = serverless
this.dev = dev
this.isEdgeRuntime = isEdgeRuntime
}

createAssets(compilation: any, assets: any) {
Expand All @@ -40,13 +53,33 @@ export default class PagesManifestPlugin implements webpack.Plugin {
pages[pagePath] = files[files.length - 1]

if (!this.dev) {
pages[pagePath] = pages[pagePath].slice(3)
if (!this.isEdgeRuntime) {
pages[pagePath] = pages[pagePath].slice(3)
}
}
pages[pagePath] = pages[pagePath].replace(/\\/g, '/')
}

assets[`${!this.dev ? '../' : ''}` + PAGES_MANIFEST] =
new sources.RawSource(JSON.stringify(pages, null, 2))
// This plugin is used by both the Node server and Edge server compilers,
// we need to merge both pages to generate the full manifest.
if (this.isEdgeRuntime) {
edgeServerPages = pages
} else {
nodeServerPages = pages
}

assets[
`${!this.dev && !this.isEdgeRuntime ? '../' : ''}` + PAGES_MANIFEST
] = new sources.RawSource(
JSON.stringify(
{
...edgeServerPages,
...nodeServerPages,
},
null,
2
)
)
}

apply(compiler: webpack.Compiler): void {
Expand Down
@@ -1,6 +1,10 @@
import { renderViaHTTP } from 'next-test-utils'
import { join } from 'path'
import fs from 'fs-extra'

export default async function runtime(context, { runtime }) {
import { distDir } from './utils'

export default async function runtime(context, { runtime, env }) {
if (runtime === 'edge') {
it('should support per-page runtime configuration', async () => {
const html1 = await renderViaHTTP(context.appPort, '/runtime')
Expand All @@ -9,4 +13,31 @@ export default async function runtime(context, { runtime }) {
expect(html2).toContain('Runtime: Node.js')
})
}
if (runtime === 'edge' && env === 'prod') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should nodejs runtime be included here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is mainly for mixed runtimes. Since the page runtime of /runtime and /runtime-rsc is nodejs, we want to test the case that all other pages are using the edge runtime. If we set the global runtime to nodejs, it's not affected by this PR and already works today.

it('should include entrypoints from both runtimes in pages manifest', async () => {
const distServerDir = join(distDir, 'server')
const pagesManifest = await fs.readJSON(
join(distServerDir, 'pages-manifest.json')
)

for (const key of [
// Defaults:
'/_app',
'/_error',
'/_document',
// Special:
'/404',
// API routes:
'/api/ping',
// Edge runtime pages:
'/streaming',
'/streaming-rsc',
// Node runtime pages:
'/runtime',
'/runtime-rsc',
]) {
expect(key in pagesManifest).toBeTruthy()
}
})
}
}