Skip to content
Source
Sign inSign up
TanStack/router

solid-start: Redirecting inside server functions causes a crash - Invariant failed #5620

Send to my agent
Closeds3bba(GitHub profile, opens in a new tab) opened this issue 9 comments

Which project does this relate to?

Start

Describe the bug

Redirecting in server functions or middleware causes a crash in certain situations, the most reliable is when you initially visit the application from a browser url.

For some reason router cannot find a match id for the route and assertion then kills the entire dev server: https://github.com/TanStack/router/blob/b5c96d42cf519eb2a0f60379c3dc65b1e68b77ac/packages/solid-router/src/Match.tsx#L28

Full stack trace:

file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/tiny-invariant/dist/esm/tiny-invariant.js:12
    throw new Error(value);
          ^

Error: Invariant failed: Could not find match for matchId "/". Please file an issue!
    at invariant (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/tiny-invariant/dist/esm/tiny-invariant.js:12:11)
    at Object.s (/home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/solid-router/src/Match.tsx:28:7)
    at state (/home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/solid-router/src/useRouterState.tsx:32:35)
    at eval (/home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/solid-store/src/index.tsx:30:22)
    at file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/store/dist/esm/scheduler.js:35:19
    at Set.forEach (<anonymous>)
    at __notifyListeners (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/store/dist/esm/scheduler.js:34:19)
    at __flush (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/store/dist/esm/scheduler.js:64:9)
    at batch (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/store/dist/esm/scheduler.js:95:9)
    at file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/router-core/dist/esm/router.js:629:19
    at RouterCore.startViewTransition (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/router-core/dist/esm/router.js:740:9)
    at Object.onReady (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/router-core/dist/esm/router.js:625:22)
    at triggerOnReady (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/router-core/dist/esm/load-matches.js:10:27)
    at loadMatches (file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/router-core/dist/esm/load-matches.js:568:26)
    at async file:///home/sebba/work/builds/tanstack-start-solid-invariant-failed-reproduction/node_modules/@tanstack/router-core/dist/esm/router.js:617:13

Node.js v22.21.0
error: script "dev" exited with code 1

Your Example Website or App

https://github.com/s3bba/tanstack-start-solid-invariant-failed-reproduction

Steps to Reproduce the Bug or Issue

Application has two routes / and /about

/ route has a server function that is called in the component and redirects you to /about

  1. Open the / route in the browser
  2. Your dev server should exit with status code 1 and show you Error: Invariant failed: Could not find match for matchId "/". Please file an issue! error

Note: If you go to /about route and click the link that takes you to / route, redirect will work fine and take you back to /about

Expected behavior

Expected behavior is that redirect works regardless of how link was accessed

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.133.27
  • OS: Linux
  • Browser: Chrome (helium), Firefox (zen)
  • Browser Version: Version 0.5.8.1 (Official Build, Chromium 141.0.7390.122), 1.17.3b (Firefox 144.0)
  • Bundler: vite
  • Bundler Version: 7.1.12

Additional context

No response

  1. schiller-manuel(GitHub profile, opens in a new tab) added labelspackage: solid-start

  2. @s3bba , it should work if you set ssr: 'data-only' like below here:

    export const redirectServerFn = createServerFn().handler(async () => {
      const cookie = getCookie("random_missing_cookie");
      if (cookie === undefined) {
        throw redirect({ to: "/about" })
      }
      return "Why do you have this coookie set? The reproduction is for redirect handling...";
    });
    
    export const Route = createFileRoute('/')({
      component: Home,
      ssr: 'data-only',
    })

    Does it solve your issue?

  3. @birkskyum Yes this solves the Invariant failed error, but using data-only means I'm opting out of component rendering on the server right? Ideally I would still like to server render my components with redirect working - or is this not supported?

  4. Right now it only works with data-only for solid, but it's WIP to make it work with ssr: true as well. There's a ticket here

  5. @s3bba , you can set ssr: true if you do this in the query client

    const queryClient = new QueryClient({
      defaultOptions: {
        queries: {
          experimental_prefetchInRender: true,
        },
      },
    });
  6. @birkskyum Cannot confirm this, the invariant failed error still happens with this option for me.

    I was already using this in my project, I did double check on the reproduction example and it also does not make a difference there.

  7. @s3bba , I've added a test for your case in #5668 , and we'll see to getting it working. So far i can see the redicect in the server function works if it's called from a loader. I'll try to see if we can make the solid-query work as well.

  8. @s3bba , we've just now merged a fix that should be resolving this issue, included automated testing. Would you be able to try this with the latest version of all the tanstack solid packages (query, query-ssr, router, start (1.134.10) etc.)?

  9. @birkskyum Yes, I can confirm, invariant error has been fixed on the reproduction example and in my project with SSR enabled. Thanks for the quick fix!

Sign in to join this conversation.