#Point index.md file in `src/content/info` to `/` instead of `/index`

8 messages · Page 1 of 1 (latest)

restive sapphire
#

I have this file in src/pages/[...slug].astro:

---
import ContentLayout from '@layouts/ContentPage.astro';
import { getCollection, type CollectionEntry } from 'astro:content';

interface Props {
  info: CollectionEntry<'info'>;
}

export const getStaticPaths = async () => {
  const entries = await getCollection('info');

  return entries.map((info) => ({
    params: { slug: info.slug },
    props: { info },
  }));
};

const { info } = Astro.props;
const { Content } = await info.render();
---

<ContentLayout {...info.data}>
  <Content />
</ContentLayout>

Which works okay. If I try to access /about, which has the following structure: (src/content/info/about.md)

---
title: About
pageTitle: About
description: Dummy description for "About" page
---

Dummy about page text

then I see Astro rendering this properly.

However, if I try to access /, which has the following structure: (src/content/info/index.md)

---
title: Home
pageTitle: Home test
description: Home page for my site.
---

## Home
Dummy description for "Home" page

[Dummy link](https://google.com)

I get the following warning in Astro:

04:08:35 [404] / 126ms
04:13:19 [WARN] [router] 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.```

and the user sees a 404 Page not found error.
jagged horizonBOT
#

Hi @restive sapphire. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To resolve this issue and make the index.md file in src/content/info point to / instead of /index, you need to make a few adjustments to your [...slug].astro file. The main problem is that when accessing the root URL /, the slug parameter is empty, which doesn't match any of the entries returned by getCollection('info').

Here's how you can modify your src/pages/[...slug].astro file to handle both the root URL and other pages:

---
import ContentLayout from '@layouts/ContentPage.astro';
import { getCollection, type CollectionEntry } from 'astro:content';

interface Props {
  info: CollectionEntry<'info'>;
}

export const getStaticPaths = async () => {
  const entries = await getCollection('info');

  return entries.map((info) => ({
    params: { slug: info.slug === 'index' ? undefined : info.slug },
    props: { info },
  }));
};

const { info } = Astro.props;
const { Content } = await info.render();
---

<ContentLayout {...info.data}>
  <Content />
</ContentLayout>

The key change is in the getStaticPaths function. We're now mapping the index slug to undefined, which will match the root URL /. This approach is based on the information provided in the Dynamic Routes documentation, which states:

"Setting the rest parameter to undefined allows it to match the top level page."

With this modification, your index.md file should now be accessible at the root URL /, and other pages like /about will continue to work as before.

Remember that you don't need to change anything in your content files. The index.md file can remain in the src/content/info/ directory with its current structure.

#

If you're still experiencing issues after making this change, you might want to check if there are any other routes or configurations in your project that could be interfering with the root URL handling.

Dynamic Routes

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@restive sapphire Kapa.ai is still learning and improving, please let me know how I did by reacting below.

restive sapphire
#

@jagged horizon resolved, thanks

jagged horizonBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You're welcome! I'm glad I could help you resolve the issue with routing your index.md file to the root URL /. If you have any more questions about Astro in the future, feel free to ask. Have a great day!

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: