Generated file-based routes for Vite
Generated file-based routes for Vite
Motivation
I enjoyed using file-based routing since I tried Next.js (pages directory). After applying the same concept with Vite and client-side applications, I started writing blog posts covering the implementation of client-side file-based routing with React Router which was packaged later as generouted.
Today generouted brings many features, supports multiple frameworks and routers, and inspires ideas and conventions from Next.js, Remix, Expo, Docusaurus, SvelteKit and more.
How does it work?
generouted uses Vite's glob import API to list the routes within the src/pages directory and generates the routes tree and modals based on generouted's conventions.
There are also Vite plugins available for some integrations to provide type-safe components/hooks/utils through code-generation.
react-router or @tanstack/router or @tanstack/react-location @solidjs/router@mdx-js/rollup installation and setupgenerouted's interactive playground via StackBlitzsrc/pages/ files and see your changes reflectingReact Router
In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.
pnpm add @generouted/react-router react-router// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import generouted from '@generouted/react-router/plugin'
export default defineConfig({ plugins: [react(), generouted()] })// src/main.tsx
import { createRoot } from 'react-dom/client'
import { Routes } from '@generouted/react-router'
createRoot(document.getElementById('root')!).render()Add the home page by creating a new file src/pages/index.tsx → /, then export a default component:
export default function Home() {
return
Home
}Check the routing conventions section below.
You can find more details about type-safe navigation and global modals at @generouted/react-router docs.
Solid Router
In case you don't have a Vite project with Solid and TypeScript, check Vite documentation to start a new project.
pnpm add @generouted/solid-router @solidjs/router// vite.config.ts
import { defineConfig } from 'vite'
import solid from 'vite-plugin-solid'
import generouted from '@generouted/solid-router/plugin'
export default defineConfig({ plugins: [solid(), generouted()] })// src/main.tsx
import { render } from 'solid-js/web'
import { Routes } from '@generouted/solid-router'
render(Routes, document.getElementById('root')!)Add the home page by creating a new file src/pages/index.tsx → /, then export a default component:
export default function Home() {
return
Home
}See more about generouted routing conventions below.
You can find more details about type-safe navigation and global modals at @generouted/solid-router docs.
TanStack React Router — In-progress experimental support
React Location — Deprecated
In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.
pnpm add generouted @tanstack/react-location// src/main.tsx
import { createRoot } from 'react-dom/client'
import { Routes } from 'generouted/react-location'
createRoot(document.getElementById('root')!).render()Add the home page by creating a new file src/pages/index.tsx → /, then export a default component:
export default function Home() {
return
Home
}src/pages.tsx, .jsx and .mdx file extensionssrc/pages/_app.tsx for an app level layoutsrc/pages/404.tsx for a custom not-found pagesrc/pages/index.tsx → /src/pages/posts/index.tsx → /postssrc/pages/posts/2022/index.tsx → /posts/2022src/pages/posts/2022/resolutions.tsx → /posts/2022/resolutionssrc/pages/posts/[slug].tsx → /posts/:slugsrc/pages/posts/[slug]/tags.tsx → /posts/:slug/tagssrc/pages/posts/[...all].tsx → /posts/*_layout.tsx in any nested directory → src/pages/posts/_layout.tsxsrc/pages/posts/ directory in this case, will be wrapped with that layout. between the segments to be converted to forward slashessrc/pages/posts.nested.as.url.not.layout.tsx → /posts/nested/as/url/not/layout()src/pages/(auth)/_layout.tsxsrc/pages/(auth)/login.tsx → /login+ (thinking the modal is an extra route overlaying the current route)useModals() hooksrc/pages/+info.tsx → /infosrc/pages/+checkout/+details.tsx → /checkout/detailssrc/pages/+checkout/+payment.tsx → /checkout/payment- (thinking the segment can be subtracted or removed from the route path)src/pages/-en/about.tsx → /en?/about → /en/about, /aboutsrc/pages/-[lang]/about.tsx → /:lang?/about → /en/about, /fr/about, /about_ will be ignoredsrc/pages/_ignored.tsxsrc/pages/posts/_components/button.tsxsrc/pages/posts/_components/link.tsxexport default Component() {...}export const Loader = () => {...}export const Action = () => {...}export const Pending = () => {...}export const Catch = () => {...}Directory structure
src/pages
├── (auth)
│ ├── _layout.tsx
│ ├── login.tsx
│ └── register.tsx
├── blog
│ ├── _components
│ │ ├── button.tsx
│ │ └── comments.tsx
│ ├── [...all].tsx
│ ├── [slug].tsx
│ ├── _layout.tsx
│ ├── index.tsx
│ └── tags.tsx
├── docs
│ ├── -[lang]
│ │ ├── index.tsx
│ │ └── resources.tsx
│ └── -en
│ └── contributors.tsx
├── +info.tsx
├── 404.tsx
├── _app.tsx
├── _ignored.tsx
├── about.tsx
├── blog.w.o.layout.tsx
└── index.tsxOverview
| File | Path | Convention |
|---|---|---|
(auth)/_layout.tsx |
Pathless Layout group | |
(auth)/login.tsx |
/login |
Regular route |
(auth)/register.tsx |
/register |
Regular route |
blog/_components/button.tsx |
Ignored route by an ignored directory | |
blog/_components/comments.tsx |
Ignored route by an ignored directory | |
blog/[...all].tsx |
/blog/* |
Dynamic catch-all route |
blog/[slug].tsx |
/blog/:slug |
Dynamic route |
blog/_layout.tsx |
Layout for /blog routes |
|
blog/index.tsx |
/blog |
Index route |
blog/tags.tsx |
/blog/tags |
Regular route |
docs/-[lang]/index.tsx |
/docs/:lang?/index |
Optional dynamic route segment |
docs/-[lang]/resources.tsx |
/docs/:lang?/resources |
Optional dynamic route segment |
docs/-en/contributors.tsx |
/docs/en?/contributors |
Optional static route segment |
+info.tsx |
/info |
Modal route |
404.tsx |
* |
Custom 404 (optional) |
_app.tsx |
Custom app layout (optional) |
|
_ignored.tsx |
Ignored route | |
about.tsx |
/about |
Regular route |
blog.w.o.layout.tsx |
/blog/w/o/layout |
Route without /blog layout |
index.tsx |
/ |
Index route |
Via @generouted/react-router or @generouted/solid-router
routes — file-based routes tree/object used by default at `` componentVia @generouted/react-router/lazy or @generouted/solid-router/lazy
No open issues yet, or sync has not completed.