Coder Social home page Coder Social logo

krosf / generouted Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oedotme/generouted

0.0 1.0 0.0 854 KB

Generated file-based routes for Vite

Home Page: https://stackblitz.com/github.com/oedotme/generouted/tree/main/explorer

License: MIT License

Shell 0.25% JavaScript 1.78% TypeScript 97.18% CSS 0.10% HTML 0.70%

generouted's Introduction


generouted


Generouted

Generated file-based routes for Vite

Motivation

I enjoyed using file-based routing since I tried Next.js (pages directory). After applying the same concept with Vite and client-side applications, I started writing blog posts covering the implementation of client-side file-based routing with React Router which was packaged later as generouted.

Today generouted brings many features, supports multiple frameworks and routers, and inspires ideas and conventions from Next.js, Remix, Expo, Docusaurus, SvelteKit and more.


How does it work?

generouted uses Vite's glob import API to list the routes within the src/pages directory and generates the routes tree and modals based on generouted's conventions.

There are also Vite plugins available for some integrations to provide type-safe components/hooks/utils through code-generation.



Features

  • ๐Ÿ“ Client-side file-based routing
  • โšก Powered by Vite
  • โœจ React support with react-router-dom or @tanstack/router ๐Ÿงช or @tanstack/react-location ๐Ÿšจ
  • โœจ Solid support with @solidjs/router
  • ๐Ÿ” Type-safe navigation
  • ๐Ÿš€ Type-safe global modals
  • ๐Ÿ’ค Route-based code-splitting and lazy-loading
  • ๐Ÿ“„ Route-based data loaders and actions
  • ๐Ÿ’ฃ Route-based error boundary
  • ๐Ÿ“‚ Nested layouts
  • ๐Ÿซ™ Pathless layout groups
  • โ“ Optional static and dynamic routes
  • ๐Ÿ’ญ Ignored routes per file or directory

Online explorer

  • โšก Visit generouted's interactive playground via StackBlitz
  • ๐Ÿงฉ Explore file-based routing patterns and conventions
  • ๐Ÿ”Ž Visualize the routes layouts and the resolved routes paths
  • ๐Ÿ“ Update src/pages/ files and see your changes reflecting

Getting started

React Router

React Router

In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.

Installation

pnpm add @generouted/react-router react-router-dom

Setup

// vite.config.ts

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import generouted from '@generouted/react-router/plugin'

export default defineConfig({ plugins: [react(), generouted()] })

Usage

// src/main.tsx

import { createRoot } from 'react-dom/client'
import { Routes } from '@generouted/react-router'

const app = document.getElementById('app')
createRoot(app).render(<Routes />)

Adding pages

Add the home page by creating a new file src/pages/index.tsx โ†’ /, then export a default component:

export default function Home() {
  return <h1>Home</h1>
}

Check the routing conventions section below.

Docs

You can find more details about type-safe navigation and global modals at @generouted/react-router docs.

Examples


Solid Router

Solid Router

In case you don't have a Vite project with Solid and TypeScript, check Vite documentation to start a new project.

Installation

pnpm add @generouted/solid-router @solidjs/router

Setup

// vite.config.ts

import { defineConfig } from 'vite'
import solid from 'vite-plugin-solid'
import generouted from '@generouted/solid-router/plugin'

export default defineConfig({ plugins: [solid(), generouted()] })

Usage

// src/main.tsx

import { render } from 'solid-js/web'
import { Routes } from '@generouted/solid-router'

render(Routes, document.getElementById('app'))

Adding pages

Add the home page by creating a new file src/pages/index.tsx โ†’ /, then export a default component:

export default function Home() {
  return <h1>Home</h1>
}

See more about generouted routing conventions below.

Docs

You can find more details about type-safe navigation and global modals at @generouted/solid-router docs.

Examples


TanStack React Router โ€” In-progress experimental support ๐Ÿงช

TanStack React Router โ€” In-progress experimental support ๐Ÿงช

Check out the docs here

Examples


React Location โ€” Deprecated ๐Ÿšจ

React Location โ€” Deprecated ๐Ÿšจ

In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.

Installation

pnpm add generouted @tanstack/react-location

Usage

// src/main.tsx

import { createRoot } from 'react-dom/client'
import { Routes } from 'generouted/react-location'

const app = document.getElementById('app')
createRoot(app).render(<Routes />)

Adding pages

Add the home page by creating a new file src/pages/index.tsx โ†’ /, then export a default component:

export default function Home() {
  return <h1>Home</h1>
}

Examples



Conventions

File and directories naming and conventions

  • Routes declaration at src/pages
  • Supports .tsx or .jsx extensions
  • Optional src/pages/_app.tsx for an app level layout
  • Optional src/pages/404.tsx for a custom not-found page

Index routes

  • src/pages/index.tsx โ†’ /
  • src/pages/posts/index.tsx โ†’ /posts

Nested routes

  • src/pages/posts/2022/index.tsx โ†’ /posts/2022
  • src/pages/posts/2022/resolutions.tsx โ†’ /posts/2022/resolutions

Dynamic routes

  • src/pages/posts/[slug].tsx โ†’ /posts/:slug
  • src/pages/posts/[slug]/tags.tsx โ†’ /posts/:slug/tags
  • src/pages/posts/[...all].tsx โ†’ /posts/*

Nested layouts

  • By defining _layout.tsx in any nested directory โ†’ src/pages/posts/_layout.tsx
  • Requires using an <Outlet /> component to render layout children
  • All the files within the src/pages/posts/ directory in this case, will be wrapped with that layout

Nested URLs without nested layouts

  • Route file should be outside of the nested layout directory
  • Include dots . between the segments to be converted to forward slashes
  • src/pages/posts.nested.as.url.not.layout.tsx โ†’ /posts/nested/as/url/not/layout

Pathless layouts

  • Similar to nested layouts for layout definition
  • By wrapping the parent directory with parentheses ()
  • src/pages/(auth)/_layout.tsx
  • src/pages/(auth)/login.tsx โ†’ /login
  • Layout parent directory name is not included in the routes paths

Global modals

  • By prefixing the file name with a plus sign + (thinking the modal is an extra route overlaying the current route)
  • Modals navigation available via the useModals() hook
  • src/pages/+info.tsx โ†’ /info
  • src/pages/+checkout/+details.tsx โ†’ /checkout/details
  • src/pages/+checkout/+payment.tsx โ†’ /checkout/payment

Optional segments

  • By prefixing a route segment with a minus sign - (thinking the segment can be subtracted or removed from the route path)
  • src/pages/-en/about.tsx โ†’ /en?/about โ†’ /en/about, /about
  • src/pages/-[lang]/about.tsx โ†’ /:lang?/about โ†’ /en/about, /fr/about, /about

Ignored routes

  • Any directory or file starts with an underscore _ will be ignored
  • src/pages/_ignored.tsx
  • src/pages/posts/_components/button.tsx
  • src/pages/posts/_components/link.tsx

Page exports

  • Required page component export default Component() {...}
  • Optional page loader function export const Loader = () => {...}
  • Optional page action function export const Action = () => {...}
  • Optional error boundary component export const Catch = () => {...}

Example

Directory structure
src/pages
โ”œโ”€โ”€ (auth)
โ”‚   โ”œโ”€โ”€ _layout.tsx
โ”‚   โ”œโ”€โ”€ login.tsx
โ”‚   โ””โ”€โ”€ register.tsx
โ”œโ”€โ”€ blog
โ”‚   โ”œโ”€โ”€ _components
โ”‚   โ”‚   โ”œโ”€โ”€ button.tsx
โ”‚   โ”‚   โ””โ”€โ”€ comments.tsx
โ”‚   โ”œโ”€โ”€ [...all].tsx
โ”‚   โ”œโ”€โ”€ [slug].tsx
โ”‚   โ”œโ”€โ”€ _layout.tsx
โ”‚   โ”œโ”€โ”€ index.tsx
โ”‚   โ””โ”€โ”€ tags.tsx
โ”œโ”€โ”€ docs
โ”‚   โ”œโ”€โ”€ -[lang]
โ”‚   โ”‚   โ”œโ”€โ”€ index.tsx
โ”‚   โ”‚   โ””โ”€โ”€ resources.tsx
โ”‚   โ””โ”€โ”€ -en
โ”‚       โ””โ”€โ”€ contributors.tsx
โ”œโ”€โ”€ +info.tsx
โ”œโ”€โ”€ 404.tsx
โ”œโ”€โ”€ _app.tsx
โ”œโ”€โ”€ _ignored.tsx
โ”œโ”€โ”€ about.tsx
โ”œโ”€โ”€ blog.w.o.layout.tsx
โ””โ”€โ”€ index.tsx
Overview
File Path Convention
(auth)/_layout.tsx Pathless Layout group
(auth)/login.tsx /login Regular route
(auth)/register.tsx /register Regular route
blog/_components/button.tsx Ignored route by an ignored directory
blog/_components/comments.tsx Ignored route by an ignored directory
blog/[...all].tsx /blog/* Dynamic catch-all route
blog/[slug].tsx /blog/:slug Dynamic route
blog/_layout.tsx Layout for /blog routes
blog/index.tsx /blog Index route
blog/tags.tsx /blog/tags Regular route
docs/-[lang]/index.tsx /docs/:lang?/index Optional dynamic route segment
docs/-[lang]/resources.tsx /docs/:lang?/resources Optional dynamic route segment
docs/-en/contributors.tsx /docs/en?/contributors Optional static route segment
+info.tsx /info Modal route
404.tsx * Custom 404 (optional)
_app.tsx Custom app layout (optional)
_ignored.tsx Ignored route
about.tsx /about Regular route
blog.w.o.layout.tsx /blog/w/o/layout Route without /blog layout
index.tsx / Index route

API

Routing

Via @generouted/react-router or @generouted/solid-router

  • <Routes /> โ€” file-based routing component to be render in the app entry
  • <Modals /> โ€” optional file-based modals component to be render in the _app.tsx layout
  • routes โ€” file-based routes tree/object used by default at <Routes /> component

Routing + code-splitting and lazy-loading

Via @generouted/react-router/lazy or @generouted/solid-router/lazy

  • Used instead of @generouted/react-router or @generouted/solid-router to enable lazy-loading
  • Make sure to replace all imports to lazy imports โ€” namely at app entry and src/pages/_app.tsx
  • Provides the same <Routes />, <Modals /> and routes exports

Plugins

Via @generouted/react-router/plugin or @generouted/solid-router/plugin

  • Vite plugin for type generation and initializing type-safe components/hooks/utils
  • Generates src/router.ts file
  • Exports type-safe <Link>, <Navigate>, useModals(), useNavigate(), useParams(), redirect(), etc.
  • Check out @generouted/react-router docs or @generouted/solid-router docs for more details

Core

Via @generouted/react-router/core or @generouted/solid-router/core

  • Available for customization, however it's recommended to use the available integrations directory via the <Routes/> component
  • Check out the custom integration example

License

MIT

generouted's People

Contributors

oedotme avatar agustinbravop avatar akshaypathak avatar aplulu avatar bzbetty avatar vnikolov88 avatar egreb avatar spa5k avatar az33zy avatar binghuis avatar lokhmakov avatar obedm503 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.