Gathering detailed insights and metrics for generouted
Gathering detailed insights and metrics for generouted
Gathering detailed insights and metrics for generouted
Gathering detailed insights and metrics for generouted
npm install generouted
Typescript
Module System
Node Version
NPM Version
TypeScript (97.56%)
JavaScript (1.69%)
HTML (0.66%)
CSS (0.09%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1,155 Stars
435 Commits
60 Forks
8 Watchers
10 Branches
23 Contributors
Updated on Jul 14, 2025
Latest Version
1.20.0
Package Id
generouted@1.20.0
Unpacked Size
37.27 kB
Size
8.76 kB
File Count
9
NPM Version
10.9.2
Node Version
23.7.0
Published on
Feb 07, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Generated file-based routes for Vite
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.
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 reflectingIn case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.
1pnpm add @generouted/react-router react-router
1// vite.config.ts 2 3import { defineConfig } from 'vite' 4import react from '@vitejs/plugin-react' 5import generouted from '@generouted/react-router/plugin' 6 7export default defineConfig({ plugins: [react(), generouted()] })
1// src/main.tsx 2 3import { createRoot } from 'react-dom/client' 4import { Routes } from '@generouted/react-router' 5 6createRoot(document.getElementById('root')!).render(<Routes />)
Add the home page by creating a new file src/pages/index.tsx
→ /
, then export a default component:
1export default function Home() { 2 return <h1>Home</h1> 3}
Check the routing conventions section below.
You can find more details about type-safe navigation and global modals at @generouted/react-router
docs.
In case you don't have a Vite project with Solid and TypeScript, check Vite documentation to start a new project.
1pnpm add @generouted/solid-router @solidjs/router
1// vite.config.ts 2 3import { defineConfig } from 'vite' 4import solid from 'vite-plugin-solid' 5import generouted from '@generouted/solid-router/plugin' 6 7export default defineConfig({ plugins: [solid(), generouted()] })
1// src/main.tsx 2 3import { render } from 'solid-js/web' 4import { Routes } from '@generouted/solid-router' 5 6render(Routes, document.getElementById('root')!)
Add the home page by creating a new file src/pages/index.tsx
→ /
, then export a default component:
1export default function Home() { 2 return <h1>Home</h1> 3}
See more about generouted
routing conventions below.
You can find more details about type-safe navigation and global modals at @generouted/solid-router
docs.
In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.
1pnpm add generouted @tanstack/react-location
1// src/main.tsx 2 3import { createRoot } from 'react-dom/client' 4import { Routes } from 'generouted/react-location' 5 6createRoot(document.getElementById('root')!).render(<Routes />)
Add the home page by creating a new file src/pages/index.tsx
→ /
, then export a default component:
1export default function Home() { 2 return <h1>Home</h1> 3}
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
→ /posts
src/pages/posts/2022/index.tsx
→ /posts/2022
src/pages/posts/2022/resolutions.tsx
→ /posts/2022/resolutions
src/pages/posts/[slug].tsx
→ /posts/:slug
src/pages/posts/[slug]/tags.tsx
→ /posts/:slug/tags
src/pages/posts/[...all].tsx
→ /posts/*
_layout.tsx
in any nested directory → src/pages/posts/_layout.tsx
<Outlet />
component to render layout childrensrc/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.tsx
src/pages/(auth)/login.tsx
→ /login
+
(thinking the modal is an extra route overlaying the current route)useModals()
hooksrc/pages/+info.tsx
→ /info
src/pages/+checkout/+details.tsx
→ /checkout/details
src/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
, /about
src/pages/-[lang]/about.tsx
→ /:lang?/about
→ /en/about
, /fr/about
, /about
_
will be ignoredsrc/pages/_ignored.tsx
src/pages/posts/_components/button.tsx
src/pages/posts/_components/link.tsx
export default Component() {...}
export const Loader = () => {...}
export const Action = () => {...}
export const Pending = () => {...}
export const Catch = () => {...}
1src/pages 2├── (auth) 3│ ├── _layout.tsx 4│ ├── login.tsx 5│ └── register.tsx 6├── blog 7│ ├── _components 8│ │ ├── button.tsx 9│ │ └── comments.tsx 10│ ├── [...all].tsx 11│ ├── [slug].tsx 12│ ├── _layout.tsx 13│ ├── index.tsx 14│ └── tags.tsx 15├── docs 16│ ├── -[lang] 17│ │ ├── index.tsx 18│ │ └── resources.tsx 19│ └── -en 20│ └── contributors.tsx 21├── +info.tsx 22├── 404.tsx 23├── _app.tsx 24├── _ignored.tsx 25├── about.tsx 26├── blog.w.o.layout.tsx 27└── index.tsx
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 routing component to be render in the app entryroutes
— file-based routes tree/object used by default at <Routes />
componentVia @generouted/react-router/lazy
or @generouted/solid-router/lazy
@generouted/react-router
or @generouted/solid-router
to enable lazy-loadingsrc/pages/_app.tsx
<Routes />
and routes
exportsVia @generouted/react-router/plugin
or @generouted/solid-router/plugin
src/router.ts
file<Link>
, <Navigate>
, useModals()
, useNavigate()
, useParams()
, redirect()
, etc.@generouted/react-router
docs or @generouted/solid-router
docs for more detailsVia @generouted/react-router/core
or @generouted/solid-router/core
<Routes/>
componentThere are multiple approaches to achieve that. If you prefer handling the logic in one place, you can define the protected routes with redirection handling within a component:
1// src/config/redirects.tsx 2 3import { Navigate, useLocation } from 'react-router' 4 5import { useAuth } from '../context/auth' 6import { Path } from '../router' 7 8const PRIVATE: Path[] = ['/logout'] 9const PUBLIC: Path[] = ['/login'] 10 11export const Redirects = ({ children }: { children: React.ReactNode }) => { 12 const auth = useAuth() 13 const location = useLocation() 14 15 const authenticatedOnPublicPath = auth.active && PUBLIC.includes(location.pathname as Path) 16 const unAuthenticatedOnPrivatePath = !auth.active && PRIVATE.includes(location.pathname as Path) 17 18 if (authenticatedOnPublicPath) return <Navigate to="/" replace /> 19 if (unAuthenticatedOnPrivatePath) return <Navigate to="/login" replace /> 20 21 return children 22}
Then use that component (<Redirects>
) at the root-level layout src/pages/_app.tsx
to wrap the <Outlet>
component:
1// src/pages/_app.tsx 2 3import { Outlet } from 'react-router' 4 5import { Redirects } from '../config/redirects' 6 7export default function App() { 8 return ( 9 <section> 10 <header> 11 <nav>...</nav> 12 </header> 13 14 <main> 15 <Redirects> 16 <Outlet /> 17 </Redirects> 18 </main> 19 </section> 20 ) 21}
You can find a full example of this approach at Render template
You can use the exported routes
object to customize the router or to use hash/memory routers:
1import { createRoot } from 'react-dom/client' 2import { RouterProvider, createHashRouter } from 'react-router' 3import { routes } from '@generouted/react-router' 4 5const router = createHashRouter(routes) 6const Routes = () => <RouterProvider router={router} /> 7 8createRoot(document.getElementById('root')!).render(<Routes />)
MIT
No vulnerabilities found.
No security vulnerabilities found.