ctx.res

Member
status(code) Status for the rendered page. Returns ctx.res.
header(name, value) Set a response header. header(name) reads it back.
cookie(name, value, options?) Set a cookie.
clearCookie(name, options?) Expire a cookie (same path / domain as when set).
redirect(location, { status = 302, flash }?) or redirect(location, status) Send a redirect. status must be a 3xx code. flash sets a one-time message.
json(data) / json(status, data) Send JSON.
html(html) / html(status, html) Send HTML as-is (no layouts).
text(text) / text(status, text) Send plain text.
file(path) Send a file from pb_pages (path relative to pb_pages).
sse() Start a text/event-stream response.
layout(false) Render this page without layouts (e.g. an HTML fragment for htmx). Returns ctx.res.
sent true once a response has been sent.

Once a response is sent, the later steps of the request (handlers, template, layouts) are skipped.

Status and headers

<% ctx.res.status(404).header('Cache-Control', 'no-store') %>
<h1>Not here</h1>

Redirects

return ctx.res.redirect('/login')
return ctx.res.redirect('/account', { flash: 'Saved' })
return ctx.res.redirect('/new-home', 301) // or { status: 301 }

Cookies

ctx.res.cookie('theme', 'dark', { maxAge: 60 * 60 * 24 * 365 })
ctx.res.cookie('prefs', { compact: true })   // objects are JSON.stringify'd
ctx.res.clearCookie('theme')
  • Non-string values are JSON.stringify'd. The value is then percent-encoded (encodeURIComponent), and ctx.req.cookie decodes it.
  • Defaults: path=/, HttpOnly, SameSite=Lax, and Secure when the request is HTTPS.
  • Options: path, domain, maxAge (seconds), expires (a Date, milliseconds since the epoch, or a date string; with maxAge too, browsers use maxAge), httpOnly, secure, sameSite.

JSON, HTML, text and files

return ctx.res.json({ ok: true })
return ctx.res.json(201, { id: record.id })
return ctx.res.html('<p>Fragment</p>')
return ctx.res.text(404, 'Not found')
return ctx.res.file('downloads/manual.pdf')

Server-sent events

sse({ keepAlive }?) starts a text/event-stream response and returns { send(event, data, { id, retry }?), comment(text), close() }. comment(text) writes an SSE comment (: text), which browsers ignore. keepAlive is a number of seconds: while the handler runs, a comment is written that often, so proxies that close quiet connections (Cloudflare after about 100 seconds) keep the stream open.

// pb_pages/clock.ts
module.exports = {
  get(ctx) {
    const stream = ctx.res.sse()
    stream.send('time', new Date().toISOString())
    stream.close()
  },
}

To keep a stream open, make the handler async and let it wait. While it waits (on a timer or an .async call), the app serves other requests. When the client goes away, the handler stops at its next await and its timers are cleared:

// pb_pages/ticks.ts
module.exports = {
  async get(ctx) {
    const stream = ctx.res.sse({ keepAlive: 30 })
    setInterval(() => stream.send('time', new Date().toISOString()), 1000)
    await new Promise(() => {}) // wait until the client leaves
  },
}

A stream stays open for at most the app's await time (15 minutes unless pocketone.yaml sets js_limits.await_time). EventSource and Datastar reconnect when it ends. To stream record changes, see ctx.realtime.subscribe().

Streaming is only available through sse(). Templates return their text all at once.