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

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

Open
4 tasks done
HelloYu opened this issue Oct 10, 2023 · 5 comments
Open
4 tasks done
Labels

Comments

@HelloYu
Copy link

HelloYu commented Oct 10, 2023

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

@HelloYu HelloYu added the bug: pending triage Maybe a bug, waiting for confirmation label Oct 10, 2023
@brc-dd brc-dd removed the bug: pending triage Maybe a bug, waiting for confirmation label Oct 10, 2023
@brc-dd
Copy link
Member

brc-dd commented Oct 10, 2023

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
Copy link
Author

HelloYu commented Oct 10, 2023

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
Copy link

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
Copy link
Author

HelloYu commented Oct 16, 2023

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
Copy link

aiktb commented Nov 9, 2023

I have the same issue:
Discuss #3198

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

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

No branches or pull requests

4 participants