Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
generouted
Advanced tools
Generouted - Generated Routes
Generated file-based routes for React Location and Vite
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.
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.
src/pages
directory.tsx
extensionssrc/pages/_app.tsx
(optional)src/pages/404.tsx
(optional)<Link />
componentsrc/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/*
loader
from a page: export const loader = async () => ({...})
<Outlet />
component to render its childrenAdd a layout for all the routes within src/pages/posts
directory by adding src/pages/posts.tsx
layout:
src/pages/posts/index.tsx
→ /posts
src/pages/posts/2022/index.tsx
→ /posts/2022
src/pages/posts/[slug].tsx
→ /posts/:slug
Replace regular file name with directory nesting by adding dots, it will be converted to forward slashes:
src/pages/posts.nested.as.url.not.layout.tsx
→ /posts/nested/as/url/not/layout
If you have an existing Vite project setup with React you can skip this section and go to the installation section.
Otherwise you can scaffold a new Vite project with React and TypeScript:
npm create vite@latest react-file-based-routing -- --template react-ts # npm 7+
npm install generouted react-location
Render the <Routes />
component from generouted
at the app entry (you'd mostly wrap it with other providers/components):
// src/main.tsx
import { render } from 'react-dom'
import { Routes } from 'generouted'
const container = document.getElementById('app')!
render(<Routes />, container)
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>
}
<Routes />
That's the only export at the moment. Could add some customization options in the future.
MIT
FAQs
Generated client-side file-based routes for Vite
We found that generouted demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.