Skip to content

Fix slug override with / value#3293

Merged
delucis merged 2 commits intowithastro:mainfrom
HiDeoo:hd-fix-slug-to-param-slash
Jul 14, 2025
Merged

Fix slug override with / value#3293
delucis merged 2 commits intowithastro:mainfrom
HiDeoo:hd-fix-slug-to-param-slash

Conversation

@HiDeoo
Copy link
Copy Markdown
Member

@HiDeoo HiDeoo commented Jul 12, 2025

Description

This PR fixes an issue preventing to override the slug of a page with the slug frontmatter property using the / value.

Huge thanks to @ArmandPhilippot for all the help in debugging this issue and setting up many reproductions pinpointing the problem.

As visible in this StackBlitz example, a dynamic route parameter set to the / value ends being undefined in Astro.params. In Starlight, we have a slugToParam function which does not handle this case.

With Starlight 0.32 and the introduction of route data, we are now always calling getRoute() (which uses getRouteBySlugParam) with the context.params.slug value (whereas before this was only used in SSR). When explicitly setting the slug frontmatter property to /, this results in the getRouteBySlugParam() being called with an undefined parameter but as the slugToParam() function does not handle this case, it's failing to find the route (which instead of having a key of undefined has a key of /).

Another local test to verify the fix:

  1. Run pnpm dev in the docs/ directory.

  2. Delete the docs/src/content/docs/index.mdx file.

  3. Add the following frontmatter field to docs/src/content/docs/reference/configuration.mdx:

    slug: /

Before the fix, opening http://localhost:4321/ would result in a 404 error. After the fix, it correctly displays the configuration reference page.

HiDeoo and others added 2 commits July 12, 2025 12:02
This is mostly an edit to include a co-author I forgot to add in the
previous commit.

Co-authored-by: Armand Philippot <git@armand.philippot.eu>
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jul 12, 2025

🦋 Changeset detected

Latest commit: 68feac2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

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

@netlify
Copy link
Copy Markdown

netlify bot commented Jul 12, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 68feac2
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/68723723918e53000848858b
😎 Deploy Preview https://deploy-preview-3293--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Jul 12, 2025
Copy link
Copy Markdown
Member

@ArmandPhilippot ArmandPhilippot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tested in my repro by updating the code in node_modules and it works, nice finding! 🙌🏽

@kevinzunigacuellar
Copy link
Copy Markdown
Member

Feel free to disregard but I’d like to suggest a slightly more readable version of the slugToParam. I am scared of double ternaries 😬

export function slugToParam(slug: string): string | undefined {
	if (slug === 'index' || slug === '' || slug === '/') return undefined;
	if (slug.endsWith('/index')) return slug.slice(0, -6);
	return slug;
}

@delucis delucis mentioned this pull request Jul 14, 2025
1 task
Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great work investigating and fixing this @ArmandPhilippot & @HiDeoo! LGTM.

@kevinzunigacuellar Definitely feel free to open a refactor PR — always happy to see those 💖

@delucis delucis added 🌟 patch Change that triggers a patch release ✅ approved Pull requests that have been approved and are ready to merge when next cutting a release labels Jul 14, 2025
@delucis delucis merged commit 88f0d34 into withastro:main Jul 14, 2025
16 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jul 14, 2025
HiDeoo added a commit to shubham-padia/starlight that referenced this pull request Jul 15, 2025
* main: (26 commits)
  [ci] release (withastro#3296)
  Fix slug override with `/` value (withastro#3293)
  i18n(fr): update `guides/i18n.mdx` (withastro#3294)
  i18n(ko-KR): update `i18n.mdx` (withastro#3292)
  [ci] release (withastro#3286)
  Revert withastro#3281 (withastro#3291)
  i18n(de): update `guides/i18n.mdx` (withastro#3289)
  Fix Astro i18n config default locale issue (withastro#3288)
  docs: fix `t.exists()` documentation + example (withastro#3287)
  Make targeting sidebar links with CSS a little easier (withastro#3281)
  i18n(fr): update `resources/plugins.mdx` (withastro#3284)
  Extract main padding to CSS custom property  (withastro#3282)
  i18n(de): update plugins translation (withastro#3285)
  i18n(ko-KR): update `plugins.mdx` (withastro#3283)
  Add link to the codeblock-fullscreen plugin (withastro#3279)
  Fix TabItem typo in zh-cn authoring-content.mdx (withastro#3268) (withastro#3269)
  [ci] format
  i18n(ru): update translations (withastro#3270)
  Update `sharp` in docs & examples to latest (withastro#3261)
  Add missing danish UI translations (withastro#3252)
  ...
Yoxnear pushed a commit to Yoxnear/starlight-custom that referenced this pull request Jul 23, 2025
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✅ approved Pull requests that have been approved and are ready to merge when next cutting a release 🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/ can't be replaced anymore

4 participants