Middlewares

syncRoutes

Check whether the client and server routes are in sync.

Check whether the client and server routes are in sync.

When you deploy a server where a route's types changed, an app that is already open still sends data in the old shape, and the server may reject or misread it. Use this middleware to stop such a call, so your app can ask the user to reload.

With each call, the client sends a route sync id: an id for the types of every route it calls. When the server's id differs, the call stops before the route runs and your code gets an error.

Changes That Stop a Call

The id changes when the route's params type or return type changes, or the way they are encoded. Only the compact parser encodes them differently. In a batch, every route is checked, and one changed route stops the whole batch.

Change on the serverThe call
The route's params typeStopped
The route's return typeStopped
The parser of the route or router switches to or from compactStopped
mion upgraded, and the client was built with the old versionStopped, for every route
The route's options, like a query turned into a mutationRuns
A middleware's params typeRuns. The middleware's own validation rejects data that no longer fits.
A route addedOther routes keep running
The route removedNot checked. The call fails because the route is not found.

Adding the Middleware

Add mionSyncRoutes from @mionjs/router/middlewares at the root of your routes. If you also use mionFetchMetadata, put that one first.

server.routes.ts
import {createMionRouter, Routes} from '@mionjs/router';
import {mionFetchMetadata, mionSyncRoutes} from '@mionjs/router/middlewares';

const mion = createMionRouter();

const routes = {
  mionFetchMetadata,
  // before your own middleware and routes, so a stopped call runs nothing else
  mionSyncRoutes,
  sayHello: mion.route((ctx, name: string): string => `Hello ${name}`),
} satisfies Routes;

const myApi = mion.initRoutes(routes);

export type MyApi = typeof myApi;
Middleware placed before mionSyncRoutes still runs on a stopped call, and so does any middleware with alwaysRun. See Stopping the Request.
Copyright © 2026