Pages

2 min·Assumes: components

A page is a .tsrx file in pages/, and its path there is the
it answers.
export default function About() @{
	<h1>About the shop</h1>
}

The file's path, pages/about.tsrx, makes the address /about. Its default export is what the browser shows there.

A file's path is its address

Each file in pages/ is a route: one address your app answers.

  • index.tsrx answers /, and about.tsrx answers /about and /about/.
  • A folder adds a part: blog/[slug].tsrx answers /blog/ plus one part.
  • An address with no file shows 404.tsrx, with status 404.

Drag a file into pages/, then out again.

pages/index.tsrxblog//HomeAbouthello404about.tsrx[slug].tsrxhello
An editor window shows a pages/ folder holding index.tsrx and an empty blog/ folder. Two files wait on the desk: about.tsrx and [slug].tsrx. Beside it, a browser shows the address /. Drag about.tsrx into pages/, or press it, and the browser goes to /about and shows the About page. Drag [slug].tsrx into blog/ and the browser goes to /blog/hello, with hello pencilled over [slug]. Type in the address bar to change the slug. Take a file back out, and its address shows a 404 page.

[slug] is a blank the address fills

A bracket in a file name is a param. Type a slug in the drawing's address bar to fill it.

import type { PageProps } from '@markless/router';

export default function Post({ params }: PageProps<{ slug: string }>) @{
	<h1>Post {params.slug}</h1>
}
  • /blog/hello shows Post hello.
  • docs/[...slug].mdx catches one part or more, as one string like guide/intro.
  • A plain name beats [param], and [param] beats [...rest].

A page can answer with a status

Every page gets four props: params, url, status and http. Set http.response.status to answer 404 for a missing invoice.

import type { PageProps } from '@markless/router';
import { findInvoice } from '../../src/invoices.ts';

export default function Invoice({ params, http }: PageProps<{ id: string }>) @{
	const invoice = findInvoice(params.id);
	if (!invoice) http.response.status = 404;

	<h1>{invoice ? invoice.title : 'No such invoice'}</h1>
}

Set it before anything the page waits on, because the status leaves with the first bytes.

http exists only on the server, so read it in the page body, never in a handler (why the build stops).

Next, link between your pages so the build checks every link.