Unverified Commit 3c87e1b5 authored by Damien Simonin Feugas's avatar Damien Simonin Feugas Committed by GitHub

chore: improves console warnings related to edge runtime (#44563)

## 📖 What's in there?

With Edge Function GA for API Routes, it would make sense to suggest
developer to change their runtime key from `experimental-edge` to
`edge`.

Current behavior is:
1. when using `experimental-edge` in API route: 
> prints a warning _once:_ `warn - You are using an experimental edge
runtime, the API might change.`
   
1. when using `experimental-edge` in pages:
> prints a warning _once:_ `warn - You are using an experimental edge
runtime, the API might change. `
   
1. when using `edge` in pages:
> throws an error _for each page_: `error - Page /xyz provided runtime
'edge', the edge runtime for rendering is currently experimental. Use
runtime 'experimental-edge' instead.`

This PR adjust case # 1 to indicates which API file is using
`experimental-edge` (a warning per page), and suggest to migrate:
`warn - /pages/api/xyz provided runtime 'experimental-edge'. It can be
updated to 'edge' instead.`

- [x] Integration tests added

## 🧪 How to test?

Besides running e2e tests with `NEXT_TEST_MODE=dev pnpm testheadless
--testPathPattern edge-configurable-runtime`, you can create a test app
in `examples` folder:
```jsx
// examples/edge-warnings/pages/api/edge.js
export default () => new Response('ok')
export const config = { runtime: 'experimental-edge' }

// examples/edge-warnings/pages/index.jsx
export default () => <p>hello world</p>
export const runtime = 'experimental-edge'
```

Build next.js then run with `pnpm next dev examples/edge-warnings`.

You can try adding more pages with `experimental-edge` runtime (will not
produce more warnings), adding more API with `experimental-edge` (will
produce new warnings), or change page runtime to `edge` (will produce
errors).
parent beb37f0c
......@@ -2,6 +2,7 @@ import type { NextConfig } from '../../server/config-shared'
import type { Middleware, RouteHas } from '../../lib/load-custom-routes'
import { promises as fs } from 'fs'
import LRUCache from 'next/dist/compiled/lru-cache'
import { matcher } from 'next/dist/compiled/micromatch'
import { ServerRuntime } from 'next/types'
import {
......@@ -231,13 +232,17 @@ function getMiddlewareConfig(
return result
}
let warnedAboutExperimentalEdge = false
function warnAboutExperimentalEdge() {
if (warnedAboutExperimentalEdge) {
const apiRouteWarnings = new LRUCache({ max: 250 })
function warnAboutExperimentalEdge(apiRoute: string | null) {
if (apiRouteWarnings.has(apiRoute)) {
return
}
Log.warn(`You are using an experimental edge runtime, the API might change.`)
warnedAboutExperimentalEdge = true
Log.warn(
apiRoute
? `${apiRoute} provided runtime 'experimental-edge'. It can be updated to 'edge' instead.`
: `You are using an experimental edge runtime, the API might change.`
)
apiRouteWarnings.set(apiRoute, 1)
}
const warnedUnsupportedValueMap = new Map<string, boolean>()
......@@ -326,6 +331,8 @@ export async function getPageStaticInfo(params: {
const requiresServerRuntime = ssr || ssg || pageType === 'app'
const isAnAPIRoute = isAPIRoute(page?.replace(/^\/pages\//, '/'))
resolvedRuntime = isEdgeRuntime(resolvedRuntime)
? resolvedRuntime
: requiresServerRuntime
......@@ -333,14 +340,14 @@ export async function getPageStaticInfo(params: {
: undefined
if (resolvedRuntime === SERVER_RUNTIME.experimentalEdge) {
warnAboutExperimentalEdge()
warnAboutExperimentalEdge(isAnAPIRoute ? page! : null)
}
if (
resolvedRuntime === SERVER_RUNTIME.edge &&
pageType === 'pages' &&
page &&
!isAPIRoute(page.replace(/^\/pages\//, '/'))
!isAnAPIRoute
) {
const message = `Page ${page} provided runtime 'edge', the edge runtime for rendering is currently experimental. Use runtime 'experimental-edge' instead.`
if (isDev) {
......
......@@ -51,7 +51,7 @@ describe('Configurable runtime for pages and API routes', () => {
expect(res.status).toEqual(200)
expect(next.cliOutput).not.toInclude('error')
expect(next.cliOutput).toInclude(
'warn - You are using an experimental edge runtime, the API might change.'
`warn - /pages/api/edge provided runtime 'experimental-edge'. It can be updated to 'edge' instead.`
)
})
it('warns about page using edge runtime', async () => {
......@@ -67,7 +67,7 @@ describe('Configurable runtime for pages and API routes', () => {
expect(res.status).toEqual(200)
expect(next.cliOutput).not.toInclude('error')
expect(next.cliOutput).toInclude(
'warn - You are using an experimental edge runtime, the API might change.'
`warn - You are using an experimental edge runtime, the API might change.`
)
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment