Beispiele
Praxisnahe Beispiele. Live-Demos.
Öffne ein Beispiel im Vollbild: Live-Vorschau und Quellcode liegen nebeneinander.
Öffne ein Beispiel im Vollbild: Live-Vorschau und Quellcode liegen nebeneinander.
Statische, :param- und *rest-Muster sowie eine Abschnittstabelle, die unter zwei Parents gemountet ist. Tipp einen Pfad ein und sieh zu, wie match() ihn auflöst, ohne etwas zu laden.
nuclo-router gehört nicht zu nuclo und muss separat installiert werden:npm install nuclo nuclo-routerStatic patterns win over dynamic ones, so /blog/new is not a slug. The section table is mounted under two parents with no path written twice.
// npm install nuclo nuclo-router import { createRouter, type RouteTable } from 'nuclo-router' // A reusable section: "./" patterns are relative to the parent. const recordSection: RouteTable = { '/': () => import('./Record.ts'), // the parent path itself './preview': () => import('./Preview.ts'), './preview/raw': () => import('./Raw.ts'), } const router = createRouter({ '/': () => import('./pages/Home.ts'), '/blog/new': () => import('./pages/NewPost.ts'), '/blog/:slug': () => import('./pages/Post.ts'), '/invoices/:id': recordSection, '/customers/:id': recordSection, '/files/*rest': () => import('./pages/File.ts'), '*': () => import('./pages/NotFound.ts'), }) // match() resolves a URL and loads nothing: no import, no history. router.match('/blog/new') // { pattern: '/blog/new', params: {} } static wins router.match('/blog/hello') // { pattern: '/blog/:slug', params: { slug: 'hello' } } router.match('/customers/7/preview/raw') // { pattern: '/customers/:id/preview/raw', params: { id: '7' } } router.match('/files/a/b.txt') // { pattern: '/files/*rest', params: { rest: 'a/b.txt' } } // On a server, a 404 before importing a single chunk: if (!router.match(request.url)) { return new Response(null, { status: 404 }) }