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

fix(switchable-runtime): make dev server not break when wrong runtime config is exported #40312

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -226,21 +226,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>
}