Skip to Content
Reference

Page requests

How the built program answers a page request

Static files are handled before routing and do not go through OnRequest (which files go where: Static files): files under /assets/ carry Cache-Control: public, max-age=31536000, immutable, files from the public folder carry no-cache and an ETag, and only GET and HEAD are answered. The ETag of a public file is the content hash that tifa build wrote into build.json, since the copy embedded in the program has no modification time. A request with a matching If-None-Match gets 304, also for HEAD. The ETag is strong, and weak (W/"…") when the file goes out compressed, see Compression. tifa dev writes no hashes, and the files it serves keep their modification time. A file under /assets/ that is not in the browser bundle is then looked up in the public folder, so public/assets/logo.svg is still /assets/logo.svg. Any other path under /assets/ of an app with pages gets a plain text 404 with Cache-Control: no-store, without OnRequest, a Load or a render, such as a chunk of an older build that a page opened before a deploy asks for.

A GET for a page URL, after OnRequest:

Request Answer
No _tifa Runs every Load on the matched chain concurrently. Then the render memo and the ETag decide whether the VM runs: a matching If-None-Match gets 304, a memo hit sends the kept page, and a miss renders once for all requests that miss the same key at the same time (see The render memo, and Query parameters for the URL the VM renders)
As above, with SSR = false on the matched chain Skips the VM and returns a minimal document: asset tags and the injected data (the results of the Loads, each with its JSON text in json, and the session of the visitor in session), in <script type="application/json" id="tifa-data">. The browser renders it without requesting data again
Has _tifa=<build ID>.<digits>[.p] Runs only the Load or Layout of the routes with a 1 (tifa.Use is deduplicated within the request) and answers 200 with {"routes": {route ID: result}, "session": ...}, each route with its own status. Answers 409 when the build ID is not the current build, and 400 when the value is not of this form or the digits are not as many as the Loads on the matched chain. Errors and redirects are described below

A page with Cache in its route.go is answered like any page, except that GET, HEAD and the requests with _tifa are anonymous and the HTML says what a shared cache in front of the program may keep. The Loads still run on every request, and X-Tifa-Render and the ETag are those of the render memo. See Cache for CDNs for the rules and Cache-Control of an answer for the header of each kind of answer. The framework adds the default Cache-Control in one place, when the final header goes out.

Pages and JSON answers carry Content-Length when they go out uncompressed.

