Links

2 min·Assumes: routes are files

A link points at a
in your app, and the build checks that the route exists.
import { Link } from '@markless/router';

export default function Home() @{
	<nav>
		<Link href="/about">About</Link>
		<Link href="/blog/[slug]" params={{ slug: 'hello' }}>
			First post
		</Link>
	</nav>
}

Link renders a real <a>. params fills the brackets, so the build writes the second link as href="/blog/hello".

/blog/[slug] is a route pattern: a file path in pages/, with brackets for the parts that change.

  • A pattern that matches no file in pages/ stops the build.
  • A pattern without its params stops the build and names them.
  • A plain address like /about isn't checked by the build. Your editor flags its typos.

Try both keys in the door.

/blog/[slug]/blgo/[slug]
A pen-and-ink door with a lock, and two keys on the desk beside it, named /blog/[slug] and /blgo/[slug]. Press a key, or drag it to the lock. The /blog/[slug] key goes in, turns, and the door opens. The /blgo/[slug] key stops at the lock and slides back, and the door stays shut.

The typo key is the build error you'd see:

Typed route error: /blgo/[slug] does not match any route in pages/.

A click swaps in only the page

An in-place navigation keeps the page you're on and swaps in only its middle.

  • The address changes when the new middle lands, not at the click.
  • Back and Forward bring back each page and its scroll.
  • Plain <a> links to your pages do the same.

Swap the sheet, then try to pull the clip off.

ShopAbout//about
A pen-and-ink clipboard. Under its clip is a sheet with a coffee bean on it, and a strip of tape on the clip reads /. Press the sheet, drag it aside, or use the arrow keys, and a new sheet with a cup on it takes its place while the label re-inks to /about. The board and clip stay. Try to pull the clip off, and it snaps back.

Early fetches are real requests

A pointer resting on a link, a focus or a press starts fetching the next page before the click. That fetch is a real GET, so turn it off for a link that does something:

<Link href="/logout" prefetch={false}>Log out</Link>
  • The router never fetches other sites or /api/ paths early.
  • Links with download, a target or rel="external" aren't fetched early either.
  • router({ prefetch: false }) in vite.config.ts turns early fetches off everywhere.
import { navigate } from '@markless/router';

export default function Checkout() @{
	<button
		onClick={async () => {
			await fetch('/api/orders', { method: 'POST' });
			await navigate('/orders', { replace: true });
		}}
	>
		Place order
	</button>
}

navigate() resolves once the new page is in place. replace swaps the current history entry instead of adding one, on navigate() and on Link.

prefetch={false} belongs on a link, or on a component that passes it to one (why anywhere else stops the build).

Pages often need data from somewhere slow. Load it in the page.