Static files
TutorialServe a directory beside your API: a built front end, its scripts and stylesheets, a favicon. One route does it:
Spindle.Static.directory "dist"That is all a folder needs. Use Static for files your build made, which
are read once when the server starts; for files that change while it runs
– uploads, reports – use Files.
Serving a directory
Section titled “Serving a directory”(* A directory served: public, at the root, so / is its index.html and /style.css its stylesheet. The path is the program's working directory's, so run it from this one:
cd examples && dune exec ./static.exe
The directory is read into memory once, when the server starts -- one whose directory cannot be read does not start, and says why -- and every request is a lookup among what was there: a file changed afterwards needs a restart, and a path that was not there is never a file, whatever it spells. *)
let routes = [ Spindle.Static.directory "public" ]
let () = Spindle.Log.setup (); Eio_main.run @@ fun env -> Spindle.serve env routes"public" is a folder next to where you start the program. The example’s is
examples/public, so start it from examples/ and ask for a file:
$ cd examples && dune exec ./static.exe$ curl -I localhost:8080/style.cssHTTP/1.1 200 OKcontent-type: text/css; charset=utf-8accept-ranges: bytesetag: "4e3597b273bb16778f1093c5780a9fe2"cache-control: no-cachex-content-type-options: nosniffcontent-length: 88.../ is the directory’s index.html, and a path with no file is the
framework’s 404.
- The directory is read into memory once, when the server starts. A
request is a lookup among those files, never a path on disk, so
../reaches nothing. A file you change afterwards is served after a restart. - A directory that cannot be read stops the server: it logs why and
exits 1, instead of answering
404to everything. - It is a route like any other, over the rest of the path, so it goes in the same list as your API and every other route is matched first.
- Every file carries an
etag, so a browser revalidates cheaply (304), and oneRangeis answered with206. - Every file carries
X-Content-Type-Options: nosniff, so a browser takes the type it is given and never guesses one from the bytes. - A precompressed copy is served when the browser accepts it. Beside
app.js, anapp.js.br,app.js.zstorapp.js.gzyour build made is sent withContent-EncodingandVary: Accept-Encoding. See Compression. - Names that start with a dot are skipped, at any depth, so
.envand.gitare never served. The one exception is.well-knownat the root, which belongs to the site (RFC 8615).~dotfiles:trueserves them all.
Static files beside an API
Section titled “Static files beside an API”A built front end at the root, beside the API, with a page of its own for what is not there:
let routes = api @ [ Spindle.Static.directory ~not_found:"/404.html" "dist" ]
let () = Eio_main.run @@ fun env -> Spindle.serve env routesThe options of Static.directory. The files and prefixes they name are
request paths, starting with / ("/404.html", not "404.html"); a file
that is not in the directory stops the server at start:
~not_found– the file answered, with a404, for a path with no file.~shell:(file, prefixes)– answers every path under the prefixes with one document and a200, for client-side pages whose ids are made at run time:~shell:("/app/index.html", [ "/orders/" ]). A prefix ends in/, so/ordersheetis not under/orders/.~index– the file a directory answers with,"index.html"unless told.~at– serve under a prefix instead of the root:~at:Path.(s "static").~types– add content types by extension:[ (".wasm", "application/wasm") ]. An unknown extension is served as bytes.
Keeping assets a year
Section titled “Keeping assets a year”Every file is served no-cache: a browser keeps its copy and asks again
each time, and is answered 304 while the file is the same. That is right
for any file, and you need nothing more.
A front-end build names its scripts and stylesheets by a hash of what is in
them – Vite’s assets/index-3f2a9c.js, Astro’s _astro/ – so a new
version is a new name. Those a browser can keep without asking. Say so for
the whole app, apart from what serves them:
Spindle.serve env ~middleware:[ Spindle.Cache.forever ~under:Spindle.Path.[ s "assets" ] ] (api @ [ Spindle.Static.directory "dist" ])Cache.forever keeps every successful GET under those paths a year –
/assets/..., never /assetsheet –
whichever route answered it, and never a failure. Leave it out and nothing
is ever stale; put a prefix in it only when every name under it changes with
its bytes. Your index.html never goes in it: it is how a browser finds the
new names.
Static files in a test
Section titled “Static files in a test”Spindle.Test.app refuses Static.directory, which reads its files when a
real server starts. In a test, load the directory yourself and serve it with
Static.route:
match Spindle.Static.load Eio.Path.(Eio.Stdenv.cwd env / "public") with| Ok files -> Spindle.Test.app (routes @ [ Spindle.Static.route files ])| Error e -> Alcotest.fail eFiles that change at run time
Section titled “Files that change at run time”For files that change while the server runs – uploads, generated reports –
use Spindle.Files, which opens the file when the request arrives and
streams it:
Spindle.Files.directory ~at:Path.(s "uploads") ~download:true "var/uploads"- It never leaves its directory. A segment that is
.., starts with a dot, or holds a/or\is404before anything is opened, and a symlink out of the directory is refused by the operating system. - Nothing lists a directory: one is
404, or its~indexwhere you name one. - An upload never runs as your site. A file a browser would open as a
page of your origin – HTML, SVG, any XML – is sent as an attachment and
carries
Content-Security-Policy: sandbox, so an uploadedx.htmlis downloaded rather than run beside your cookies.~inline_active:trueshows one in the browser instead, still sandboxed: with no origin of its own and none of its script run. ~download:truesends each file as an attachment with its name;~dotfiles:trueserves names that start with a dot.- Answers carry an
etag,Last-Modifiedandnosniff, and answer conditional requests and oneRange, asStaticdoes. - Replace a file by writing it elsewhere and renaming it over the old
one. A file rewritten in place, at the same size within the clock’s tick,
keeps its
etag.
Spindle.Test.app refuses Files.directory too. Spindle.Files.route takes
the same options over a directory you already hold as an Eio.Path.t, so a
test can serve one of its own:
Spindle.Files.route Eio.Path.(Eio.Stdenv.cwd env / "uploads").
