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