vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.89k stars 2.08k forks source link

Enable rewrites function got 404 when running in preview and build mode. #3062

Open HelloYu opened 1 year ago

HelloYu commented 1 year ago

Describe the bug

Below is my rule:

    rewrites: {
    // change /posts/some-folders/some-file.md to some-file.html
    'posts/:any*/:md': ':md',
    },

it works in dev mode, but got 404 when running in Preview mode or build mode, the url created from createContentLoader still has posts prefix but route.path was removed.

export default createContentLoader('posts/**/*.md', {
    excerpt: true,
    transform(raw): Post[] {
        debugger
        return raw
            .map(({ url, frontmatter, excerpt }) => ({
                title: frontmatter.title,
                categories: frontmatter?.categories ?? [],
                tags: frontmatter.tags ?? [],
                url,
                excerpt,
                date: formatDate(frontmatter.date),
            }))
            .sort((a, b) => b.date.time - a.date.time)
    },
})

export function usePosts() {
    const route = useRoute()
    const path = route.path
    function findCurrentIndex() {
        // the url not map correctly that posts created from createContentLoader
        const result = posts.findIndex((p) => p.url.includes(route.path))
        if (result === -1) console.error(`blog post missing: ${route.path}`)
        return result
    }

    const post = computed(() => posts[findCurrentIndex()])
    const nextPost = computed(() => posts[findCurrentIndex() - 1])
    const prevPost = computed(() => posts[findCurrentIndex() + 1])

    return { posts, post, nextPost, prevPost, path }
}

Reproduction

enable rewrites by below rule:

    rewrites: {
    // change /posts/some-folders/some-file.md to some-file.html
    'posts/:any*/:md': ':md',
    },

Expected behavior

mapping to correct url path.

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
    Memory: 19.90 GB / 31.96 GB
  Binaries:
    Node: 18.17.1 - C:\Program Files\nodejs\node.EXE
    npm: 9.6.7 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.7.4 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (117.0.2045.60)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    vitepress: 1.0.0-rc.20 => 1.0.0-rc.20

Additional context

No response

Validations

brc-dd commented 1 year ago

That rule is not valid. It would be trickier to map arbitrarily nested files. I'll share a pattern once I get some time.

HelloYu commented 1 year ago

That rule is not valid. It would be trickier to map arbitrarily nested files. I'll share a pattern once I get some time.

Oh, please help to share a pattern, many thanks.

tracy2zhang commented 1 year ago

That rule is not valid. It would be trickier to map arbitrarily nested files. I'll share a pattern once I get some time.

@brc-dd I encounter the same issue and paths are not arbitrary.Rewrites works just fine in dev mode, but got 404 in preview and build mode. Repo: https://github.com/tracy2zhang/test-vitepress

HelloYu commented 1 year ago

That rule is not valid. It would be trickier to map arbitrarily nested files. I'll share a pattern once I get some time.

@brc-dd I encounter the same issue and paths are not arbitrary.Rewrites works just fine in dev mode, but got 404 in preview and build mode. Repo: https://github.com/tracy2zhang/test-vitepress

this is must fix bug I think, if you want to migration from other blog system, maybe has different url routes, it's the same issue as #3063 this one that I posted, they said it fixed in some merge, but reverted, I hope VitePress teammate could fix this, so I could deploy VitePress blog online.

aiktb commented 11 months ago

I have the same issue: Discuss https://github.com/vuejs/vitepress/discussions/3198

At least development mode and production mode should have the same behaviour so that they both show as 404.