Package Exports
- generouted/core
- generouted/react-location
- generouted/react-router
- generouted/react-router-lazy
- generouted/solid-router
- generouted/solid-router-lazy
Readme
Generouted
Generated file-based routes for Vite
Motivation
I enjoyed working with file-based routing since started using it with Next.js. After trying the same concept with Vite, I started a series of blog posts covering client-side file-based routing with React Router inspired by Next.js. Later, in the last two posts, I replaced React Router with React Location to add more features like data loaders and nested layouts that are inspired by Remix. The final version covered in the blog posts is now published as generouted, see all the available features below.
How
generouted is only one source code file, with no dependencies or build step. It uses Vite's glob import API to list the modules within src/pages directory to be used as React Location's routes.
Why
- Declarative and universal file-based routing system
- Automatically update routes by adding/removing/renaming files at the
src/pagesdirectory - Can be used with any Vite project
- Easier to migrate when switching from or to Next.js
- Route-based code-splitting and pre-loading
- Route-based data loaders
- Route-based actions
Framework support
- React with React Router with type-safe navigation + global modals ๐
- React with TanStack React Router
- React with TanStack's React Location (deprecated)
- Solid with Solid Router with type-safe navigation + global modals ๐
Getting started
In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.
React Router with type-safe navigation + global modals ๐
Installation
pnpm add @generouted/react-router react-router-domSetup
// 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()] })Usage
// src/main.tsx
import { createRoot } from 'react-dom/client'
import { Routes } from '@generouted/react-router'
const container = document.getElementById('app')!
createRoot(container).render(<Routes />)๐ Check more about type-safe navigation and global modals in the plugin docs.
TanStack React Router
React Location (deprecated)
Installation
pnpm add generouted @tanstack/react-locationUsage
// src/main.tsx
import { createRoot } from 'react-dom/client'
import { Routes } from 'generouted/react-location'
const container = document.getElementById('app')!
createRoot(container).render(<Routes />)Solid Router with type-safe navigation + global modals ๐
In case you don't have a Vite project with Solid and TypeScript, check out this getting started guide to start a new project.
Installation
pnpm add @generouted/solid-router @solidjs/routerSetup
// 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()] })Usage
// src/main.tsx
import { render } from 'solid-js/web'
import { Routes } from '@generouted/solid-router'
render(Routes, document.getElementById('app')!)๐ Check more about type-safe navigation and global modals in the plugin docs.
Adding pages
Add the home page by creating a new file src/pages/index.tsx โ /, then export a default component:
export default function Home() {
return <h1>Home</h1>
}See more about generouted routing conventions below.
Features
- File-based routing
- Route-based code-splitting and pre-loading
- Route-based data loaders and actions
- Route-based actions
- Nested layouts
File-based routing
- Next.js inspired
- Files within
src/pagesdirectory - Supports
.jsxand.tsxextensions - Renders page's
defaultexport - Custom app at
src/pages/_app.tsx(optional) - Custom 404 page at
src/pages/404.tsx(optional) - Navigation between routes using the routing library
LinkorAcomponent
Route-based code-splitting and pre-loading
- Enable code-splitting with React Router by using
import { Routes } from '@generouted/react-router/lazy' - Enable code-splitting with Solid Router by using
import { Routes } from '@generouted/solid-router/lazy' - Includes routes components, data loaders and actions
- Pre-loading is only available for TanStack's React Location
Route-based data loaders
- Remix inspired
- By exporting a named function
Loaderfrom a page:export const Loader = async () => ({...}) - React Location's route loaders guide
Route-based actions
- Actions are only available for React Router
- By exporting a named function
Actionfrom a page:export const Action = async () => ({...})
Nested layouts
- Remix inspired
- Adding a layout for a group of routes by naming a file same as their parent directory or using a
_layout.tsxfile inside of the nested directory - Supports data loaders
- Requires
<Outlet />component to render its children
Conventions
Index routes
src/pages/index.tsxโ/src/pages/posts/index.tsxโ/posts
Nested routes
src/pages/posts/2022/index.tsxโ/posts/2022src/pages/posts/2022/resolutions.tsxโ/posts/2022/resolutions
Dynamic routes
src/pages/posts/[slug].tsxโ/posts/:slugsrc/pages/posts/[slug]/tags.tsxโ/posts/:slug/tagssrc/pages/posts/[...all].tsxโ/posts/*
Nested layouts
Enable for all directory routes
Add a layout for all the routes within src/pages/posts directory by adding src/pages/posts.tsx or src/pages/posts/_layout.tsx:
src/pages/posts.tsxorsrc/pages/posts/_layout.tsxsrc/pages/posts/index.tsxโ/postssrc/pages/posts/2022/index.tsxโ/posts/2022src/pages/posts/[slug].tsxโ/posts/:slug
Exclude a route - URL nesting without layout nesting
Add a file outside of the directory with a nested layout, then name the file by adding a dot between each segment, it will be converted to forward slashes:
src/pages/posts.nested.as.url.not.layout.tsxโ/posts/nested/as/url/not/layout
Pathless layout groups ๐
By wrapping a directory name with (): src/pages/(app)/...
src/pages/
โโโ (app)/
โ โโโ _layout.tsx
โ โโโ dashboard.tsx โ /dashboard wrapped by (app)/_layout.tsx
โ โโโ item.tsx โ /item wrapped by (app)/_layout.tsx
โโโ (marketing)/
โ โโโ _layout.tsx
โ โโโ about.tsx โ /about wrapped by (marketing)/_layout.tsx
โ โโโ testimonials.tsx โ /testimonials wrapped by (marketing)/_layout.tsx
โโโ admin/
โโโ _layout.tsx
โโโ index.tsx โ /admin wrapped by admin/_layout.tsxOptional route segments ๐
By prefixing a minus sign - to a segment; meaning this segment can be subtracted/removed from route url:
src/pages/-some/thing.tsxโ/some?/thingsrc/pages/-[param]/another.tsxโ/:param?/another
React Router v6.5.0+ supports regular and dynamic optional route segments:
src/pages/-en/about.tsx โ /en?/about /en/about and /about
src/pages/-[lang]/about.tsx โ /:lang?/about /en/about, /fr/about, /aboutHowever other integration might only support optional dynamic segments:
src/pages/-[lang]/about.tsx โ /:lang?/about /en/about, /fr/about, /aboutIgnored routes - co-locating non-pages files inside the pages directory
Any directory or a file starts with _ will be ignored
src/pages/_ignored.tsxsrc/pages/posts/_components/button.tsxsrc/pages/posts/_components/link.tsx
API
React Location
<Routes />
<Routes /> component accepts all React Location's RouterProps except children, location and routes props.
React Router
<Routes />
No available props.
Solid Router
<Routes />
No available props.
Examples
React Router
TanStack React Router
TanStack React Location
Solid Router
License
MIT