Skip to content

Commit b6fd8fd

Browse files
committed
fix: added invariant errors for unexpected conditions and FIXME's
1 parent 6cdaed0 commit b6fd8fd

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

packages/next/errors.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,5 +788,7 @@
788788
"787": "\\`experimental.clientParamParsing\\` can not be \\`true\\` when \\`experimental.clientSegmentCache\\` is \\`false\\`. Client param parsing is only relevant when client segment cache is enabled.",
789789
"788": "Unexpected dynamic param type: %s",
790790
"789": "Expected RSC response, got %s",
791-
"790": "Invariant: Expected RSC response, got %s"
791+
"790": "Invariant: Expected RSC response, got %s",
792+
"791": "Unexpected match for a pathname \"%s\" with a param \"%s\" of type \"%s\"",
793+
"792": "Unexpected empty path segments match for a pathname \"%s\" with param \"%s\" of type \"%s\""
792794
}

packages/next/src/build/static-paths/app.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { createIncrementalCache } from '../../export/helpers/create-incremental-
2626
import type { NextConfigComplete } from '../../server/config-shared'
2727
import type { WorkStore } from '../../server/app-render/work-async-storage.external'
2828
import type { DynamicParamTypes } from '../../shared/lib/app-router-types'
29+
import { InvariantError } from '../../shared/lib/invariant-error'
2930

3031
/**
3132
* Filters out duplicate parameters from a list of parameters.
@@ -596,18 +597,22 @@ export function resolveParallelRouteParams(
596597
// This mimics the behavior in getDynamicParam where the pagePath
597598
// is split and used to populate catchall values
598599
if (pathSegments.length > 0) {
600+
// FIXME: (NAR-335) this should handle prefixed segments
599601
params[paramName] = pathSegments
600602
} else if (paramType === 'optional-catchall') {
601603
params[paramName] = []
602604
} else {
603-
// For regular catchall and catchall-intercepted with no segments
604-
fallbackRouteParams.push(
605-
createFallbackRouteParam(paramName, paramType, true)
605+
// We shouldn't be able to match a catchall segment without any path
606+
// segments if it's not an optional catchall.
607+
throw new InvariantError(
608+
`Unexpected empty path segments match for a pathname "${pathname}" with param "${paramName}" of type "${paramType}"`
606609
)
607610
}
608611
} else {
609-
fallbackRouteParams.push(
610-
createFallbackRouteParam(paramName, paramType, true)
612+
// This is some other type of route param that shouldn't get resolved
613+
// statically.
614+
throw new InvariantError(
615+
`Unexpected match for a pathname "${pathname}" with a param "${paramName}" of type "${paramType}"`
611616
)
612617
}
613618
}

packages/next/src/server/request/fallback-params.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { collectFallbackRouteParams } from '../../build/segment-config/app/app-segments'
22
import type { FallbackRouteParam } from '../../build/static-paths/types'
33
import type { DynamicParamTypesShort } from '../../shared/lib/app-router-types'
4+
import { InvariantError } from '../../shared/lib/invariant-error'
45
import { getRouteMatcher } from '../../shared/lib/router/utils/route-matcher'
56
import { getRouteRegex } from '../../shared/lib/router/utils/route-regex'
67
import { dynamicParamTypes } from '../app-render/get-short-dynamic-param-type'
@@ -139,24 +140,27 @@ export function getFallbackRouteParams(
139140
continue
140141
}
141142

142-
// If there are no path segments and this is not an optional catchall,
143-
// we can add it to the fallback route params as the value is unknown.
144143
if (
145144
pathSegments.length === 0 &&
146145
fallbackRouteParam.paramType !== 'optional-catchall'
147146
) {
148-
fallbackRouteParams.push(fallbackRouteParam)
149-
continue
147+
// We shouldn't be able to match a catchall segment without any path
148+
// segments if it's not an optional catchall.
149+
throw new InvariantError(
150+
`Unexpected empty path segments match for a pathname "${page}" with param "${fallbackRouteParam.paramName}" of type "${fallbackRouteParam.paramType}"`
151+
)
150152
}
151153

152154
// The path segments are not empty, and the segments didn't contain any
153155
// unknown params, so we know that this particular fallback route param
154156
// route param is not actually unknown, and is known. We can skip adding
155157
// it to the fallback route params.
156158
} else {
157-
// This is some other type of route param, but it wasn't already
158-
// resolved, so we can add it to the fallback route params.
159-
fallbackRouteParams.push(fallbackRouteParam)
159+
// This is some other type of route param that shouldn't get resolved
160+
// statically.
161+
throw new InvariantError(
162+
`Unexpected match for a pathname "${page}" with a param "${fallbackRouteParam.paramName}" of type "${fallbackRouteParam.paramType}"`
163+
)
160164
}
161165
} else if (unknownParamKeys.has(fallbackRouteParam.paramName)) {
162166
// As this is a non-parallel route segment, and it exists in the unknown

packages/next/src/shared/lib/router/utils/get-dynamic-param.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export function getDynamicParam(
7777

7878
// handle the case where a catchall or optional catchall does not have a value,
7979
// e.g. `/foo/bar/hello` and `@slot/[...catchall]` or `@slot/[[...catchall]]` is matched
80+
// FIXME: (NAR-335) this should handle prefixed segments
8081
value = pagePath
8182
.split('/')
8283
// remove the first empty string

0 commit comments

Comments
 (0)