Skip to content

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

Closed

Description

@s3bba

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:

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

Activity

  1. changed the title [-]Start (solid): Redirecting inside server functions causes a crash - Invariant failed[/-] [+]solid-start: Redirecting inside server functions causes a crash - Invariant failed[/+] on Oct 26, 2025
  2. birkskyum commented on Oct 26, 2025

    @birkskyum
    Member

    @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. s3bba commented on Oct 26, 2025

    @s3bba
    Author

    @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. birkskyum commented on Oct 26, 2025

    @birkskyum
    Member

    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. birkskyum commented on Oct 26, 2025

    @birkskyum
    Member
  6. birkskyum commented on Oct 28, 2025

    @birkskyum
    Member

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

    const queryClient = new QueryClient({
      defaultOptions: {
        queries: {
          experimental_prefetchInRender: true,
        },
      },
    });
  7. s3bba commented on Oct 28, 2025

    @s3bba
    Author

    @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.

  8. birkskyum commented on Oct 28, 2025

    @birkskyum
    Member

    @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.

  9. birkskyum commented on Nov 3, 2025

    @birkskyum
    Member

    @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.)?

  10. s3bba commented on Nov 3, 2025

    @s3bba
    Author

    @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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions