Unverified Commit f9f3afc6 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix: route handlers should validate invalid exports (#83500)

When using an unsupported API in a route handler (eg, `export const
dynamic` with `cacheComponents` enabled), we were not erroring in dev or
failing the build. This is because the transform only asserted on pages
& layouts, and not route handlers.

This updates the handling to also check route handlers. 

Fixes NAR-371
parent 3ef17684
...@@ -805,10 +805,10 @@ impl ReactServerComponentValidator { ...@@ -805,10 +805,10 @@ impl ReactServerComponentValidator {
return; return;
} }
static RE: Lazy<Regex> = static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[\\/](page|layout)\.(ts|js)x?$").unwrap()); Lazy::new(|| Regex::new(r"[\\/](page|layout|route)\.(ts|js)x?$").unwrap());
let is_layout_or_page = RE.is_match(&self.filepath); let is_app_entry = RE.is_match(&self.filepath);
if is_layout_or_page { if is_app_entry {
let mut possibly_invalid_exports: FxIndexMap<Atom, (InvalidExportKind, Span)> = let mut possibly_invalid_exports: FxIndexMap<Atom, (InvalidExportKind, Span)> =
FxIndexMap::default(); FxIndexMap::default();
......
export const runtime = 'edge'
export async function GET(_request: Request) {
// This import should not be instrumented, because edge routes are never prerendered.
// `trackDynamicImport` will throw if it's used in the edge runtime,
// so it's enough to just do an import() here and see if it succeeds.
const messages = (await import('./messages')).default
return new Response(messages.title)
}
...@@ -161,13 +161,6 @@ describe('async imports in cacheComponents', () => { ...@@ -161,13 +161,6 @@ describe('async imports in cacheComponents', () => {
// indirectly tests the behavior of middleware by rendering a page which the middleware matches // indirectly tests the behavior of middleware by rendering a page which the middleware matches
await testPage('/not-instrumented/middleware') await testPage('/not-instrumented/middleware')
}) })
it('edge route handler', async () => {
const result = await next
.fetch('/not-instrumented/edge-route-handler')
.then((res) => res.text())
expect(result).toBe('hello')
})
}) })
}) })
......
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export const dynamic = 'force-static'
export async function GET(request: Request) {
return new Response('route GET')
}
export const fetchCache = 'force-cache'
export async function GET(request: Request) {
return new Response('route GET with fetchCache')
}
export const revalidate = 60
export async function GET(request: Request) {
return new Response('route GET with revalidate')
}
import { nextTestSetup } from 'e2e-utils'
import {
assertHasRedbox,
getRedboxDescription,
getRedboxSource,
} from 'next-test-utils'
describe('cache-components-route-handler-errors', () => {
const { next, skipped, isNextDev, isTurbopack } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
})
if (skipped) {
return
}
it("should error when route handlers use segment configs that aren't supported by cacheComponents", async () => {
try {
await next.start()
} catch {
// we expect the build to fail
}
if (isNextDev) {
// Test the first route handler with "dynamic" config
const browser = await next.browser('/route-with-dynamic')
await assertHasRedbox(browser)
const redbox = {
description: await getRedboxDescription(browser),
source: await getRedboxSource(browser),
}
if (isTurbopack) {
expect(redbox.description).toMatchInlineSnapshot(
`"Ecmascript file had an error"`
)
} else {
expect(redbox.description).toMatchInlineSnapshot(
`" x Route segment config "dynamic" is not compatible with \`nextConfig.experimental.cacheComponents\`. Please remove it."`
)
}
expect(redbox.source).toContain(
'"dynamic" is not compatible with `nextConfig.experimental.cacheComponents`. Please remove it.'
)
} else {
// In build mode, check for all three errors in the output
expect(next.cliOutput).toContain('./app/route-with-dynamic/route.ts')
expect(next.cliOutput).toContain(
'"dynamic" is not compatible with `nextConfig.experimental.cacheComponents`. Please remove it.'
)
expect(next.cliOutput).toContain('./app/route-with-revalidate/route.ts')
expect(next.cliOutput).toContain(
'"revalidate" is not compatible with `nextConfig.experimental.cacheComponents`. Please remove it.'
)
expect(next.cliOutput).toContain('./app/route-with-fetchcache/route.ts')
expect(next.cliOutput).toContain(
'"fetchCache" is not compatible with `nextConfig.experimental.cacheComponents`. Please remove it.'
)
}
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
cacheComponents: true,
},
}
module.exports = nextConfig
...@@ -140,6 +140,7 @@ ...@@ -140,6 +140,7 @@
"test/e2e/app-dir/app/standalone-gsp.test.ts", "test/e2e/app-dir/app/standalone-gsp.test.ts",
"test/e2e/app-dir/app/standalone.test.ts", "test/e2e/app-dir/app/standalone.test.ts",
"test/e2e/app-dir/app/useReportWebVitals.test.ts", "test/e2e/app-dir/app/useReportWebVitals.test.ts",
"test/e2e/app-dir/bun-externals/bun-externals.test.ts",
"test/e2e/app-dir/async-component-preload/async-component-preload.test.ts", "test/e2e/app-dir/async-component-preload/async-component-preload.test.ts",
"test/e2e/app-dir/autoscroll-with-css-modules/index.test.ts", "test/e2e/app-dir/autoscroll-with-css-modules/index.test.ts",
"test/e2e/app-dir/back-forward-cache/back-forward-cache.test.ts", "test/e2e/app-dir/back-forward-cache/back-forward-cache.test.ts",
...@@ -268,6 +269,7 @@ ...@@ -268,6 +269,7 @@
"test/e2e/app-dir/static-shell-debugging/static-shell-debugging.test.ts", "test/e2e/app-dir/static-shell-debugging/static-shell-debugging.test.ts",
"test/e2e/app-dir/taint/process-taint.test.ts", "test/e2e/app-dir/taint/process-taint.test.ts",
"test/e2e/app-dir/temporary-references/temporary-references.test.ts", "test/e2e/app-dir/temporary-references/temporary-references.test.ts",
"test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts",
"test/e2e/app-dir/unauthorized/basic/unauthorized-basic.test.ts", "test/e2e/app-dir/unauthorized/basic/unauthorized-basic.test.ts",
"test/e2e/app-dir/unauthorized/default/unauthorized-default.test.ts", "test/e2e/app-dir/unauthorized/default/unauthorized-default.test.ts",
"test/e2e/app-dir/unstable-rethrow/unstable-rethrow.test.ts", "test/e2e/app-dir/unstable-rethrow/unstable-rethrow.test.ts",
......
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