0.6.3 • Published 1 year ago

react-autoroute v0.6.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

React Autoroute

NextJS/RemixJS/SvelteKit inspired auto routing based on filesystem layout using Vite glob imports. Currently supports React Router only. TanStack Router support will be added later when reaching a more mature phase.

Test Deploy npm

Feature Overview

  • Auto glob matching of routes/** (NextJS uses pages/** which is not a good match to layout nesting though)
  • Component centric route file names: routes/settings/PrivacySettings.js => /settings/privacy-settings
  • Index Routes e.g. routes/blog/Index.js/blog or alternatively routes/blog/Blog.js => /blog.
  • Layout Nesting like in RemixJS, not actually nested routes like in NextJS.
  • Dynamic route segments routes/blog/[slug].js/blog/:slug (/blog/hello-world)
  • 404 handling by adding a 404.tsx file at any location.

File Name Conventions

  • Files exporting React components are using camelCase and start with a uppercase letter: UserSettings.tsx
  • Index pages are either named Index.js or are using the parents name e.g. settings/Settings.js

Usage

Example:

/// <reference types="vite/client" />

import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import { createRoutes, modulesToLazyRouteObjects } from 'react-autoroute'

const routes = createRoutes(
  modulesToLazyRouteObjects(
    import.meta.glob<ReactRouterRouteModule>(['/src/routes/**/[\\w$[]*.{jsx,tsx}']),
    '/src/routes/'
  )
)
const router = createBrowserRouter(routes)

function App() {
  return <RouterProvider router={router} />
}

Note: You might wonder: The actual glob import feature of Vite cannot be implemented in the library-space because of unwanted side-effects (e.g. the library creates dependencies to the actual application using it) and produces problems in combination with lazy loading of components/routes.

License

Apache License; Version 2.0, January 2004

Copyright

Copyright 2022-2023Sebastian Software GmbH

0.6.3

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago