fix: prevent dashboard hydration crash from undefined usageStats fields#3
Merged
Merged
Conversation
…ard hydration crash The dashboard layout crashed during hydration with 'Cannot read properties of undefined (reading toLocaleString)' because the usageStats guard only checked the root object, while render code accessed nested fields like usageStats.usage.uploads.toLocaleString(). Two corruption sources triggered this: 1. Stale shapes persisted in localStorage from older schema versions 2. Partial API responses from late.usage.getUsageStats() (the SDK declares every field as optional) Fixed by adding a normalizeUsageStats() function applied at both boundaries: setUsageStats (API entry) and onRehydrateStorage (localStorage entry). Components keep their existing usageStats && guards; the store now guarantees the full shape so nested access is safe.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/dashboard(and every child route) crashed during hydration withCannot read properties of undefined (reading 'toLocaleString').{usageStats && (...)}guard in the dashboard layout only checked the root object, but render code accessed nested fields (usageStats.usage.uploads,usageStats.limits.uploads) that could be undefined.localStorage(zustandpersist) and partial responses from@getlatedev/nodeSDK (which declares everyUsageStatsfield as optional).normalizeUsageStats()insrc/stores/auth-store.ts, applied at both boundaries:setUsageStats(API entry) andonRehydrateStorage(localStorage entry).Why this approach
?.sprinkled across 6 render sites.usageStats &&guards are now sufficient because the store contract is strong.0, missingplanNameto"Free".NaN/Infinity/strings are rejected.What was verified (senior-debugger QA on the diff)
usageStatsaudited — no breaking changesnormalizebeforesetHasHydrated(true))if (!hasHydrated || !apiKey) return null) still works.toLocaleString()on dashboard-tree dataunknowninput + nestedPartialcast correctly express the shapeKnown follow-ups (out of scope for this hotfix)
usageStatsnever refreshes after first login — stale data lingers until user re-enters API key. Should be a separate PR adding an on-mount refetch.limits.uploadsis missing, do we want to render"X / 0"(current) or"X / ∞"(treat missing as unlimited, default to-1)? Current behavior is strictly safer than the crash.normalizeUsageStats— repo has no test infrastructure visible yet.Test plan
latewiz-authfrom localStorage, log in, verify dashboard loadslatewiz-authin localStorage (e.g.{"state":{"apiKey":"sk_x","usageStats":{"planName":"Pro"}}}missinglimits/usage), reload/dashboard— should render without crash, plan info shows0 / 0