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.
  • Nothing is fetched early when the browser asks to save data, or while the tab is hidden. A mouse press still fetches its page: the click has started.
  • A click cancels the other early fetches still arriving, so they don't slow down the page you asked for.

Choose when pages are fetched early

There are three triggers. Each one runs on 'all' networks, only on 'fast' ones, or 'none':

router({
	prefetch: {
		intent: 'all', // a resting pointer, a focus or a press
		viewport: 'none', // every page link while it's on screen
		idle: 'fast', // once the page is idle, up to six links on screen
		concurrency: 2, // viewport and idle fetches in flight at once
		budget: 2 * 1024 * 1024, // bytes viewport and idle may spend per page
	},
});

These are the defaults. A fast network is one the page's own downloads crossed at 1.6 Mbit/s or better, on a browser that doesn't report a slower one. Turn viewport on for sites whose readers jump around a big menu; every link on screen then costs a request.

One link can choose too. prefetch="intent" keeps it out of the viewport and idle triggers, and prefetch="viewport" fetches it while it's on screen even when viewport is off:

<Link href="/pricing" prefetch="viewport">Pricing</Link>
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.

Button sounds are from Josh Comeau's Button Sounds library.