Skip to content

Commit

Permalink
Fix edge runtime detection from layouts (#49126)
Browse files Browse the repository at this point in the history
This ensures we properly handle edge runtime during build when loading pages as currently we are only check the page itself for the runtime flag although it can be nested higher up but we already have the relevant info in the middleware-manifest so we can use that during build. 


Fixes: 
```sh
info  - Linting and checking validity of types
info  - Collecting page data ..ReferenceError: self is not defined
    at Object.<anonymous> (/Users/jj/dev/vercel/next.js/test/e2e/app-dir/app-edge/.next/server/app/edge/basic/page.js:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at requirePage (/Users/jj/dev/vercel/next.js/packages/next/src/server/require.ts:126:10)
    at <anonymous> (/Users/jj/dev/vercel/next.js/packages/next/src/server/load-components.ts:105:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

> Build error occurred
Error: Failed to collect page data for /edge/basic
```
  • Loading branch information
ijjk authored May 3, 2023
1 parent 56de0b2 commit e54e38a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
7 changes: 6 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,12 @@ export default async function build(
})
: undefined

const pageRuntime = staticInfo?.runtime
const pageRuntime = middlewareManifest.functions[
originalAppPath || page
]
? 'edge'
: staticInfo?.runtime

isServerComponent =
pageType === 'app' &&
staticInfo?.rsc !== RSC_MODULE_TYPES.client
Expand Down
14 changes: 9 additions & 5 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ import * as Log from '../../build/output/log'
import isError, { getProperError } from '../../lib/is-error'
import { getRouteRegex } from '../../shared/lib/router/utils/route-regex'
import { getSortedRoutes } from '../../shared/lib/router/utils'
import { runDependingOnPageType } from '../../build/entries'
import {
getStaticInfoIncludingLayouts,
runDependingOnPageType,
} from '../../build/entries'
import { NodeNextResponse, NodeNextRequest } from '../base-http/node'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import {
Expand Down Expand Up @@ -498,12 +500,14 @@ export default class DevServer extends Server {
pagesType: 'root',
})

const staticInfo = await getPageStaticInfo({
const staticInfo = await getStaticInfoIncludingLayouts({
pageFilePath: fileName,
nextConfig: this.nextConfig,
config: this.nextConfig,
appDir: this.appDir,
page: rootFile,
isDev: true,
pageType: isAppPath ? 'app' : 'pages',
isInsideAppDir: isAppPath,
pageExtensions: this.nextConfig.pageExtensions,
})

if (isMiddlewareFile(rootFile)) {
Expand Down
15 changes: 9 additions & 6 deletions packages/next/src/server/dev/on-demand-entry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import type {

import { EventEmitter } from 'events'
import { findPageFile } from '../lib/find-page-file'
import { runDependingOnPageType } from '../../build/entries'
import {
getStaticInfoIncludingLayouts,
runDependingOnPageType,
} from '../../build/entries'
import { join, posix } from 'path'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash'
import { removePagePathTail } from '../../shared/lib/page-path/remove-page-path-tail'
import { reportTrigger } from '../../build/output'
import getRouteFromEntrypoint from '../get-route-from-entrypoint'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import {
isInstrumentationHookFile,
isInstrumentationHookFilename,
Expand Down Expand Up @@ -712,7 +714,6 @@ export function onDemandEntryHandler({
const isInsideAppDir =
!!appDir && pagePathData.absolutePagePath.startsWith(appDir)

const pageType = isInsideAppDir ? 'app' : 'pages'
const pageBundleType = getPageBundleType(pagePathData.bundlePath)
const addEntry = (
compilerType: CompilerNameValues
Expand Down Expand Up @@ -767,12 +768,14 @@ export function onDemandEntryHandler({
}
}

const staticInfo = await getPageStaticInfo({
const staticInfo = await getStaticInfoIncludingLayouts({
page,
pageFilePath: pagePathData.absolutePagePath,
nextConfig,
isInsideAppDir,
pageExtensions: nextConfig.pageExtensions,
isDev: true,
pageType,
config: nextConfig,
appDir,
})

const added = new Map<CompilerNameValues, ReturnType<typeof addEntry>>()
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/app-dir/app-edge/app/edge/basic/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ export default function Page() {
}
return <p>Node!</p>
}

export const runtime = 'edge'
2 changes: 2 additions & 0 deletions test/e2e/app-dir/app-edge/app/edge/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export default function Layout({ children }: { children: React.ReactNode }) {
useSelectedLayoutSegments()
return children
}

export const runtime = 'edge'

0 comments on commit e54e38a

Please sign in to comment.