Unverified Commit ff699471 authored by Ngô Đức Anh's avatar Ngô Đức Anh Committed by GitHub

Fix(typedRoutes): fixed Webpack crashing without redirects/rewrites (#46591)

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:
-->

This PR fixes some issue caused by PR 46327, which asserted that
`NextBuildContext.original(Rewrites|Redirects)` are defined although
`config._original(Redirects|Rewrites)` (which these two copied from) are
not actually defined when `config.redirects`/`config.rewrites` are not
as well (see `loadRedirects`/`loadRewrites`). So this PR fixes that by
removing those assertions and checking whether those are defined in
`createRouteDefinitions` before iterating them.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

---------
parent 9b6ede29
...@@ -91,8 +91,8 @@ async function webpackBuildImpl(): Promise<{ ...@@ -91,8 +91,8 @@ async function webpackBuildImpl(): Promise<{
appDir: NextBuildContext.appDir!, appDir: NextBuildContext.appDir!,
pagesDir: NextBuildContext.pagesDir!, pagesDir: NextBuildContext.pagesDir!,
rewrites: NextBuildContext.rewrites!, rewrites: NextBuildContext.rewrites!,
originalRewrites: NextBuildContext.originalRewrites!, originalRewrites: NextBuildContext.originalRewrites,
originalRedirects: NextBuildContext.originalRedirects!, originalRedirects: NextBuildContext.originalRedirects,
reactProductionProfiling: NextBuildContext.reactProductionProfiling!, reactProductionProfiling: NextBuildContext.reactProductionProfiling!,
noMangling: NextBuildContext.noMangling!, noMangling: NextBuildContext.noMangling!,
clientRouterFilters: NextBuildContext.clientRouterFilters!, clientRouterFilters: NextBuildContext.clientRouterFilters!,
......
...@@ -636,8 +636,8 @@ export default async function getBaseWebpackConfig( ...@@ -636,8 +636,8 @@ export default async function getBaseWebpackConfig(
pagesDir?: string pagesDir?: string
reactProductionProfiling?: boolean reactProductionProfiling?: boolean
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
originalRewrites: CustomRoutes['rewrites'] originalRewrites: CustomRoutes['rewrites'] | undefined
originalRedirects: CustomRoutes['redirects'] originalRedirects: CustomRoutes['redirects'] | undefined
runWebpackSpan: Span runWebpackSpan: Span
target?: string target?: string
appDir?: string appDir?: string
......
...@@ -29,8 +29,8 @@ interface Options { ...@@ -29,8 +29,8 @@ interface Options {
isEdgeServer: boolean isEdgeServer: boolean
pageExtensions: string[] pageExtensions: string[]
typedRoutes: boolean typedRoutes: boolean
originalRewrites: Rewrites originalRewrites: Rewrites | undefined
originalRedirects: Redirect[] originalRedirects: Redirect[] | undefined
} }
function createTypeGuardFile( function createTypeGuardFile(
...@@ -139,7 +139,10 @@ export const devPageFiles = new Set<string>() ...@@ -139,7 +139,10 @@ export const devPageFiles = new Set<string>()
const edgeRoutes: string[] = [] const edgeRoutes: string[] = []
const nodeRoutes: string[] = [] const nodeRoutes: string[] = []
function createRouteDefinitions(rewrites: Rewrites, redirects: Redirect[]) { function createRouteDefinitions(
rewrites: Rewrites | undefined,
redirects: Redirect[] | undefined
) {
const extraRoutes: string[] = [] const extraRoutes: string[] = []
function addExtraRoute(source: string) { function addExtraRoute(source: string) {
let tokens: Token[] | undefined let tokens: Token[] | undefined
...@@ -171,20 +174,25 @@ function createRouteDefinitions(rewrites: Rewrites, redirects: Redirect[]) { ...@@ -171,20 +174,25 @@ function createRouteDefinitions(rewrites: Rewrites, redirects: Redirect[]) {
} }
} }
for (const rewrite of rewrites.beforeFiles) { if (rewrites) {
addExtraRoute(rewrite.source) for (const rewrite of rewrites.beforeFiles) {
} addExtraRoute(rewrite.source)
for (const rewrite of rewrites.afterFiles) { }
addExtraRoute(rewrite.source) for (const rewrite of rewrites.afterFiles) {
} addExtraRoute(rewrite.source)
for (const rewrite of rewrites.fallback) { }
addExtraRoute(rewrite.source) for (const rewrite of rewrites.fallback) {
addExtraRoute(rewrite.source)
}
} }
for (const redirect of redirects) {
// Skip internal redirects if (redirects) {
// https://github.com/vercel/next.js/blob/8ff3d7ff57836c24088474175d595b4d50b3f857/packages/next/src/lib/load-custom-routes.ts#L704-L710 for (const redirect of redirects) {
if (!('internal' in redirect)) { // Skip internal redirects
addExtraRoute(redirect.source) // https://github.com/vercel/next.js/blob/8ff3d7ff57836c24088474175d595b4d50b3f857/packages/next/src/lib/load-custom-routes.ts#L704-L710
if (!('internal' in redirect)) {
addExtraRoute(redirect.source)
}
} }
} }
...@@ -264,8 +272,8 @@ declare namespace __next_route_internal_types__ { ...@@ -264,8 +272,8 @@ declare namespace __next_route_internal_types__ {
type RouteImpl<T> = ${fallback} type RouteImpl<T> = ${fallback}
| StaticRoutes | StaticRoutes
| \`\${StaticRoutes}\${SearchOrHash}\` | \`\${StaticRoutes}\${Suffix}\`
| (T extends \`\${DynamicRoutes<infer _>}\${'' | SearchOrHash}\` ? T : never) | (T extends \`\${DynamicRoutes<infer _>}\${Suffix}\` ? T : never)
} }
declare module 'next' { declare module 'next' {
...@@ -274,9 +282,8 @@ declare module 'next' { ...@@ -274,9 +282,8 @@ declare module 'next' {
export type Route<T = any> = __next_route_internal_types__.RouteImpl<T> export type Route<T = any> = __next_route_internal_types__.RouteImpl<T>
} }
declare module 'next/link' { declare module 'next/link' {
import type { Route } from 'next'
import type { LinkProps as OriginalLinkProps } from 'next/dist/client/link' import type { LinkProps as OriginalLinkProps } from 'next/dist/client/link'
import type { AnchorHTMLAttributes } from 'react' import type { AnchorHTMLAttributes } from 'react'
import type { UrlObject } from 'url' import type { UrlObject } from 'url'
...@@ -306,8 +313,8 @@ export class NextTypesPlugin { ...@@ -306,8 +313,8 @@ export class NextTypesPlugin {
pageExtensions: string[] pageExtensions: string[]
pagesDir: string pagesDir: string
typedRoutes: boolean typedRoutes: boolean
originalRewrites: Rewrites originalRewrites: Rewrites | undefined
originalRedirects: Redirect[] originalRedirects: Redirect[] | undefined
constructor(options: Options) { constructor(options: Options) {
this.dir = options.dir this.dir = options.dir
......
...@@ -25,10 +25,10 @@ export default function page() { ...@@ -25,10 +25,10 @@ export default function page() {
<Link href="/blog/a#1/b">test</Link> <Link href="/blog/a#1/b">test</Link>
<Link href="/blog/v/w/z">test</Link> <Link href="/blog/v/w/z">test</Link>
<Link href="/(newroot)/dashboard/another" /> <Link href="/(newroot)/dashboard/another" />
<Link href="/dashboard/">test</Link> <Link href="/dashboard">test</Link>
<Link href={`/blog/a/${test}`}>test</Link> <Link href={`/blog/a/${test}`}>test</Link>
<Link href="/rewrite-any">test</Link> <Link href="/rewrite-any">test</Link>
<Link href="/rewrite-one-or-more/">test</Link> <Link href="/rewrite-one-or-more">test</Link>
<Link href="/rewrite-param/page">test</Link> <Link href="/rewrite-param/page">test</Link>
<Link href="/rewrite-param/x/page1">test</Link> <Link href="/rewrite-param/x/page1">test</Link>
</> </>
......
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