[openstreetmap/openstreetmap-website] Rewrite search results URL when scrolling and restore sidebar position (PR #6895)
Arpan Mukherjee
notifications at github.com
Mon Mar 16 07:11:59 UTC 2026
## Summary
This PR updates the search results sidebar so that the URL reflects the user's scroll position and browser back navigation restores the approximate location in the results list.
Previously, when navigating back from a search result, the search page would reload and start from the top of the list.
This change introduces scroll state tracking similar to the behavior implemented for the history page.
---
## How it works
1. **Scroll detection**
- An `IntersectionObserver` watches `.search_results_entry` items inside the sidebar.
- When a result leaves the viewport while scrolling, the URL is updated with:
```
before=<result_key>
```
or
```
after=<result_key>
```
2. **URL state**
- The URL is updated using `OSM.router.replace()` so history is preserved without creating extra entries.
3. **Scroll restoration**
- When returning to `/search?...&before=<key>` via the browser back button:
- search results load normally
- additional result pages are fetched automatically if needed
- once the target result appears, the sidebar scrolls to it.
4. **Observer safety**
- Scroll restoration temporarily disables the observer to prevent URL updates during the restore process.
---
## Result
Users can now:
1. Search for a place
2. Scroll through multiple result pages
3. Open a result
4. Press browser **Back**
The sidebar will return **close to the previous scroll position** instead of restarting at the top.
---
## Demo
A short video demonstrating the behavior is attached below.
Steps shown:
1. Search `bank`
2. Scroll through multiple result pages
3. Open a result
4. Use browser back button
5. Sidebar restores near the previous scroll position
Uploading Untitled design (1) (1).mp4…
---
## Closes
Fixes #6893
You can view, comment on, or merge this pull request online at:
https://github.com/openstreetmap/openstreetmap-website/pull/6895
-- Commit Summary --
* Update search results URL while scrolling and restore position on browser back
-- File Changes --
M app/assets/javascripts/index/search.js (158)
-- Patch Links --
https://github.com/openstreetmap/openstreetmap-website/pull/6895.patch
https://github.com/openstreetmap/openstreetmap-website/pull/6895.diff
--
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/6895
You are receiving this because you are subscribed to this thread.
Message ID: <openstreetmap/openstreetmap-website/pull/6895 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20260316/2fd5674b/attachment-0001.htm>
More information about the rails-dev
mailing list