KScript docs
EN

Routes and APIs

Caching and ETags

Browsers and proxies can keep an answer and ask again only when it may have changed. KScript.Http helps at three levels, with KScript.Cache underneath.

ETags

etag: true on a route, or the etag middleware, adds an ETag header to every 200 answer to GET and HEAD: a hash of the body. When the browser asks again with If-None-Match and the tag still matches, it gets 304 Not Modified with no body, so nothing is sent twice:

<KScript.Routes>
  <import path="controllers/people.kscript" as="people" />

  <Get path="/people" to="people.index" etag="true" cache="60" />
  <Get path="/people/{id}" to="people.show" etag="true" cache="private, max-age=0, must-revalidate" />
</KScript.Routes>
import KScript.Http
import "controllers/people.kscript" as people

Routes {
  Get(path: /people, to: @people.index, etag: true, cache: 60)
  Get(path: /people/{id}, to: @people.show, etag: true, cache: 'private, max-age=0, must-revalidate')
}

The route still runs before its body is hashed. To skip the work as well, a controller may name its version itself with @etag(value). The value can be anything that changes when the answer would, such as the record or its updated_at. When the browser already has that version, the request stops right there with 304:

<import package="KScript.Http" />
<import package="KScript.IO" />

<fn name="show" params="id: int">
  <let name="person" value="@read_json(./data/people.json)->find(fn(p) => $p.id == $id) ?? @abort(404)" />
  <do>@etag($person.updated_at)</do>
  <return value="@view('people/show', person: $person)" />
</fn>
kscript {
  import KScript.Http
  import KScript.IO

  fn show(id: int) {
    $person = @read_json(./data/people.json)->find(fn(p) => $p.id == $id) ?? @abort(404)
    @etag($person.updated_at)
    return @view('people/show', person: $person)
  }
}

@etag(value, weak: true) makes a weak tag. You may also tag a response by hand with ->with_etag() (a hash of its body) or ->with_etag($value).

To tag every route at once, put etag in http.middleware, or in a group:

JSON
{ "http": { "middleware": ["log", "etag"] } }

Cache-Control

cache: on a route, or the cache_control middleware, sets Cache-Control on successful GET answers and on 304s. A number alone is a public max-age in seconds, so cache: 300 gives public, max-age=300. Otherwise, list the directives: public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, must-understand, immutable, max-age=, s-maxage=, stale-while-revalidate= and stale-if-error=. A directive the browser wouldn't understand is an error when the route file loads. A header the controller sets itself, with ->with_cache('no-store'), wins.

Answers kept on the server

cache_response:5m keeps whole 200 answers to GET in KScript.Cache's memory store for that long, by URL, and answers again from memory without running the controller. Answers carry X-Cache: HIT or MISS.

So that one person's answer is never given to another, a request with an Authorization header or any cookie (a session, a login) always runs the controller and is never kept. Neither is an answer that sets a cookie, starts a session (a form's CSRF token does) or is marked private or no-store. That makes cache_response right for public pages and API answers read without cookies, while a signed-in visitor's page is always made fresh.

Edit this page on GitHub