A typesafe routing utility for Vike applications. Inspired by TanStack Start.
Forewarned, this is more like a temporary. For me, it's currently a feature-complete solution and I'll be using it for my projects. That's why it's published under a scoped package name.
But this also serves as an experiment for adding this into Vike's core. Vike is planning to make this a built-in feature.
npm install -D @blankeos/vike-routegen
Add the plugin to your Vite config:
// vite.config.ts
import { defineConfig } from "vite";
import vikeRoutegen from "@blankeos/vike-routegen";
export default defineConfig({
plugins: [
// ...
vike(),
vikeRoutegen(), // Has sensible defaults + automatic detection (solid/react/vue), so it can be zero-config.
],
});
Once installed, Vike Routegen will automatically generate a route tree file that provides typesafe routing utilities for your application.
Generates typesafe route URLs based on your Vike pages:
import { getRoute } from "./route-tree.gen";
// Regular route
const homeUrl = getRoute("/");
// π Result: "/"
// Route with params
const postUrl = getRoute("/blog/@slug", {
params: { slug: "my-awesome-post" },
});
// π Result: "/blog/my-awesome-post"
// Route with search params
const searchUrl = getRoute("/search", {
search: { query: "vike", category: "framework" },
});
// π Result: "/search?query=vike&category=framework"
// Catch-all route
const docsUrl = getRoute("/docs/@", {
params: { "@": ["guide", "getting-started"] },
});
// π Result: "/docs/guide/getting-started"
Access route parameters with full type safety:
import { useParams } from './route-tree.gen'
function BlogPost() {
// For routes like /blog/@slug
const { slug } = useParams({ from: '/blog/@slug' }) // π Quick tip: Avoid destructuring like this in SolidJS.
// For catch-all routes like /docs/@
const { '@': segments, '_@': fullPath } = useParams({ from: '/docs/@' }) // π Quick tip: Avoid destructuring like this in SolidJS.
return (
// Your component using the params
)
}
This is designed to work out-of-the-box with Vike and the standard vike extensions, but you can also do the following.
vikeRoutegen({
// Import source for usePageContext (used to generate useParams)
// This is actually auto-detected from your vite.config.ts, but in cases
// where it doesn't detect it correctly, just explicitly write it here like so:
usePageContextImportSource?: "vike-react/usePageContext",
// Output path for the generated file
// Defaults to 'src/route-tree.gen.ts' if 'src' exists, otherwise 'route-tree.gen.ts'
outputPath?: "src/routes.generated.ts",
});
Set usePageContextImportSource
to false
to disable the useParams
utility. If you're not using
one of the standard vike extensions (vike-react, vike-solid, vike-vue).