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

fix typedRoutes when used with webpackBuildWorker (#59140)

When using `experimental.typedRoutes` in conjunction with
`experimental.webpackBuildWorker`, type errors would be erroneously
thrown during build.

This is because the build workers are parallelized between multiple
runtimes (edge, server, client), but the `typedRoutes` is unique to each
`webpackBuild`. The state needs to shared between the different compile
steps for each instance of the types plugin.

This leverages plugin state to keep share the `typedRoutes` state
amongst the different workers.

Closes NEXT-1734
Fixes #58369
parent 1c61153d
...@@ -15,6 +15,7 @@ import { isDynamicRoute } from '../../../../shared/lib/router/utils' ...@@ -15,6 +15,7 @@ import { isDynamicRoute } from '../../../../shared/lib/router/utils'
import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths'
import { getPageFromPath } from '../../../entries' import { getPageFromPath } from '../../../entries'
import { devPageFiles } from './shared' import { devPageFiles } from './shared'
import { getProxiedPluginState } from '../../../build-context'
const PLUGIN_NAME = 'NextTypesPlugin' const PLUGIN_NAME = 'NextTypesPlugin'
...@@ -221,10 +222,9 @@ async function collectNamedSlots(layoutPath: string) { ...@@ -221,10 +222,9 @@ async function collectNamedSlots(layoutPath: string) {
// By exposing the static route types separately as string literals, // By exposing the static route types separately as string literals,
// editors can provide autocompletion for them. However it's currently not // editors can provide autocompletion for them. However it's currently not
// possible to provide the same experience for dynamic routes. // possible to provide the same experience for dynamic routes.
const routeTypes: Record<
'edge' | 'node' | 'extra', const pluginState = getProxiedPluginState({
Record<'static' | 'dynamic', string> routeTypes: {
> = {
edge: { edge: {
static: '', static: '',
dynamic: '', dynamic: '',
...@@ -237,7 +237,8 @@ const routeTypes: Record< ...@@ -237,7 +237,8 @@ const routeTypes: Record<
static: '', static: '',
dynamic: '', dynamic: '',
}, },
} } as Record<'edge' | 'node' | 'extra', Record<'static' | 'dynamic', string>>,
})
function formatRouteToRouteType(route: string) { function formatRouteToRouteType(route: string) {
const isDynamic = isDynamicRoute(route) const isDynamic = isDynamicRoute(route)
...@@ -341,7 +342,8 @@ function addRedirectsRewritesRouteTypes( ...@@ -341,7 +342,8 @@ function addRedirectsRewritesRouteTypes(
for (const normalizedRoute of possibleNormalizedRoutes) { for (const normalizedRoute of possibleNormalizedRoutes) {
const { isDynamic, routeType } = formatRouteToRouteType(normalizedRoute) const { isDynamic, routeType } = formatRouteToRouteType(normalizedRoute)
routeTypes.extra[isDynamic ? 'dynamic' : 'static'] += routeType pluginState.routeTypes.extra[isDynamic ? 'dynamic' : 'static'] +=
routeType
} }
} }
} }
...@@ -374,8 +376,8 @@ function createRouteDefinitions() { ...@@ -374,8 +376,8 @@ function createRouteDefinitions() {
let dynamicRouteTypes = '' let dynamicRouteTypes = ''
for (const type of ['edge', 'node', 'extra'] as const) { for (const type of ['edge', 'node', 'extra'] as const) {
staticRouteTypes += routeTypes[type].static staticRouteTypes += pluginState.routeTypes[type].static
dynamicRouteTypes += routeTypes[type].dynamic dynamicRouteTypes += pluginState.routeTypes[type].dynamic
} }
// If both StaticRoutes and DynamicRoutes are empty, fallback to type 'string'. // If both StaticRoutes and DynamicRoutes are empty, fallback to type 'string'.
...@@ -578,7 +580,7 @@ export class NextTypesPlugin { ...@@ -578,7 +580,7 @@ export class NextTypesPlugin {
const { isDynamic, routeType } = formatRouteToRouteType(route) const { isDynamic, routeType } = formatRouteToRouteType(route)
routeTypes[this.isEdgeServer ? 'edge' : 'node'][ pluginState.routeTypes[this.isEdgeServer ? 'edge' : 'node'][
isDynamic ? 'dynamic' : 'static' isDynamic ? 'dynamic' : 'static'
] += routeType ] += routeType
} }
...@@ -667,11 +669,11 @@ export class NextTypesPlugin { ...@@ -667,11 +669,11 @@ export class NextTypesPlugin {
// Clear routes // Clear routes
if (this.isEdgeServer) { if (this.isEdgeServer) {
routeTypes.edge.dynamic = '' pluginState.routeTypes.edge.dynamic = ''
routeTypes.edge.static = '' pluginState.routeTypes.edge.static = ''
} else { } else {
routeTypes.node.dynamic = '' pluginState.routeTypes.node.dynamic = ''
routeTypes.node.static = '' pluginState.routeTypes.node.static = ''
} }
compilation.chunkGroups.forEach((chunkGroup) => { compilation.chunkGroups.forEach((chunkGroup) => {
......
...@@ -806,6 +806,7 @@ export const defaultConfig: NextConfig = { ...@@ -806,6 +806,7 @@ export const defaultConfig: NextConfig = {
process.env.__NEXT_EXPERIMENTAL_PPR === 'true' process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
? true ? true
: false, : false,
webpackBuildWorker: false,
}, },
} }
......
import { NextRequest, NextResponse } from 'next/server'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
return NextResponse.json({ hello: 'world' })
}
import React from 'react'
export const dynamic = 'force-dynamic'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Home() {
return (
<main>
<Link href="/asdfasdfasdf">Hello, world!</Link>
</main>
)
}
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
typedRoutes: true,
webpackBuildWorker: true,
},
}
import { NextRequest, NextResponse } from 'next/server'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
return NextResponse.json({ hello: 'world' })
}
import React from 'react'
export const dynamic = 'force-dynamic'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Home() {
return (
<main>
<Link href="/">Hello, world!</Link>
</main>
)
}
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
typedRoutes: true,
webpackBuildWorker: true,
},
}
import { nextBuild } from 'next-test-utils'
import path from 'path'
describe('app dir - typed-routes-with-webpack-worker', () => {
it('builds successfully without errors', async () => {
const output = await nextBuild(
path.join(__dirname, 'good-routes'),
undefined,
{
stdout: true,
stderr: true,
}
)
// check for the experimental flag warning
expect(output.stdout).toContain('webpackBuildWorker')
// should have a successful build
expect(output.code).toBe(0)
// with no errors
expect(output.stderr).not.toContain(`"/" is not an existing route.`)
})
it('builds with valid errors', async () => {
const output = await nextBuild(
path.join(__dirname, 'bad-routes'),
undefined,
{
stdout: true,
stderr: true,
}
)
// check for the experimental flag warning
expect(output.stdout).toContain('webpackBuildWorker')
// should have a failed build
expect(output.code).toBe(1)
// with correct error
expect(output.stderr).toContain(`"/asdfasdfasdf" is not an existing route.`)
})
})
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