Skip to content

Commit

Permalink
Fix(typedRoutes): fixed Webpack crashing without redirects/rewrites (#…
Browse files Browse the repository at this point in the history
…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)

---------
  • Loading branch information
DuCanhGH authored Mar 1, 2023
1 parent 9b6ede2 commit ff69947
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/build/webpack-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 29 additions & 22 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ interface Options {
isEdgeServer: boolean
pageExtensions: string[]
typedRoutes: boolean
originalRewrites: Rewrites
originalRedirects: Redirect[]
originalRewrites: Rewrites | undefined
originalRedirects: Redirect[] | undefined
}

function createTypeGuardFile(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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' {
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/integration/app-types/src/app/type-checks/link/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
</>
Expand Down

0 comments on commit ff69947

Please sign in to comment.