withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.65k stars 2.48k forks source link

🐛 BUG: Dynamic pages using rest parameters and generating multiple subfolders not working (404) when running dev server. #1322

Closed jornki closed 2 years ago

jornki commented 3 years ago

What package manager are you using?

npm

What operating system are you using?

Mac

Describe the Bug

Using Astro version 0.20.4

When generating a pages using rest parameters the page will fail with a 404 when running in the dev server. However, when building the project the page is correctly being generated (and works fine with preview).

Structure

/pages /pages/subfolder/[...slug].astro

The page /pages/subfolder/[...slug].astro is generating output pages in multiple sub-levels. The date of the content for the page is being used to construct the path. For instance /subfolder/2021/09/05/name-of-page/index.html.

When running astro build the expected files will be created and everything works. However, when running the dev server all generated pages will fail with a 404.

getStaticPaths

The paths are generated to create subfolders (as seen above) using the year, month and date in this case.

export async function getStaticPaths() {
    const allPosts = await getAllPosts();
    return allPosts.map((post) => {
        const fullSlug = generateSlug(post._createdAt, post.slug.current);
                // fullSlug  example -> «/2021/09/05/the-name-of-the-post»
        return {
            params: { slug: fullSlug },
            props: { post }
        };
    });
}

Steps to Reproduce

  1. npm init astro (using Typescript)
  2. Create the structure described above
  3. Run the dev server «astro dev»
  4. Visit one of the dynamic/generated pages
  5. The page is not found.

Link to Minimal Reproducible Example (Optional)

No response

okikio commented 3 years ago

This is related to #1187

jasikpark commented 3 years ago

I was able to replicate this here: https://codesandbox.io/s/astro-1322-reproduction-48dld?file=/src/pages/subfolder/%5B...slug%5D.astro

Can confirm that the HTML is built only on build, but not when using the dev server.

tony-sull commented 2 years ago

Looks like this was fixed along the way post v0.21 🎉

Repro I threw together to confirm in the lastest Minimal template: https://stackblitz.com/edit/github-my7zu2?file=src/pages/blog/[...slug].astro