Skip to content

Client middleware on index/catchall routes crashes the lazy loader #138

Description

@KyleJune

Summary

When an index.tsx or [catchall].tsx route module exports client
middleware, the generated main.tsx imports the module eagerly, but Client
treats index/catchall as a lazy loader and calls it as a function. The call
throws a TypeError whenever that route's lazy loader runs.

Where

At 64fc98e:

  • src/_build.ts:307 and src/_build.ts:310: when hasMiddlewareExport is
    true, properties.index and properties.catchall get directImport
    (await import("…")), which is the module namespace object.
  • src/client.tsx:53 and :58: ClientRoute.index / .catchall are typed
    RouteModuleLoader only. Unlike main, they have no RouteModule branch.
  • src/client.tsx:183 and :216: both are always passed to
    createLazyRoute(route.index, …) / createLazyRoute(route.catchall, …).
  • src/_client.tsx createLazyRoute: await lazyRouteFile() on a module
    object throws TypeError: lazyRouteFile is not a function. That lands in the
    load-failure recovery branch, which treats it as a stale-deploy chunk failure
    and hard-navigates to the destination. The retry guard (shouldReload,
    MAX_RELOAD_RETRIES within RELOAD_WINDOW_MS) should eventually stop the
    reloads and surface the error, but the page reloads repeatedly first. The
    exact runtime reload behavior is UNVERIFIED.

There is a second problem even without the crash: middleware can't be lazy in
React Router, so createLazyRoute strips it. An index or catchall route's
client middleware therefore never runs through this path.

Expected

An index or catchall .tsx module that exports middleware builds and runs,
with its middleware applied, the same way a main.tsx or regular child route
does: an eager RouteModule whose route object is built by createRoute.

Notes

Found during review of #136. That PR doesn't fix this; it only adds
shouldRevalidate passthrough.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions