Skip to content

Invariant failed: Could not find an active match #1368

Closed

Description

@bazo

Describe the bug

i have a router definition that works on version 1.16.0, after upgrading to 1.20.0 - at least that's the version the error has been reported to me, it stopped working. it's still present on 1.22.5

any routes which parent is carRoute or cargoRoute thrown invariant failed error, except the routes at "/" path

the Car or Cargo page at "$id" contains outlet and code that loads a given car or cargo by $id and sets it in a context which the child routes consume

eg: for url http://domain.localhost/cars/1/edit or http://domain.localhost/cars/1/prices i get error Invariant failed: Could not find an active match from "/auth/cars/$id/"

but the http://domain.localhost/cars/1 works even though the detail page is structured the same way as the others

Your Example Website or App

http://localhost

Steps to Reproduce the Bug or Issue

this is the whole router file

import { createRouter, redirect, Outlet, createRoute, createRootRouteWithContext } from "@tanstack/react-router";
import type { QueryClient } from "lib/queryClient";
import type { TrpcClient, TrpcProxyClient, TrpcUtils } from "lib/trpc";
import IndexPage from "pages";
import AuthenticatedPage from "pages/authenticated";
import LoginPage from "pages/login";
import RootPage from "pages/root";
import { Claims } from "types";
import { decode } from "lib/jwt";
import z from "zod";
import CarsIndexPage from "pages/cars";
import { useAuthStore } from "./authStore";
import CarEditPage from "pages/cars/carEdit";
import CarDetailPage from "pages/cars/carDetail";
import CargosIndexPage from "pages/cargos";
import CargoDetailPage from "pages/cargos/cargoDetail";
import CargoEditPage from "pages/cargos/cargoEdit";
import CarCreatePage from "pages/cars/carCreate";
import CargoCreatePage from "pages/cargos/cargoCreate";
import OrdersIndexPage from "pages/orders";
import OrderDetailPage from "pages/orders/orderDetail";
import { SettingsPage } from "pages/settings/settings";
import CarPage from "pages/cars/car";
import CarPricesPage from "pages/cars/carPrices";
import CarCalendarPage from "pages/cars/carCalendar";
import CargoPage from "pages/cargos/cargo";
import CargoPricesPage from "pages/cargos/cargoPrices";
import CargoCalendarPage from "pages/cargos/cargoCalendar";

export interface RouterContext {
	queryClient: QueryClient;
	trpc: TrpcClient;
	utils: TrpcUtils;
	trpcProxy: TrpcProxyClient;
}

// Create a root route
const rootRoute = createRootRouteWithContext<RouterContext>()({
	component: RootPage,
	beforeLoad({ abortController }) {
		return {
			abortController,
		};
	},
});

export const loginRoute = createRoute({
	getParentRoute: () => rootRoute,
	path: "/login",
	component: LoginPage,
	validateSearch: (search) =>
		z
			.object({
				redirect: z.string().optional(),
			})
			.parse(search),
});

export const authenticatedRoute = createRoute({
	getParentRoute: () => rootRoute,
	id: "auth",
	component: AuthenticatedPage,
	beforeLoad: async ({ context, location, navigate }) => {
		const auth = useAuthStore.getState();
		if (auth.user === null || auth.token === null) {
			throw redirect({
				to: loginRoute.to,
				search: {
					// Use the current location to power a redirect after login
					// (Do not use `router.state.resolvedLocation` as it can
					// potentially lag behind the actual current location)
					redirect: location.href,
				},
			});
		}

		const claims = Claims.parse(decode(auth.token));

		if (claims.exp <= new Date()) {
			auth.logout();
			throw redirect({
				to: loginRoute.to,
				search: {
					redirect: location.href,
				},
			});
		}
	},
});

export const indexRoute = createRoute({
	getParentRoute: () => authenticatedRoute,
	path: "/",
	component: IndexPage,
});

export const carsRoute = createRoute({
	getParentRoute: () => authenticatedRoute,
	path: "/cars",
});

export const carsIndexRoute = createRoute({
	getParentRoute: () => carsRoute,
	component: CarsIndexPage,
	path: "/",
});

export const carCreateRoute = createRoute({
	getParentRoute: () => carsRoute,
	component: CarCreatePage,
	path: "/new",
});

export const carRoute = createRoute({
	getParentRoute: () => carsRoute,
	path: "/$id",
	component: CarPage,
});

export const carDetailRoute = createRoute({
	getParentRoute: () => carRoute,
	path: "/",
	component: CarDetailPage,
});

export const carPricesRoute = createRoute({
	getParentRoute: () => carRoute,
	path: "/prices",
	component: CarPricesPage,
});

export const carCalendarRoute = createRoute({
	getParentRoute: () => carRoute,
	path: "/calendar",
	component: CarCalendarPage,
});

export const carEditRoute = createRoute({
	getParentRoute: () => carRoute,
	path: "/edit",
	component: CarEditPage,
});

export const cargosRoute = createRoute({
	getParentRoute: () => authenticatedRoute,
	path: "/cargos",
});

export const cargosIndexRoute = createRoute({
	getParentRoute: () => cargosRoute,
	component: CargosIndexPage,
	path: "/",
});

export const cargoCreateRoute = createRoute({
	getParentRoute: () => cargosRoute,
	component: CargoCreatePage,
	path: "/new",
});

export const cargoRoute = createRoute({
	getParentRoute: () => cargosRoute,
	path: "/$id",
	component: CargoPage,
});

export const cargoDetailRoute = createRoute({
	getParentRoute: () => cargoRoute,
	path: "/",
	component: CargoDetailPage,
});

export const cargoPricesRoute = createRoute({
	getParentRoute: () => cargoRoute,
	path: "/prices",
	component: CargoPricesPage,
});

export const cargoCalendarRoute = createRoute({
	getParentRoute: () => cargoRoute,
	path: "/calendar",
	component: CargoCalendarPage,
});

export const cargoEditRoute = createRoute({
	getParentRoute: () => cargoRoute,
	path: "/edit",
	component: CargoEditPage,
});

export const ordersRoute = createRoute({
	getParentRoute: () => authenticatedRoute,
	path: "/orders",
});

export const ordersIndexRoute = createRoute({
	getParentRoute: () => ordersRoute,
	component: OrdersIndexPage,
	path: "/",
});

export const orderRoute = createRoute({
	getParentRoute: () => ordersRoute,
	path: "/$id",
	component: () => <Outlet />,
});

export const ordersDetailRoute = createRoute({
	getParentRoute: () => orderRoute,
	path: "/",
	component: OrderDetailPage,
});

export const settingsRoute = createRoute({
	getParentRoute: () => authenticatedRoute,
	path: "/settings",
});

export const settingsIndexRoute = createRoute({
	getParentRoute: () => settingsRoute,
	component: SettingsPage,
	path: "/",
});

// Create the route tree using your routes
const routeTree = rootRoute.addChildren([
	loginRoute,
	authenticatedRoute.addChildren([
		indexRoute,
		carsRoute.addChildren([
			carsIndexRoute,
			carCreateRoute,
			carRoute.addChildren([carDetailRoute, carPricesRoute, carCalendarRoute, carEditRoute]),
		]),
		cargosRoute.addChildren([
			cargosIndexRoute,
			cargoCreateRoute,
			cargoRoute.addChildren([cargoDetailRoute, cargoPricesRoute, cargoCalendarRoute, cargoEditRoute]),
		]),
		ordersRoute.addChildren([ordersIndexRoute, orderRoute.addChildren([ordersDetailRoute])]),
		settingsRoute.addChildren([settingsIndexRoute]),
	]),
]);

const router = createRouter({
	routeTree,
	context: {
		queryClient: undefined as unknown as QueryClient,
		trpc: undefined as unknown as TrpcClient,
		utils: undefined as unknown as TrpcUtils,
		trpcProxy: undefined as unknown as TrpcProxyClient,
	},
});

// Register your router for maximum type safety
declare module "@tanstack/react-router" {
	interface Register {
		router: typeof router;
	}
}

export default router;

Expected behavior

it works correctly

Screenshots or Videos

No response

Platform

  • OS: any
  • Browser: any
  • Version: 1.20 and up

Additional context

No response

Activity

  1. schiller-manuel commented on Mar 25, 2024

    @schiller-manuel
    Collaborator

    please provide a minimal complete example, especially where you use the useParams, useMatch etc. hooks.

  2. SeanCassiere commented on Mar 25, 2024

    @SeanCassiere
    Member

    ^ furthering Manuel's comment.

    You can create a minimal reproducible example using one of our quickstart Stackblitz examples:

  3. chinanderm commented on Mar 28, 2024

    @chinanderm

    I'm now running into this issue as well. It happens using the useParams hook to get params from a child route. If the child route isn't active, the hook throws.

  4. schiller-manuel commented on Mar 28, 2024

    @schiller-manuel
    Collaborator

    If the child route isn't active, the hook throws.

    if the child route is not active, there are no params.

    you can use useParams({strict: false}) instead.

  5. chinanderm commented on Mar 28, 2024

    @chinanderm

    If the child route isn't active, the hook throws.

    if the child route is not active, there are no params.

    you can use useParams({strict: false}) instead.

    Thanks, I must have missed this breaking change during an upgrade. I just tried using strict: false and the types do not allow for false, only true | undefined, nor does the "false" value have an effect.

    Here is a repro: https://stackblitz.com/edit/github-qc1kr2?file=src%2Froutes%2Fabout.tsx

  6. chinanderm commented on Mar 28, 2024

    @chinanderm

    @schiller-manuel Nvm.. I re-read the docs to see you cannot combine from and strict. Thanks for the help here -- I can work around this. The previous way this worked was pretty nice, I do admit. This feels like a step backward.

  7. schiller-manuel commented on Mar 28, 2024

    @schiller-manuel
    Collaborator

    that's why I only specified strict above.
    if strict is false you will get the union of all possible params and will have to narrow it down yourself.

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