fix(url-sync): don't overwrite URL position params on initial load#2580
fix(url-sync): don't overwrite URL position params on initial load#2580rayanhabib07 wants to merge 2 commits intokoala73:mainfrom
Conversation
Race condition: setupUrlStateSync() called debouncedUrlSync() immediately, but applyInitialUrlState() had already started an async flyTo via setCenter(). At ~250ms the debounce fired while the flyTo was still running, reading the map's default center (lat=20, lon=0, zoom=1) and writing it back over the URL-specified params. Fix: skip the immediate debouncedUrlSync() call when URL position params are present. onStateChanged fires on moveend (after flyTo completes) and handles the first URL write at that point with the correct position. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@rayanhabib07 is attempting to deploy a commit to the Elie Team on Vercel. A member of the Team first needs to authorize it. |
Greptile SummaryThis PR fixes a race condition in Key changes:
Minor edge case (P2): When Confidence Score: 5/5Safe to merge — the fix is narrowly scoped, correctly handles the race condition, and only skips an erroneous initial sync that was clobbering URL params. The single changed file makes a minimal, well-targeted fix. The logic is sound: No files require special attention beyond the one P2 edge case noted on Important Files Changed
Sequence DiagramsequenceDiagram
participant App
participant PanelLayout
participant EventHandlers
participant Map
participant URL
App->>PanelLayout: applyInitialUrlState()
PanelLayout->>Map: setCenter(lat, lon, zoom)
Map-->>Map: flyTo animation starts (async)
App->>EventHandlers: setupUrlStateSync()
EventHandlers->>EventHandlers: urlHasPosition = (lat/lon/zoom in initialUrlState)?
alt urlHasPosition = false (no position params in URL)
EventHandlers->>EventHandlers: debouncedUrlSync() fires after 250ms
EventHandlers->>Map: getCenter() returns default (lat=20, lon=0, zoom=1)
EventHandlers->>URL: replaceState(default position) BUG
else urlHasPosition = true (position params present) NEW BEHAVIOUR
EventHandlers->>EventHandlers: skip debouncedUrlSync()
Note over Map,URL: flyTo animation runs...
Map-->>EventHandlers: onStateChanged fires on moveend
EventHandlers->>EventHandlers: debouncedUrlSync() fires after 250ms
EventHandlers->>Map: getCenter() returns correct position
EventHandlers->>URL: replaceState(correct position)
end
Reviews (1): Last reviewed commit: "fix(url-sync): don't overwrite URL posit..." | Re-trigger Greptile |
| const urlHasPosition = | ||
| this.ctx.initialUrlState?.lat !== undefined || | ||
| this.ctx.initialUrlState?.lon !== undefined || | ||
| this.ctx.initialUrlState?.zoom !== undefined; | ||
| if (!urlHasPosition) { | ||
| this.debouncedUrlSync(); | ||
| } |
There was a problem hiding this comment.
Silent gap when
zoom ≤ 2 with lat/lon present
In applyInitialUrlState() there is an existing guard that skips setCenter() when effectiveZoom <= 2:
if (lat !== undefined && lon !== undefined) {
const effectiveZoom = zoom ?? this.ctx.map.getState().zoom;
if (effectiveZoom > 2) this.ctx.map.setCenter(lat, lon, zoom); // ← skipped when zoom≤2
}When a URL like ?lat=41&lon=0&zoom=1 is loaded:
urlHasPositionistrue(lat/lon/zoom are all defined), sodebouncedUrlSync()is correctly skipped by the new guard.- However, because
effectiveZoom <= 2,setCenter()is never called — no flyTo animation is started, andmoveendnever fires. onStateChangedtherefore never fires from the animation, meaning the first URL write is silently dropped. The URL retains the original params indefinitely until the user manually interacts with the map.
Before this PR the old debouncedUrlSync() call would at least update the URL to the actual map state (even if with the wrong position). After this PR the URL is preserved but permanently out of sync with the map's actual position until the next user interaction.
The practical impact is low — a URL with zoom=1 or zoom=2 alongside explicit lat/lon is an unusual combination, and the user interacting with the map immediately restores consistency. But if precise URL-shareability on initial load is important, you may want to call debouncedUrlSync() explicitly after applyInitialUrlState() when setCenter() was skipped for this reason.
When the URL contains ?view=global&zoom=1.00, setView was called without a zoom override, so flyTo animated to the preset zoom (1.5) instead. The subsequent moveend/onStateChange cycle then wrote zoom=1.50 back to the URL, overwriting the correct zoom=1.00. Pass the URL zoom as the second argument to setView so flyTo targets the correct zoom level. Also remove the effectiveZoom > 2 guard from the lat/lon branch so URL lat/lon is always honoured regardless of zoom level. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Root cause
Race condition in setupUrlStateSync(). applyInitialUrlState() calls
map.setCenter() which starts an async flyTo animation. setupUrlStateSync()
was then immediately calling debouncedUrlSync() — 250ms later the debounce
fired while flyTo was still running, reading the default center
(lat=20, lon=0, zoom=1) and writing it back over the URL params.
Fix
Skip the immediate debouncedUrlSync() call when URL position params
(lat/lon/zoom) are present. onStateChanged fires on moveend after flyTo
completes, at which point getCenter() returns the correct position and
handles the first URL write.
Test plan