Improve performance of sidebar generation logic#3768
Conversation
Inspired by #3761 but with an implementation that is closer to the existing architecture and avoids some unnecessary code duplication.
🦋 Changeset detectedLatest commit: 212bff1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for astro-starlight ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
size-limit report 📦
|
commit: |
HiDeoo
left a comment
There was a problem hiding this comment.
Looks great to me 🔥 I left a few minor comments but nothing major.
I also tried experimenting with some further improvements regarding some string manipulations we do a lot when trying to find the current entry, e.g. pathsMatch(encodeURI(entry.href), pathname) which calls encodeURI once and neverPathFormatter twice for every entry in the sidebar until it finds the current one. But in the end, it didn't seem to make a significant difference in performance and it's already pretty fast even with large sidebars.
Co-Authored-By: HiDeoo <494699+HiDeoo@users.noreply.github.com>
Co-Authored-By: HiDeoo <494699+HiDeoo@users.noreply.github.com>
HiDeoo
left a comment
There was a problem hiding this comment.
Thanks for the updates, ready to 🚀
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [@astrojs/starlight](https://starlight.astro.build) ([source](https://github.com/withastro/starlight/tree/HEAD/packages/starlight)) | [`0.38.1` → `0.38.2`](https://renovatebot.com/diffs/npm/@astrojs%2fstarlight/0.38.1/0.38.2) |  |  | --- ### Release Notes <details> <summary>withastro/starlight (@​astrojs/starlight)</summary> ### [`v0.38.2`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0382) [Compare Source](https://github.com/withastro/starlight/compare/@astrojs/starlight@0.38.1...@astrojs/starlight@0.38.2) ##### Patch Changes - [#​3759](withastro/starlight#3759) [`f24ce99`](withastro/starlight@f24ce99) Thanks [@​MilesChou](https://github.com/MilesChou)! - Fixes an issue where monolingual sites using a region-specific locale (e.g., `zh-TW`) as the default would incorrectly display base language translations (e.g., `zh` Simplified Chinese) instead of the region-specific ones (e.g., `zh-TW` Traditional Chinese). - [#​3768](withastro/starlight#3768) [`a4c6c20`](withastro/starlight@a4c6c20) Thanks [@​delucis](https://github.com/delucis)! - Improves performance of sidebar generation for sites with very large sidebars </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44Ni4wIiwidXBkYXRlZEluVmVyIjoiNDMuODYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> Co-authored-by: Renovate Bot <renovate@zarantonello.dev> Co-committed-by: Renovate Bot <renovate@zarantonello.dev>

Description
This PR refactors our sidebar generation logic to avoid using
structuredClone()each time a sidebar is created. Especially on large sidebars, the clone can be expensive.#2252 added the current sidebar approach and introduced the structured clone partially to defend against user mutations in component overrides. Since then, we refactored our route data system in #2390, which introduced
klonato create a clone of the entire route data object. This means it is now safe to remove the extrastructuredClone()in the sidebar logic.The new strategy tracks the sidebar entries with
isCurrentmanually so we can patch those in a performant way on a single persistent object without relying on the structured clone to avoid mutations.I haven’t tested on Cloudflare’s docs (mentioned in #3760) but I tested in Starlight’s docs:
I would assume the increase should be even more significant in the CF docs given their high page count and sidebar size. And the fix here is essentially the same as the one tested in cloudflare/cloudflare-docs#28970 —
structuredClone()was removed in both — this PR just avoids introducing some unnecessary code duplication and forking of code paths.