๐Ÿ“ฆ aquaticcalf / file-system-router

a file based routing system for react router

โ˜… 6 stars โ‘‚ 0 forks ๐Ÿ‘ 6 watching โš–๏ธ MIT License
basedfilereactrouterrouting
๐Ÿ“ฅ Clone https://github.com/aquaticcalf/file-system-router.git
HTTPS git clone https://github.com/aquaticcalf/file-system-router.git
SSH git clone git@github.com:aquaticcalf/file-system-router.git
CLI gh repo clone aquaticcalf/file-system-router
sam sam v1.1.1 ad36937 10 months ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“„ .gitignore
๐Ÿ“„ index.d.ts
๐Ÿ“„ index.jsx
๐Ÿ“„ license
๐Ÿ“„ package-lock.json
๐Ÿ“„ package.json
๐Ÿ“„ readme.md
๐Ÿ“„ README.md

file system router

installation :

npm install file-system-router

usage in your app :

// App.jsx
import { BrowserRouter as Router } from 'react-router-dom'
import { FileSystemRouter } from 'file-system-router'

// vite users:
const pages = import.meta.glob('./pages/**/*.jsx', { eager: true })

// webpack users:
// const pages = require.context('./pages', true, /\.jsx$/)

export default function App() {
  return (
    <Router>
      <FileSystemRouter pages={pages} />
    </Router>
  )
}

file structure convention :

pages/
  index.jsx           -> /
  about.jsx           -> /about
  blog/
    index.jsx         -> /blog
    [id].jsx          -> /blog/:id
  author/
    [author_name]/
      name.jsx        -> /author/:author_name/name

features :

  • automatically converts files to routes
  • supports dynamic parameters with [param].jsx
  • supports nested routes based on your file and directory structure, [param]/example.jsx
  • handles index routes with index.jsx

for your page components :

// pages/blog/[id].jsx
import { useParams } from 'react-router-dom'

export default function BlogPost() {
    const { id } = useParams()
    return <div>blog post : {id}</div>
}

// pages/author/[author_name]/name.jsx
import { useParams } from 'react-router-dom'

export default function AuthorName() {
    const { author_name } = useParams()
    return <div>author name : {author_name}</div>
}