Load data for a page
import { computed, state } from '@markless/core';
import type { PageProps } from '@markless/router';
import { loadReviews } from '../../src/reviews.ts';
export default function Recipe({ params }: PageProps<{ name: string }>) @{
let page = state(1);
const reviews = computed(async () => loadReviews(params.name, page));
<main>
<h1>{params.name}</h1>
@try {
@for (const review of reviews; key review.id) {
<p>{review.text}</p>
}
} @pending {
<p>Loading reviews…</p>
} @catch (error) {
<p>Couldn't load the reviews: {error.message}</p>
}
<button onClick={() => page++}>Older reviews</button>
</main>
}computed(async …) asks for the reviews. @try shows them when they arrive, and @pending holds their place until then. Press Older reviews, and page changes, so the reviews load again.
Keep the database on the server
The reviews load on the server for the first visit. They load again in the browser when page changes. So the code an async value runs goes to the browser too.
A database can't run in a browser. If the page imports one, the build stops with MARKLESS_NODE_IN_BROWSER and lists the imports that lead to it. So give the loading its own file, with one half for each side:
export type Review = { id: number; text: string };
export async function loadReviews(name: string, page: number): Promise<Review[]> {
if (import.meta.env.SSR) {
const { reviews } = await import('../server/db.ts');
const { signedIn } = await import('../server/session.ts');
if (!signedIn()) throw new Error('Sign in to read reviews.');
return reviews(name, page);
}
const response = await fetch(`/api/reviews/${encodeURIComponent(name)}?page=${page}`);
if (!response.ok) throw new Error(await response.text());
return response.json();
}import.meta.env.SSRistrueon the server andfalsein the browser. The build drops the half that can't run there.- Import server files with
await import(…)inside the server half. Animportat the top of the file goes to the browser too, and the build stops. - In the browser, the loader asks an API route for the same data.
The API route answers from the same database:
import type { EndpointHttpContext } from '@markless/router';
import { reviews } from '../../server/db.ts';
import { signedIn } from '../../server/session.ts';
export default async function list({ params, request, url }: EndpointHttpContext<{ name: string }>) {
if (!signedIn(request)) return new Response('Sign in to read reviews.', { status: 401 });
const page = Number(url.searchParams.get('page') ?? 1);
return Response.json(await reviews(params.name, page));
}Here server/db.ts stands in for your database:
import { DatabaseSync } from 'node:sqlite';
import type { Review } from '../src/reviews.ts';
const db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE reviews (id INTEGER PRIMARY KEY, recipe TEXT, text TEXT)');
const add = db.prepare('INSERT INTO reviews (recipe, text) VALUES (?, ?)');
for (const text of ['Lovely and lemony', 'Too sweet for me', 'Made it twice', 'Needs more zest'])
add.run('lemon-cake', text);
export async function reviews(recipe: string, page: number): Promise<Review[]> {
const query = db.prepare('SELECT id, text FROM reviews WHERE recipe = ? ORDER BY id LIMIT 2 OFFSET ?');
return query.all(recipe, (page - 1) * 2) as Review[];
}Check who is asking
A cookie is a small value the browser sends with every request to your site, such as a sign-in. In the browser, fetch to your own site sends the visitor's cookies by itself.
On the server, the loader gets no request to read them from. Turn on the server's request context in vite.config.ts:
import { markless } from '@markless/core/vite';
import { router } from '@markless/router/vite';
import { defineConfig } from 'vite-plus';
export default defineConfig({
plugins: [markless(), router()],
nitro: { experimental: { asyncContext: true } },
});Then useRequest() gives server code the request the page is answering, cookies included:
import { useRequest } from 'nitro/context';
export function signedIn(request: Request = useRequest()): boolean {
return request.headers.get('cookie')?.includes('session=') ?? false;
}The API route passes its own request. The page's server half calls signedIn() with nothing, and gets the page's request. Replace the cookie check with your own sign-in check.
The slow part arrives in the same response
The server sends a page as a stream: one response that keeps arriving in parts.
- Everything outside
@trygoes out at once. @pendingholds the slow part's place.- When the data is ready, the same response carries the
@trycontent.
Reload the recipe, then turn the database dial towards the snail and reload again.
- To do: Press Call Grandalf. The heading changes at once; static fills the screen while he takes his time.
- To do: Change
1500to4000. On the fresh run, the static holds the screen for four seconds, and the heading never waits.
Keep in mind:
- Data that is ready within a few milliseconds goes out with the rest, and
@pendingis never sent. - The router can't wait for all the data first. A slow
@tryalways sends its@pendingcontent first. - On the server, a
fetchto your own API route goes out without the visitor's cookies, so a route that checks who is asking refuses it. Call the server code directly, asloadReviewsdoes.
Coming from another framework?
The router has no loader or action export. A page gets only params, url, status and http as props. Data comes from async values inside the page, and slow parts arrive later in the same response.
Some requests want JSON, not a page. Answer them with API routes.