On errors, a Load error first goes back to OnRequest through next, and the framework handles it only after OnRequest returns:

  • When a Load fails, next returns the outermost error among the Load functions that ran, for both full-page and data requests (a data request runs only the listed routes, so it is the outermost among those). Successful Load functions write nothing to the response, so middleware can reject the whole request based on this.
  • OnRequest returns this error unchanged (wrapping it once with %w also counts): only then does the framework classify each error. Unexpected errors go to OnError, and when several Load functions share the same error through tifa.Use, it goes there only once. A full-page request renders the whole page with all results and the status code of the outermost error (404, the tifa.Error status code, or for an unexpected error the result of OnError), and the router shows the ErrorBoundary at the matching level. If the outermost error is a redirect, the answer is that redirect. A data request answers 200 with {"routes": {route ID: result}}, each entry with its own status.
  • OnRequest returns a different error: the middleware wins, and the error is handled on its own terms (redirects, ErrNotFound and tifa.Error are handled directly, anything else goes to OnError). A full-page request gets a simple error page written by Go. A data request gets {"error": message} and the status code, with no per-route data. If OnRequest wrote a response itself, the framework writes nothing more.
  • When a data request hits a redirect for the whole request (OnRequest returns tifa.Redirect, or the URL is written in a non-canonical form), it answers 200 with {"redirect", "status"} and does not send a 3xx. The URL in this JSON, and the location of a route whose Load redirects, is the one a 3xx would carry: a relative URL such as ../list is resolved against the path of the request, as http.Redirect does.
  • Unmatched URL, for a GET or HEAD of an app with pages: Go finds the deepest Layout the URL matches (the rule in 404) and sets the parameters of its path on the request. next(c) runs the Layout functions from the root down to it and returns tifa.ErrNotFound. A full-page request renders with their results and status 404, or with the status of a Layout that failed, as on a page. A navigation data request runs the listed ones among them and answers their results with 404, or with 200 when one failed, as on a page. Listing a route that is not among them answers 400. Other methods, and apps without pages, get the JSON 404.
  • A VM render failure (including a component throwing during render: the string renderer swallows it into a Suspense boundary and leaves a <!--$!--> marker, and the SSR entry treats the render as failed when it sees this marker) counts as an unexpected error. It goes to OnError once and is never stored in the render memo. The page then renders again with the error placed in the route tree, see the next point. When no try renders, Go writes a simple error page with the status and the public message, which does not expose the internal error. Under tifa dev (DevMain) that page also names the request, shows the whole error and loads the dev client, which reloads it once a rebuild serves a fix. The error OnError gets is the one the code threw, also when React swallowed it, with the first 64 frames of its stack trace given in the source files by dist/ssr.js.map. When the code called require, which beastjs does not have, the error also names the package and the place, and says what to do: import the package from a .client file, call it from a useEffect, or use a build of the package that does not need Node. When the bundle fails as it loads, before any page renders, the import itself fails and moving the call into a useEffect does not help, so the error of the prewarm says to import the package from a .client file or to use another build. A component that suspends on work that never settles, by throwing a promise or as a React.lazy component that does not load, fails the render the same way, since the server has no timers and does no I/O. Its error says to give the page its data with Load and import the component statically, or to set SSR = false. The server does not see why a lazy import failed. A page that shows an error of its Load functions, a 404 included, goes through the same tries when its render fails.
  • With a failure, the SSR entry reports levels: how many of the matched routes, from the deepest one it shows up to the root route, could show the error. The server renders again with error: {status, message, level} in the input, the status and the public message from OnError. The entry then replaces the match level routes above the deepest one shown with a failed match holding a TifaError, before the router dehydrates, so that route's errorComponent shows the error and the browser hydrates the same tree. The first try has level 0 and each failed try moves one level up. The answer has the status of OnError, Cache-Control: no-store and no ETag. Under tifa dev the dev client shows the error over the page. A failed render retires its VM, so each try runs on a fresh one. All tries share the deadline ssr.timeout after the first render started, and they stop when one runs out of time, finds every VM busy, or the request is canceled, as does the first render. A render that used up ssr.vm_memory reports no levels, so the server tries as many as the page has routes, plus two for the root route and an added route. A failure that is neither stops at once.
  • When the router writes the URL differently (/%61 as /a), the VM returns a redirect and the server answers with that redirect with status 308, without an ETag. When the URL matches a page, the render memo keeps the redirect as it keeps a page, so the next request for that URL does not render. The redirect puts back the query parameters the render did not see, as the request had them. A trailing slash (/a/ as /a) is redirected too, but such a URL matches no page and renders the 404 of a Layout, so its redirect is not kept, like a 404. The URL is only another spelling of the page, so the redirect is permanent: search engines take a permanent redirect as the canonical URL and not a temporary one, and 308 keeps the method. ServeMux writes 307 for a path it cleans (/a//b, /a/./b) or completes with a slash, and the server answers 308 for those too. A redirect a Load or OnRequest asks for with tifa.Redirect keeps the status it gave. Rendering depends only on the build ID, the SSR bundle, the URL it renders and the data, so the same ETag never belongs to both a 200 and a redirect, and the client never gets an ETag that would turn this redirect into a 304.
  • Non-GET requests, API routes and apps without pages still answer JSON.

Sessions in answers

An app that calls tifa.SetSession sends the session of the request to the browser as {"signedIn": bool, "data"?: ...}, where data is what the Exposed method of the session type returns (the manifest tells the server the type, and the generated tifaapp/app.go registers it as Session: server.SessionOf[T], so a program that starts with sessions in its store knows the type before the first request. The build fails when T cannot be written there: it must be an exported named struct type, not generic. A request that holds a session when no type is registered fails with an error). Only answers that are meant for one visitor carry it:

  • A full page: ssr.Input has session (the JSON above), sessionKey (the cookie name and :v1, where the browser keeps its copy) and sessionHead (the script and the style at the start of the head, see Sessions). The render writes data-session="in" or "out" on <html>, passes the session to the browser through the router's dehydrated data (session and sessionKey next to buildId), and hands it to useSession on the server. A page with SSR = false carries session and sessionKey in its boot data, and Go writes <html data-session="in"> or "out" and the head script itself. The ETag and the key of the render memo hash the session, so two visitors never share either unless the browser gets the same session. A page with Cache carries none of this in its HTML, since the HTML is the same for everyone (see Cache for CDNs): the script at the start of the head sets data-session in the browser, and its navigation data carries the session of that browser.
  • Navigation data: {"routes": ..., "session": ...}, also for a 404 and for an answer where a Load failed.
  • A call with X-Tifa-Call: an Output or InputOutput handler answers {"result": ..., "session"?: ...}. An Input handler answers 204, or {"session": ...} with 200 when the call signed someone in or out (SetSession or ClearSession returned without error). A redirect adds session to its JSON in the same case. A handler with no input and no result that wrote nothing answers 200, X-Tifa-Session: 1 and {"session": ...} in that case, and the function of the page reads it. An error answer has none. A request without the header gets the result alone, 204, or the redirect, as before. These answers add Vary: X-Tifa-Call, since the two shapes differ.
  • GET /__tifa/session, also HEAD: the session of the request, Cache-Control: private, no-store, no validator. Every answer of it, errors and 405 and 409 and an early answer of OnRequest included, is Cache-Control: private, no-store without ETag or Last-Modified. It answers 409 with X-Tifa-Build when the header X-Tifa-Build names another build, 405 for other methods, and 404 in an app without sessions. It runs inside OnRequest with the pattern /__tifa/session.

The browser numbers each of these requests as it sends it, and takes the session of the answer to the latest request sent (js/runtime/session.tsx). data-session is reserved on <html>: htmlAttributes that set it throw.