Skip to content

Static assets and the SPA

An application serves one built single-page application (Vite’s output: index.html, content-hashed files under assets/, a few unhashed files such as favicon.svg) from the same origin and process as its JSON API. The HTTP layer does this as the router’s fallback: a request whose path matches no route goes to the SPA, which answers with a file, index.html, or a 404. The rules, and why each exists, are in the static assets and SPA specification; this page is how to use them. The SPA itself is built with the Nuxt UI package.

Behaviour is code. There is no configuration key.

In a module’s boot, next to its routes:

// the build Maven copied into the jar (see "Packaging")
routes.spa(Spa.classpath("fw-spa").build());
// or a local build on disk, during development, without rebuilding the jar
routes.spa(Spa.directory(Path.of("web/dist")).build());

The builder:

Method Default What it does
prefix("/app") / Where the SPA lives. Outside it the router’s normal 404 answers. Vite’s base must match (/app/)
apiPrefix("runtime") none An extra API prefix, for something served outside the router
assetsDirectory("static") assets Vite’s build.assetsDir: its files are immutable, and a missing one is a 404
maxFiles(n), maxBytes(n) 10 000, 64 MiB More is a start-up problem
contentSecurityPolicy(Csp.strict("sha256-...")) or Csp.baseline() Csp.strict() See Security headers

A malformed prefix or segment is an IllegalArgumentException at once. A classpath root must be a file: directory or a directory inside one jar (nested “fat” jars are a start-up problem).

The build is read once, at start-up, and kept in memory with each file’s content type and ETag. Nothing is read per request, so a file written after start-up is not served. Symbolic links are not followed, names starting with . (Vite’s .vite/ manifest, editor files) are not indexed, and at most 10 000 files and 64 MiB are held. Problems with the files or the route table are start-up problems, listed together with every other one.

For a path no route matched, the first row that applies answers:

