How to Add Links to Starlight's Navbar
Enhancing the navigation of your Starlight-powered site is a great way to improve usability and guide users to important resources. In this guide, I’ll walk you through how to dynamically add links to Starlight’s navbar by creating a custom component and updating your configuration.
Step 1: Create the Links Data File
First, create a JSON file to store the links you want to display in the navbar. Save this file as src/data/links.json
.
This file contains an array of link objects, each with an id
, text
, and link
property.
Step 2: Override the SiteTitle Component
Next, create a custom component to display the links in the navbar. Save this file as src/starlightOverrides/SiteTitle.astro
.
This component:
- Imports the
links.json
file. - Iterates over the links using the
map
function to dynamically generate the list items. - Styles the links and ensures they integrate seamlessly with the existing design.
Step 3: Update the Configuration
Finally, update your astro.config.mjs
file to use the custom SiteTitle
component.
The components
property specifies the custom SiteTitle
component path, overriding the default behavior.
Conclusion
By following these steps, you can easily add dynamic links to Starlight’s navbar. This approach allows you to manage your links in a centralized JSON file and ensures a seamless integration with your site’s design. Happy coding!
← Back to blog