Skip to content
Source
Sign inSign up
TanStack/router

preload: undefined context in loader if navigation happens before beforeLoad is done #1886

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

Describe the bug

There's still an issue here when with context when it gets merged with beforeLoad.

export const Route = createFileRoute('/about')({
  beforeLoad: async () => {   
    await new Promise((resolve) => setTimeout(resolve, 5000));
    return { someData: 'hello' };
  },
   loader: async ({context}) => {
    await new Promise((resolve) => setTimeout(resolve, 2000));
    console.log(context.someData)
  },
})

Supposing /about is the lazy route that takes a long time to resolve, if beforeLoad isn't finished before the navigation event happens, the loader gets invoked with an undefined context, breaking the route.

This possibly happens because execution flow is skipped when a route already in beforeLoad fetching state is re-loaded (because the loader gets invoked regardless of the beforeLoad state, for which we have no pending information on reload)

Your Example Website or App

https://stackblitz.com/edit/github-nkiyw7

Steps to Reproduce the Bug or Issue

  1. Hover About link
  2. Immediately click to navigate before the 5000ms beforeLoad is done executing
  3. Undefined context in loader 🤯

Expected behavior

As a user, I expect context to not be undefined

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • Browser: Chrome
  • Version: 1.43.6

Additional context

No response

  1. SeanCassiere added labelsbug

  2. yes we'll close #1891, but I will take over the test from there, so thanks for implementing that!

Sign in to join this conversation.