vite-pages-react-router
v0.0.5
Published
File system base react-router plugin for Vite
Downloads
8
Maintainers
Readme
vite-pages-react-router
File system routing for React applications base react-router, vite
Getting Started
Install
pnpm add vite-pages-react-router react-router react-router-dom
Vite config
Add to your vite.config.js
:
import pages from "vite-pages-react-router";
export default {
plugins: [
// ...
pages(),
],
};
Overview
By default a page is a
React Router lazy component
exported from a .tsx
, .jsx
, .ts
, .js
file in the src/pages
directory.
You can access the generated routes by importing the ~pages
module in your
application.
import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import routes from "~pages";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<RouterProvider router={createBrowserRouter(routes)} />
</React.StrictMode>,
);
Type
// vite-env.d.ts
/// <reference types="@fourcels/vite-plugin-react-pages/client" />
Route Style
layout.{tsx,jsx,ts,js}
=> layout pagepage.{tsx,jsx,ts,js}
=> index page404.{tsx,jsx,ts,js}
=> not found page_lib
=> private folder (underscore prefix)(layout)
=> Layout Routes[id]
=>:id
Dynamic Segments[[id]]
=>:id?
Optional Segments[...slug]
=>*
Splats{task}
=>task?
Optional Static Segments
Example:
# folder structure
src/pages/
├── (dashboard)
│ ├── [...slug]
│ │ └── page.tsx
│ ├── posts
│ │ ├── [id]
│ │ │ └── page.tsx
│ │ └── page.tsx
│ ├── layout.tsx
│ └── page.tsx
├── about
│ └── [[lang]]
│ └── page.tsx
├── 404.tsx
├── layout.tsx
└── page.tsx