anomalyco/sst · error · VisibleError

Cannot use both `routes` and `.route()` function to add rout

Error message

Cannot use both `routes` and `.route()` function to add routes.

What it means

A Router cannot mix the inline `routes` prop with the lazy `.route()` method. The `route()` method checks `hasInlineRoutes` and throws this VisibleError if inline routes were provided in the constructor.

Source

Thrown at platform/src/components/aws/router.ts:2543

   *   rewrite: {
   *     regex: "^/api/(.*)$",
   *     to: "/$1"
   *   }
   * });
   * ```
   *
   * Here something like `/api/users/profile` will be routed to
   * `https://api.example.com/users/profile`.
   */
  public route(
    pattern: Input<string>,
    url: Input<string>,
    args?: Input<RouterUrlRouteArgs>,
  ) {
    all([pattern, args, this.hasInlineRoutes]).apply(
      ([pattern, args, hasInlineRoutes]) => {
        if (hasInlineRoutes)
          throw new VisibleError(
            "Cannot use both `routes` and `.route()` function to add routes.",
          );

        new RouterUrlRoute(
          `${this.constructorName}Route${pattern}`,
          {
            store: this.kvStoreArn!,
            routerNamespace: this.kvNamespace!,
            pattern,
            url,
            routeArgs: args,
          },
          { provider: this.constructorOpts.provider },
        );
      },
    );
  }

View on GitHub (pinned to a0bd20f762)

Solutions

  1. Move all routes to `.route()` calls and remove the `routes` prop from the constructor
  2. Or keep everything inline and drop the `.route()` call

Example fix

// before
const r = new Router($app, "R", { routes: { "/a/*": "https://a.com" } });
r.route("/b/*", "https://b.com");
// after
const r = new Router($app, "R", {});
r.route("/a/*", "https://a.com");
r.route("/b/*", "https://b.com");
Defensive patterns

Strategy: validation

Validate before calling

if (routerArgs.routes && needsRouteFn)
  throw new Error("Cannot mix `routes` prop with .route() calls");

Try / catch

try {
  const r = new Router(ctx, "R", { routes });
  r.route("/b/*", "https://b.com");
} catch (e) {
  if (String(e).includes(".route() function")) console.error("Pick one route API");
  throw e;
}

Prevention

When it happens

Trigger: `new Router(..., { routes: {...} })` followed by a call to `router.route(pattern, url)`.

Common situations: Incrementally migrating an inline-routes Router to the lazy API while leaving old routes in place; adding one extra route to an existing inline-routes config.

Related errors


AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30). Data as JSON: /api/errors/4d3b6ec404fa2d10. Report an issue: GitHub.