diff --git a/.changeset/route-malformed-path-catch-all.md b/.changeset/route-malformed-path-catch-all.md new file mode 100644 index 0000000000..b219729d0e --- /dev/null +++ b/.changeset/route-malformed-path-catch-all.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Make `Route.union` in `effect/ui` resolve malformed path escapes to the `"/*"` catch-all under the route base, preserving malformed segments in `params["*"]`. diff --git a/packages/effect/src/ui/Route.ts b/packages/effect/src/ui/Route.ts index 96ad31345a..1de08c5b04 100644 --- a/packages/effect/src/ui/Route.ts +++ b/packages/effect/src/ui/Route.ts @@ -850,11 +850,12 @@ const build = ( ? path.replace(/\/+$/, "") || "/" : path const input: Array = [] + let malformed = false if (trimmed !== "/") { for (const part of trimmed.slice(1).split("/")) { const decoded = decodeComponent(part) - if (decoded === undefined) return Option.none() - input.push(decoded) + if (decoded === undefined) malformed = true + input.push(decoded ?? part) } } const search = parseSearch(searchString) @@ -871,7 +872,7 @@ const build = ( notFound: true, invalid: [] }) - if (search === undefined) return fallback() + if (malformed || search === undefined) return fallback() for (const entry of entries) { const raw = matchShape(entry.segments, input) if (raw === undefined) continue @@ -950,11 +951,11 @@ const build = ( * same argument and prints a link: `AppRoute.Person.href({ params: { id: 1 } * })`. `Router.navigable` wraps the union to add `push` and `replace`. * - * Declare a catch-all `"/*"` route, conventionally `NotFound`, whose tag is - * `catchAll`: it ranks last, and a required search field that fails to - * decode resolves to it, so `parse` returns a route for every URL under the - * base and the Model never holds an `Option`. Without one, `parse` returns - * `Option.none()` for such URLs and `catchAll` is `undefined`. + * A catch-all `"/*"` route, conventionally `NotFound`, ranks last; its tag is + * exposed as `catchAll`. Under the route base, it handles unmatched URLs, + * required search fields that fail to decode and malformed percent-escapes. + * Malformed path segments stay as written in `params["*"]`. Without a catch-all, + * `parse` returns `Option.none()` for these URLs and `catchAll` is `undefined`. * * **Gotchas** * diff --git a/packages/effect/src/ui/Router.ts b/packages/effect/src/ui/Router.ts index 891cfd095f..ef27b62368 100644 --- a/packages/effect/src/ui/Router.ts +++ b/packages/effect/src/ui/Router.ts @@ -1690,8 +1690,10 @@ const commandLayers = Layer.mergeAll( * * The Router resolves every href through `routes`: redirects and a trailing * slash are replaced with the canonical URL, and an href the routes don't - * parse is a full page load. It implements blocking, history restore and the - * settle timing; a host adds scroll, announcements and focus in `onSettled`. + * parse is a full page load. The Layer dies if the start URL doesn't resolve; + * a `"/*"` route covers URLs under the route base. The Router implements + * blocking, history restore and the settle timing; a host adds scroll, + * announcements and focus in `onSettled`. * * History restore: unless `restore` is `false`, before leaving an entry, * the Router saves the encoded state of every mounted instance by path: its diff --git a/packages/effect/test/ui/Route.test.ts b/packages/effect/test/ui/Route.test.ts index 75469d9bae..78f715f81e 100644 --- a/packages/effect/test/ui/Route.test.ts +++ b/packages/effect/test/ui/Route.test.ts @@ -250,6 +250,16 @@ describe("Route", () => { invalid: [] } ) + // A malformed path escape falls back to the catch-all with the raw segment. + assert.deepStrictEqual( + Option.getOrThrow(routes.resolve("/people/a%ZZ")), + { + route: { _tag: "NotFound", params: { "*": "people/a%ZZ" }, search: {} }, + redirected: false, + notFound: true, + invalid: [] + } + ) const noCatchAll = Route.union({ Home: Route.make("/") }) assert.isFalse(Option.getOrThrow(noCatchAll.resolve("/")).notFound) assert.isTrue(Option.isNone(noCatchAll.resolve("/missing")))