Package Exports
- vite-dynamic-pages-router
Readme
vite-dynamic-pages-router
Automatic and flexible file-based routing system for Vite + React.
Just drop .jsx
files inside pages/
and export a settings
object โ no manual route declarations needed.
๐ Features
- ๐ Auto-imports
.jsx
files from/pages/**
- ๐ง Configurable routing with
settings
per page - ๐๏ธ Nested file/folder structure maps directly to routes
- ๐ Route-level access control (supports sync and async functions)
- โ ๏ธ Custom error pages (404, 500, 401) via
errorType
- ๐งฉ Optional tab rendering support
- ๐ Global variable system usable anywhere: React or plain JS
- ๐งผ Clean, minimal setup โ zero boilerplate
- ๐ช Also available as shortcut:
npm i vdp-router
๐ฆ Installation
npm install vite-dynamic-pages-router
or using the shortcut:
npm install vdp-router
๐ File Structure Example
src/
โโโ App.jsx
โโโ pages/
โ โโโ index.jsx # Homepage
โ โโโ about.jsx # /about
โ โโโ admin/
โ โ โโโ panel.jsx # /admin/panel
โ โโโ errors/
โ โโโ Error404.jsx # 404
โ โโโ Error401.jsx # 401
โ โโโ Error500.jsx # 500
๐งฉ Page Format
Each page exports:
- a default React component
- a
settings
object for routing config
function Home() {
return <h1>Welcome Home</h1>;
}
export default Home;
export const settings = {
access: true,
label: "/", // optional
};
โ๏ธ Settings Reference
Setting | Type | Required | Description |
---|---|---|---|
access |
boolean / function |
โ Yes | Enables route. Supports logic and async (e.g. access: async () => {} ). |
label |
string | โ No | Route path. Defaults to filename. Use '/' for homepage. |
tab |
boolean | โ No | Mark page as part of a tab layout. |
errorType |
"404" , "401" , etc. |
โ No | Defines the page as fallback for that error type. |
๐งช Basic Usage in App.jsx
import { PageRouter } from "vite-dynamic-pages-router";
import { BrowserRouter } from "react-router-dom";
const pages = import.meta.glob("./pages/**/*.jsx", { eager: true });
function App() {
return (
<BrowserRouter>
<PageRouter pages={pages} />
</BrowserRouter>
);
}
export default App;
โ ๏ธ Error Pages
Define a 404 page like this:
function NotFound() {
return <h1>404 - Page Not Found</h1>;
}
export default NotFound;
export const settings = {
access: true,
errorType: "404",
};
This will automatically be used for any unknown route and available at /__error/404
.
๐ Global Variables โ One Function to Rule Them All
You can set global values once, and reuse them anywhere โ even in access functions.
โ In React:
import { global } from "vite-dynamic-pages-router";
function Home() {
useEffect(() => {
global("user", { id: 1, role: "admin" });
}, []);
}
โ In another component:
import { global } from "vite-dynamic-pages-router";
function Admin() {
const user = global("user");
return <h1>Welcome, {user?.username}</h1>;
}
โ In access logic:
export const settings = {
access: () => global("user")?.role === "admin",
label: "admin/panel",
};
โ From anywhere (non-React):
import { global } from "vite-dynamic-pages-router";
const token = global("authToken");
No need for context, useState, Zustand, or Providers.
The global()
function uses window.__globals__
internally, so data persists and is available everywhere โ as long as it was set.
๐ Async Access Example
export const settings = {
access: async () => {
await new Promise((res) => setTimeout(res, 100));
return global("user")?.role === "admin";
},
label: "admin/panel",
};
๐งฉ Tabs (Optional)
To use tabs, mark your page with tab: true
:
export const settings = {
access: true,
label: "dashboard",
tab: true,
};
Then fetch all tab pages:
import { getTabPages } from "vite-dynamic-pages-router";
const tabs = getTabPages(pages);
// โ [{ key, label, Component }]
๐ ๏ธ Best Practices
- Use
label: "/"
orindex.jsx
for homepage - Place error pages in
pages/errors/
- Set
global("user", {...})
as early as possible - Protect routes with
access: () => global("user")?.role === "admin"
Made with ๐ by OxiJenuuu