Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 BUG: getStaticPaths() doesn't allow to create a top level root #3990

Closed
1 task
jablonski opened this issue Jul 20, 2022 · 2 comments
Closed
1 task
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@jablonski
Copy link
Contributor

What version of astro are you using?

1.0.0-beta.73

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

Linux

Describe the Bug

getStaticPaths() should allow to create a top level root page (e.g. a Frontpage derived from a headless CMS along with all other content).

So if I have /pages/[...slug].astro, the empty slug ("") doesn't work and calling / results in a 404.

export function getStaticPaths() {
  return [
    { params: { slug: "" }, props: { title: "Frontpage" } },
    { params: { slug: "a" }, props: { title: "Page a" } },
    { params: { slug: "a/b" }, props: { title: "Subpage b" } },
  ];
}

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-lspe41?file=src/pages/[...slug].astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added this to Needs Triage in 馃悰 Bug Tracker Jul 20, 2022
@natemoo-re natemoo-re added - P2: nice to have Not breaking anything but nice to have (priority) - P3: minor bug An edge case that only affects very specific usage (priority) s2-medium and removed - P2: nice to have Not breaking anything but nice to have (priority) labels Jul 20, 2022
@natemoo-re
Copy link
Member

Thanks for opening an issue! I know we discussed this on Discord. Seems like a perfectly valid use case鈥攖his would be nice to support.

I know @crutchcorn had a similar request recently.

@jablonski
Copy link
Contributor Author

I've checked again... the "root" slug must be undefined instead of an empty string. The following code works as expected in Astro 1.0.1... maybe it would have worked in earlier versions, too.

Maybe we should document this case, so other people won't get distracted, too.

---
export async function getStaticPaths() {
	const pages = [undefined, "page", "page/subpage"];
	return pages.map((slug) => {
		return {
			params: { slug }, props: { title: `slug: ${slug}` }
		};
	});
}

const { title } = Astro.props
---

<html lang="en">

<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width" />
	<meta name="generator" content={Astro.generator} />
	<title>Astro</title>
</head>

<body>
	{title}
</body>

</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
No open projects
Development

No branches or pull requests

2 participants