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%)
Love this project? Help keep it running โ sponsor us today! ๐
Total Downloads
1,174,949
Last Day
4,688
Last Week
26,725
Last Month
108,123
Last Year
938,130
MIT License
1,119 Stars
435 Commits
60 Forks
9 Watchers
10 Branches
23 Contributors
Updated on Feb 12, 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
-10.1%
4,688
Compared to previous day
Last Week
8.4%
26,725
Compared to previous week
Last Month
61.2%
108,123
Compared to previous month
Last Year
306%
938,130
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.