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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index page not found for site root (/) when using content collection #7038

Closed
1 task done
oliverlietz opened this issue May 8, 2023 · 3 comments
Closed
1 task done

Comments

@oliverlietz
Copy link

What version of astro are you using?

2.4.2

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

none

What package manager are you using?

npm

What operating system are you using?

Mac

What browser are you using?

any

Describe the Bug

It's not possible to serve the root page (e.g. index.md) from a content collection:

404: Not found
Path: /

Setting slug in front matter to '/' (slash) or '' (empty string) does not work.

npm run dev

> project@0.0.1 dev
> astro dev

  🚀  astro  v2.4.2 started in 388ms
  
  ┃ Local    http://localhost:3000/
  ┃ Network  use --host to expose
  
07:33:44 PM [content] Watching src/content/ for changes
07:33:44 PM [content] Types generated
07:33:44 PM [astro] update /.astro/types.d.ts
07:33:54 PM [getStaticPaths] A `getStaticPaths()` route pattern was matched, but no matching static path was found for requested path `/`.

Possible dynamic routes being matched: src/pages/[...slug].astro.
07:33:54 PM [serve]    404                                        /
07:34:18 PM [astro] reload /src/content/pages/index.md
07:34:18 PM [astro] update /.idea/sonarlint/issuestore/index.pb
07:34:18 PM [astro] update /.idea/sonarlint/securityhotspotstore/index.pb
07:34:18 PM [astro] update /.astro/types.d.ts
07:34:28 PM [getStaticPaths] invalid path param: slug. `undefined` expected for an optional param, but got empty string.
07:34:28 PM [getStaticPaths] A `getStaticPaths()` route pattern was matched, but no matching static path was found for requested path `/`.

Possible dynamic routes being matched: src/pages/[...slug].astro.
07:34:28 PM [serve]    404                                        /
07:34:48 PM [astro] update /.idea/sonarlint/securityhotspotstore/index.pb
07:34:48 PM [astro] update /.idea/sonarlint/issuestore/index.pb
07:34:50 PM [astro] reload /src/content/pages/index.md
07:34:50 PM [astro] update /.astro/types.d.ts
07:47:31 PM [getStaticPaths] A `getStaticPaths()` route pattern was matched, but no matching static path was found for requested path `/`.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-z4igp1-mwgqtc

Participation

  • I am willing to submit a pull request for this issue.
@matthewp
Copy link
Contributor

matthewp commented May 8, 2023

You have to use undefined as the slug to generate an index route. See this example: https://stackblitz.com/edit/github-z4igp1-mwgqtc?file=src%2Fcontent%2Fpages%2Findex.md

@matthewp matthewp closed this as completed May 8, 2023
@matthewp
Copy link
Contributor

matthewp commented May 8, 2023

@oliverlietz
Copy link
Author

oliverlietz commented May 8, 2023

Working:

---
import {getCollection} from 'astro:content';
export async function getStaticPaths() {
    const pages = await getCollection('pages');
    return pages.map(page => ({
        params: {slug: page.slug || undefined}, props: {page},
    }));
}
const {page} = Astro.props;
const {Content} = await page.render();
---

@matthewp, Is it documented already somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants