-
-
Notifications
You must be signed in to change notification settings - Fork 836
Move route data to Astro.locals
#2390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 1820dec The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 site configuration. |
size-limit report 📦
|
|
Really exciting to see this!
With the i18n update didn't we have a somewhat breaking change (possibly that's the
Is there any norm here in Astro-land for 3rd party integrations? This has a potential for name collisions if a user has implemented middleware, correct? If that's the case then I think it would be good to at the very least prefix any locals with |
|
Sharing a few quick thoughts before I forget them as I've been playing with the idea locally too and we can discuss more in depth later.
As this impacts components and not user components, I've been experimenting with a getter function instead that throws if the route data is not available, which would mean it was called in a non-Starlight route.
Definitely a tricky one indeed, been trying a few things but haven't found something that I'm happy with yet too.
As we have seen from the If we compare to labels, in a panel of 15 plugins, only 3 where using |
|
Spent a bit of time revisiting this PR and feeling quite positive about the idea. Made the following adjustments, which helps it feel better:
There’s a type error I’m not 100% sure about — I guess we’re pulling some of our virtual modules into view for type checking that we weren’t previously? Not sure, but haven’t investigate too much. Next up I’d like to tackle the idea of a route data “middleware”/“pipeline” so that users and plugins have a place to plug in and modify stuff. |
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
Astro.localsAstro.locals
|
OK, I think this should be ready! Noting I had to make some small link fixes to Hindi and Indonesian docs pages, so this should be merged with a Lunaria tracking directive in the commit message: Paging @yanthomasdev to confirm that will do what I want! Edit: we landed on this directive to be safe: |
HiDeoo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work and such a great set of changes. Lots of future improvements to come for everyone thanks to this PR 🤩
|
OMG, you guys have actually done it! I can't put it into words how excited I am! 🎉🍾🎂🎈🎁 |
Co-authored-by: Chris Swithinbank <[email protected]>
Co-authored-by: Chris Swithinbank <[email protected]> Co-authored-by: HiDeoo <[email protected]> Co-authored-by: trueberryless <[email protected]>

Description
Early draft PR exploring moving Starlight’s route data object to
Astro.locals— currently this is passed down viaAstro.propsto all our templating components.There are some tricky nuances here for sure.
For example, middleware runs for all routes on a site, which can include non Starlight pages. For these we can’t generate route data. For now I’ve reflected this in the types for locals and asserted
Astro.locals.routeData!in the components. There’s not really a sensible way to guard that without duplicating a null check in every component, but also it feels like route data should be defined in all cases where Starlight’s<Page>is rendered.The
<StarlightPage>component poses some challenges. Right now you pass some props and it generates route data. I’ve hacked this in by assigning that generated data to locals inside the component for now, but this does mean you can’t transform data for pages created this way with additional middleware. Not sure there’s any way to solve this?One way might be to have a dedicated system of “route data middleware” implemented at the Starlight level instead of using generic Astro middleware, e.g.
We could bundle those in a virtual module and have both Starlight’s
locals.tsand<StarlightPage>use them or something:The current branch rips out the prop drilling entirely. That means any overrides that rely on
Astro.propswill break. Could it be worth doing something to ease migration? Deprecate props but keep them around? Throw an error something like we did forlabels? Something to think about.There’s probably a tidier way to do some of the code — just did the quick and easy thing for now. (For example,
Contentis currently typed as optional in the route data object to make it easy to throw it in where I needed it, without checking all the places that type is used. But could probably tidy that up to have a dedicated separate type.)Can also discuss naming here. So far it’s
Astro.locals.routeData, but there’s probably an argument for something a bit more descriptive likeAstro.locals.starlightRouteor even justAstro.locals.starlightpotentially.To-do