JSPM

vite-dynamic-pages-router

1.0.6
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 23
  • Score
    100M100P100Q71451F
  • License MIT

Automatic and modular page routing system for Vite + React apps using per-page settings and dynamic import.meta.glob.

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: "/" or index.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