Skip to content

Commit

Permalink
fix(switchable-runtime): make dev server not break when wrong runtime…
Browse files Browse the repository at this point in the history
… config is exported (#40312)

Currently the DEV server can't recover if you export an invalid runtime
config. It ends up in a state where it stops to work but nothing is
printed to the terminal.

It now prints an error but keeps working. When building it should crash,
there's an existing test for that
https://github.com/vercel/next.js/blob/canary/test/production/exported-runtimes-value-validation/index.test.ts#L5-L17

#### Reproduce
```tsx
export default function Page() {
  return <p>hello world</p>
}

export const config = {
  runtime: 'something-odd',
}

```
  • Loading branch information
Hannes Bornö authored Sep 7, 2022
1 parent 084ad96 commit 35253e1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/next/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,22 @@ export async function getPageStaticInfo(params: {
}

if (
typeof config.runtime !== 'string' &&
typeof config.runtime !== 'undefined'
typeof config.runtime !== 'undefined' &&
!isServerRuntime(config.runtime)
) {
throw new Error(`Provided runtime `)
} else if (!isServerRuntime(config.runtime)) {
const options = Object.values(SERVER_RUNTIME).join(', ')
if (typeof config.runtime !== 'string') {
throw new Error(
Log.error(
`The \`runtime\` config must be a string. Please leave it empty or choose one of: ${options}`
)
} else {
throw new Error(
Log.error(
`Provided runtime "${config.runtime}" is not supported. Please leave it empty or choose one of: ${options}`
)
}
if (!isDev) {
process.exit(1)
}
}

let runtime =
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/dev/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ export default class HotReloader {
? await getPageStaticInfo({
pageFilePath: entryData.absolutePagePath,
nextConfig: this.config,
isDev: true,
})
: {}

Expand Down
1 change: 1 addition & 0 deletions packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export default class DevServer extends Server {
pageFilePath: fileName,
nextConfig: this.nextConfig,
page: rootFile,
isDev: true,
})

if (isMiddlewareFile(rootFile)) {
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/dev/on-demand-entry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ export function onDemandEntryHandler({
const staticInfo = await getPageStaticInfo({
pageFilePath: pagePathData.absolutePagePath,
nextConfig,
isDev: true,
})

const added = new Map<CompilerNameValues, ReturnType<typeof addEntry>>()
Expand Down
68 changes: 68 additions & 0 deletions test/e2e/switchable-runtime/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,74 @@ describe('Switchable runtime', () => {
})
}
})

it('should not crash the dev server when invalid runtime is configured', async () => {
await check(
() => renderViaHTTP(next.url, '/invalid-runtime'),
/Hello from page without errors/
)

// Invalid runtime type
await next.patchFile(
'pages/invalid-runtime.js',
`
export default function Page() {
return <p>Hello from page with invalid type</p>
}
export const config = {
runtime: 10,
}
`
)
await check(
() => renderViaHTTP(next.url, '/invalid-runtime'),
/Hello from page with invalid type/
)
expect(next.cliOutput).toInclude(
'error - The `runtime` config must be a string. Please leave it empty or choose one of:'
)

// Invalid runtime
await next.patchFile(
'pages/invalid-runtime.js',
`
export default function Page() {
return <p>Hello from page with invalid runtime</p>
}
export const config = {
runtime: "asd"
}
`
)
await check(
() => renderViaHTTP(next.url, '/invalid-runtime'),
/Hello from page with invalid runtime/
)
expect(next.cliOutput).toInclude(
'error - Provided runtime "asd" is not supported. Please leave it empty or choose one of:'
)

// Fix the runtime
await next.patchFile(
'pages/invalid-runtime.js',
`
export default function Page() {
return <p>Hello from page without errors</p>
}
export const config = {
runtime: 'experimental-edge',
}
`
)
await check(
() => renderViaHTTP(next.url, '/invalid-runtime'),
/Hello from page without errors/
)
})
})
} else {
describe('Switchable runtime (prod)', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/switchable-runtime/pages/invalid-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>Hello from page without errors</p>
}

0 comments on commit 35253e1

Please sign in to comment.