router icon indicating copy to clipboard operation
router copied to clipboard

Undefined context after redirect with wrapInSuspense at root route (introduced in 1.33.6)

Open Zebeqo opened this issue 1 year ago • 6 comments

Describe the bug

Undefined context after redirect with wrapInSuspense at root route.

It used to work fine in version 1.33.4.

Your Example Website or App

https://stackblitz.com/edit/github-jr3nax-t75gmj?file=src%2Froutes%2F__root.tsx

Steps to Reproduce the Bug or Issue

  1. See the log: context: undefined
  2. Remove wrapInSuspense: true and refresh
  3. See the log: context: {"test":"abc"}

Expected behavior

See the log: context: {"test":"abc"} at step 1

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 125.0

Additional context

No response

Zebeqo avatar May 22 '24 12:05 Zebeqo

can you please check which version introduced this? 1.33.5 most likely did not since it affected @tanstack/start and not the router.

schiller-manuel avatar May 22 '24 18:05 schiller-manuel

can you please check which version introduced this? 1.33.5 most likely did not since it affected @tanstack/start and not the router.

It's likely that the issue was introduced in version 1.33.6. I can confirm that version 1.33.4 was working fine, and version 1.33.6 has the issue. As for version 1.33.5, it doesn't appear to be available on npm.

1.33.4: https://stackblitz.com/edit/github-jr3nax-xxb2ka?file=package.json 1.33.6: https://stackblitz.com/edit/github-jr3nax-uphkrn?file=package.json

Zebeqo avatar May 23 '24 03:05 Zebeqo

I am also facing a similar issue. Everything works fine in version 1.33.4, but in 1.33.6, the loader's context becomes undefined. I haven't specified wrapInSuspense, but it seems to be the same problem.

ornew avatar May 23 '24 09:05 ornew

then it is caused by this PR

https://github.com/TanStack/router/pull/1611

@SeanCassiere @freshgiammi

schiller-manuel avatar May 23 '24 16:05 schiller-manuel

Yeah, looks like this is working when my change is reverted, but I'm not sure reverting the change is the right fix. My patch merely fixes support for wrapInSuspense which got lost when some SSR changes were brought in (where the root route got forced to not suspend), so maybe this was broken before too. Seems we just keep bumping into ctx issues.

Reverting this change restores context after the redirect https://github.com/TanStack/router/commit/e19f7e8703bdc8d6ccbaf3399c82527aabdc56c2#diff-2ac234f610fa0e0c2146252c10a5266af9c5b6d0b63659a0fa4c9f2c4472596a

I wonder if the root cause lays elsewhere 🤔

freshgiammi avatar May 24 '24 08:05 freshgiammi

Yeah, restoring context assignment before beforeLoad reopens https://github.com/TanStack/router/issues/1637 as the replaceEqualDeep loses all dynamic values: then when beforeLoad runs, the already mounted layout component re-renders before the router has a chance to update the route context.

I think what's happening here is updateMatch updating the store too early, causing a rerender with possibly invalid data being fed to already mounted components. I have opened https://github.com/TanStack/router/pull/1662 as a potential fix 👍 (and I'm thinking we need tests around redirects+context to try and detect these kind of issues early on)

freshgiammi avatar May 24 '24 10:05 freshgiammi