Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Static, :param and *rest patterns, and a section table mounted under two parents. Type a path and watch match(), which resolves without loading anything.
nuclo-router is not part of nuclo and must be installed: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 }) }