[FIX-008] Stale URL Rescue
Fixing loader timing issues by using context and explicit link generation.
PRID: 1118
VERIFIED
2 min read
[FIX-008] Sandbox Loader Reading Stale URL & Masked Links
📝 Problem Background
- Stale URL: The Loader runs before the browser's address bar updates, reading the old URL.
- Masked Link: Links generated on the Docs list page were pointing to
/docs/*(a route mask) instead of the specific article path, causing the Loader to receive incorrect input.
🧪 Effort Metrics
- Attempt Count: 5
- Time Spent: 2.5 hours
- Complexity: ⭐⭐⭐⭐ (Compound Failure)
🔍 Root Cause Analysis
Double Fault:
- Link Generation: TanStack Router, when handling a splat route with
path: '/*', sometimes failed to correctly interpolate when using theparamsobject, resulting in a literal*in the generatedhref. - Loader Timing: Even if the link were correct, the Loader could read
window.location(the old value) before the hash updates.
🛠️ Fix Strategy
Pincer Movement:
-
Source Correction (Index Page):
- Abandon using the
paramsobject for this case. - Build the path using an explicit string template:
to={/${lang}/docs/${slug}}. - Ensure the Router receives the fully expanded physical path.
- Abandon using the
-
Endpoint Defense (Loader Page):
- Abandon
window.location. - Use the
location.pathnamefrom the Loader Context (which represents the Router's intended destination). - Combined with the source correction,
location.pathnamewill now contain the correct slug instead of*.
- Abandon
typescript// Fix 1: Link Generation <Link to={`/${lang}/docs/${docFile.slug}`} /> // Fix 2: Loader State loader: ({ location }) => { // Now location.pathname is "/en/docs/core/plan" (Correct!) }
Result: Fully resolved the entire chain from list click to detail page load.