Pages: sites in TSX, TypeScript or EJS
Put a .tsx, .jsx, .ejs or .md file in a pb_pages folder next to pb_data, and your app serves it as a web
page. Each file is a route. Components import each other, a .ts file runs (a page's code beside it, or an endpoint), and
page code reads and writes your records directly. Every part of a page can await, and the app keeps serving other
requests while it waits. This is Pages, built into PocketOne.
esbuild is built into the pocketone binary, so TSX and TypeScript compile the first time a page is used: no Node, no
npm install, no bundler, no build step. It's on for every PocketOne app whenever a pb_pages folder exists. No folder,
no pages.
A product page in TSX
The page is an async component: it awaits its record, then renders it with another component.
// pb_pages/products/[id].tsx
import Price from './_private/Price'
export default async function Product({ ctx }: any) {
const product = await ctx.app.findRecordById.async('products', ctx.params.id)
return (
<article>
<h1>{product.getString('name')}</h1>
<Price cents={product.getInt('price')} />
</article>
)
}
// pb_pages/products/_private/Price.tsx
export default ({ cents }: { cents: number }) => <span class="price">${(cents / 100).toFixed(2)}</span>
/products/<id> now answers with <article><h1>Mug</h1><span class="price">$12.50</span></article>. Edit any of the
files and reload: the change shows up with no restart.
await anywhere
.async twins of Go functions (findRecordById.async, countRecords.async, ...) do their work
outside the JS VM, so the app serves other requests while a page waits.
-
Components: any component can be
async. Sibling components run at the same time, so these two counts load in parallel:async function Count({ collection }: { collection: string }) { const n = await $app.countRecords.async(collection) return <b>{n}</b> } export default () => <p><Count collection="orders" /> orders, <Count collection="users" /> users</p> -
Loaders and handlers:
load,get,postand the rest in a page's.tsor an endpoint, and+middlewarecan beasync. -
EJS: a tag can
await:<% const n = await ctx.app.countRecords.async('orders') %>. -
Sync stays free: a page without
awaitruns exactly as before.
Loading data, EJS and JSX and TSX have the details.
JSX renders to plain HTML on the server. There's no virtual DOM and no client-side React, so pair it with htmx,
Datastar or plain <script> tags for interactivity. TypeScript types are stripped, not checked; run tsc --noEmit or
rely on your editor. JSX and TSX has the details.
Prefer EJS? Hello world
-
Make a folder for your app with a page in it:
mkdir -p myapp/pb_pages && cd myapp echo '<%= `Hello, world!` %>' > pb_pages/index.ejs -
Create a superuser, then start the app:
pocketone superuser upsert [email protected] <password> --dir ./pb_data pocketone serve --dir ./pb_data -
Open http://127.0.0.1:8090. You see
Hello, world!
Edit the file and reload the browser. Changes show up right away, with no restart.
To start from something bigger, pocketone pages starters lists the starter kits and
pocketone pages create myapp --starter <kit> makes a new app from one.
What's covered
- Files are routes:
pb_pages/about.ejsserves/about, andpb_pages/blog/index.ejsserves/blog. - Templates: TSX, JSX, EJS and Markdown, mixed freely (an EJS page can sit in a TSX layout), with layouts,
partials, middleware and JSON routes. A route's code is a
.tsfile (about.tsbesideabout.ets, or an endpoint likefeeds/rss.ts);.jsfiles are served to the browser. - Leftovers go on: a URL no page matches goes on to
pb_public, then to PocketBase. URLs under/api/and/_/always belong to PocketBase. - Errors: in dev mode you get detailed error pages. Otherwise visitors see a plain error page and the details go to the log.
- Not with
--jsvm goja: pages run in PocketOne's own JS engine, so they're off when you serve with PocketBase's.
To send your pages to a running app, see Deploy and live sync.
Change it
To serve pages from another folder, pass --pagesDir to pocketone serve, or set server.pages_dir in
pocketone.yaml (pocketone.yaml reference). Settings for the site itself, such as which
Pages plugins to load, go in pb_pages/+config.js (+config.js).
Learn more
The full Pages docs:
- Installation and starter kits
- JSX and TSX
- Creating a page and routing
- Loading data
- Layouts and slots
- Authentication
- Going live
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.