Request Answer
outside the SPA’s prefix the normal 404 NOT_FOUND envelope
under an API prefix 404, {"error": "NOT_FOUND", "message": "no route for this path"}, no-store, for every method. Never index.html
not GET or HEAD 405 METHOD_NOT_ALLOWED, Allow: GET, HEAD, no-store
an unclean path (.., ., //, a trailing /, a dot file, \, an encoded /, NUL or a control character) 404, no-store
/ (the prefix) or /index.html index.html, no-cache
an indexed file the file. Under assets/: public, max-age=31536000, immutable. Otherwise no-cache
a missing file under assets/, or with a static-asset extension (.js, .css, .png, .json, …) 404, no-store. A stale chunk after a deploy fails as a 404, not as HTML parsed as JavaScript
anything else (a client route such as /orders/7) index.html, 200, so the router’s history mode takes over

Every file carries a strong ETag (the first 16 bytes of the SHA-256 of its bytes, in hex) and a matching If-None-Match (a list, W/ or *) answers 304. HEAD is GET without the body (same Content-Length). Range and If-Modified-Since are ignored: the answer is always the whole file. There is no compression; a proxy or CDN compresses. Requests are labelled (spa) in the access log and the HTTP metrics, never with their path.

A path with a malformed percent-escape is the router’s 400 VALIDATION, before the SPA. Over Jetty, most unclean paths are refused by Jetty’s own checks with 400 before the framework sees them. Either way nothing outside the index is ever read.

A path under an API prefix never gets index.html, so a mistyped or removed API call fails as JSON instead of answering 200 and HTML. Prefixes are matched by whole first segment after the SPA’s prefix (/apiary is a client route, /api/x is not) and are:

  1. the first segment of every route you register (/api/v1/orders gives api, /health/ready gives health);
  2. api and auth, always;
  3. the ones you add explicitly.

The consequence: a client route and a server route never share a first segment. With a route GET /orders/{id}, both /orders and /orders/7/edit are API paths and answer the JSON 404. Put the SPA’s order pages under another first segment (for example /app/orders), or route the API under /api. The start-up log lists the prefixes.

Start-up fails when:

  • a route sits at the SPA’s prefix itself, or its first segment after the prefix is a parameter (/{tenant}/orders, which would make every path an API path);
  • an API prefix equals the first segment of a built file (a route under /assets would hide every asset);
  • a second SPA is mounted (one per application);
  • the root is not a directory, a limit is exceeded, or (Java) the classpath root cannot be read.

With no index.html (the developer built only the server side):

  • when APP_ENV is local or test, start-up logs a warning and every request the SPA would answer gets 404 with a plain-text body telling the developer to run the web build and rebuild. Unknown API paths keep the JSON 404;
  • in any other environment it is a start-up problem: a deployment without its UI must not start and look healthy.

In Java, the route table learns the environment from the HTTP module. A table you create in a test is new Routes("test"); new Routes() knows no environment and is as strict as a deployed one.

Every SPA answer (304, 404 and 405 included) carries:

Header Value
X-Content-Type-Options nosniff (on every framework response)
Referrer-Policy same-origin
X-Frame-Options DENY
Content-Security-Policy the strict policy by default
Cross-Origin-Opener-Policy, Permissions-Policy same-origin; camera=(), microphone=(), geolocation=() (with the strict policy)

The strict policy allows scripts, styles, images (data: too), fonts and connections from the application’s own origin only, plus the hashes you pass for inline snippets, with no framing, base-uri 'none', form-action 'self' and no plugins. An SPA built with the flowcatalystUi() Vite plugin has one inline script, the colour-mode snippet, and the build writes its hash (and that of any other inline script) to csp-hashes.json beside index.html. The SPA support reads that file for its default policy and does not serve it, so there is nothing to copy:

Spa.classpath("fw-spa").build() // strict, with the hashes of the build's csp-hashes.json
// naming hashes yourself replaces the file:
Spa.directory(dist).contentSecurityPolicy(Csp.strict("sha256-AbC+/09=")).build()

A file that is not {"scriptHashes": ["sha256-..."]} (or holds a value that is not a sha256-, sha384- or sha512- hash) is a start-up problem. A policy passed in code replaces the file, and the file is then not read.

The Nuxt UI package runs under the strict policy on two conditions: the SPA is built with the flowcatalystUi() Vite plugin, and Zod is set to jitless before any schema exists (@flowcatalyst/api-runtime does that). See the UI package. A page that cannot run under the strict policy opts out in code, to the baseline policy, which only restricts framing, plugins and <base> (frame-ancestors 'none'; object-src 'none'; base-uri 'self') and sends neither companion header:

Spa.directory(dist).contentSecurityPolicy(Csp.baseline()).build()

In Go that is spa.Options{CSP: spa.Baseline()}.

HSTS is the TLS terminator’s job, and there is no CORS here (see CORS for the API).

The SPA is built by its own toolchain (pnpm --dir web build, Vite writing web/dist/). The server build only copies the output; it never runs Node.

Maven copies the output into the jar as an ordinary resource:

<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>web/dist</directory>
<targetPath>fw-spa</targetPath>
</resource>
</resources>

Listing <resources> replaces Maven’s default, so keep src/main/resources in it. A missing web/dist is skipped by Maven and gives a jar without the SPA: it starts in local and test with the not-built answer and fails start-up anywhere else. CI therefore runs the web build first, then ./mvnw verify.

Run Vite’s dev server (pnpm --dir web dev, port 5173) and let it proxy the API prefixes to the application on its own http.port, plus the HTML navigations /auth/login and /auth/callback, so the sign-in round trip reaches the application. Set auth.public-base-url to the Vite origin so cookies and the OIDC redirect use the page’s origin, as in production. To serve a local build without rebuilding, mount the directory (Spa.directory(Path.of("web/dist")) in Java, os.DirFS("web/dist") in Go).

SpaTesting.assertServesBuild(client) checks a built SPA the way the specification asks every application to: index.html with its caching and security headers, the first hashed asset it references (immutable), a missing asset (404, no-store) and an unknown API path (JSON 404, never HTML). It also fails when an inline script of index.html is not allowed by the policy served, and tells you the hash to use: without it the browser blocks the snippet and the page loses its pre-paint colour mode.

Routes routes = new Routes("test");
routes.forModule("web").spa(Spa.directory(dist).build());
SpaTesting.assertServesBuild(TestClient.of(routes));

With a prefix, SpaTesting.assertServesBuild(client, "/app"). In an application, run it against the real jar’s SPA (Spa.classpath("fw-spa")) in a test that runs after the web build.

The ETag is stored under the name ETag as the specification spells it, where http.Header.Get looks for Etag; a client on the wire sees either.