Spanish Total.js
English
Documentation For AI agents
Contents

Static files

Serve the public folder with minification, caching, compression and ranges, and merge files into one.

.md GitHub
On this page

The public folder

Every request whose URL has an extension is a file request. Total.js looks for it in the public/ folder (CONF.$dirpublic changes the folder): /css/default.css is public/css/default.css. URLs that start with /_<plugin>/ read from the public/ folder of that plugin, so /_shop/logo.png is plugins/shop/public/logo.png. With CONF.$root, the root path is removed before the file is looked up.

Only extensions in CONF.$httpfiles are served; other files return 404. The list contains the usual web types (images, fonts, js, mjs, css, html, json, svg, pdf, mp4, webmanifest and more). Add extensions in the config file:

$httpfiles : avif, glb

A FILE route (see Routing) handles file requests before the public/ folder; it can send any file, or call $.resume() to send the matching file from public/ the normal way. File requests with POST, PUT, PATCH or DELETE are closed without an answer, and HEAD returns only the headers.

Minification and caching

JavaScript, CSS and HTML files are minified when CONF.$minifyjs, CONF.$minifycss and CONF.$minifyhtml are on (the default). Files whose name contains .min, -min or @min are sent unchanged. HTML files are also translated with the language from your LOCALIZE() delegate (see Localization).

In release mode, the minified result is written once to the tmp/ folder and reused. Other files are streamed from disk. The first request reads the file date and size; later requests reuse them, so a browser that sends the same If-Modified-Since date gets 304 Not Modified. Missing files are remembered too and the list of missing files is cleared every minute.

Static responses carry these headers:

HeaderValue
ExpiresOne year ahead (CONF.$httpexpire, refreshed every hour)
Cache-Controlpublic, must-revalidate, max-age= + CONF.$httpmaxage (60 seconds)
Last-ModifiedThe date of the file
ETag858 + CONF.$httpetag (the app version without dots by default)

CONF.$httpcacheoffset (for example '1 minute') moves the Last-Modified date of static files forward by that time; see Configuration.

.zip and .rar files are sent with Cache-Control: private, no-cache, no-store, max-age=0. In debug mode, .js, .css and .html files are read and minified again on every request and sent with the same no-cache header, so you always see your last change.

When you change a file in public/ while the app runs in release mode, clear its cached state with TOUCH():

TOUCH('/css/default.css'); // one file, by its URL
TOUCH();                   // every static file

Compression and ranges

With CONF.$httpcompress (on by default) and a client that accepts gzip, text types are compressed: plain text, JavaScript, CSS, HTML, JSON, XML, SVG, Markdown and notebooks. Text types are sent with charset=utf-8. Images, video and .webmanifest (application/manifest+json) are sent as they are.

A request with a Range header gets 206 Partial Content. A range without an end returns at most CONF.$httprangebuffer kB (5120, so 5 MB), which is how video and audio players stream large files. A range that starts after the end of the file closes the connection; an end after the end of the file is cut to the last byte. A file that was empty when it was first served returns 404 on the next requests until it is touched with TOUCH() or the app restarts.

Merging files

MERGE(url, files...) creates a virtual file made of several files. It returns the URL, which you put in a view or a page.

exports.install = function() {
    MERGE('/css/all.css', 'bootstrap.min.css + default.css');
    MERGE('/js/all.js', '/js/jquery.min.js', '/js/app.js', 'https://cdn.componentator.com/spa.min@20.js');
};

A name without a slash is taken from public/js/ (for .js) or public/css/ (other types); a path that starts with / is relative to public/, ~ starts an absolute path, and http:// or https:// downloads the file. Each local file is minified by the $minify* setting of its type unless its name says it is already minified; downloaded files are added as they are. In release mode the result is built on the first request and saved to tmp/; in debug mode it is built on every request, with a comment before each part.

Made in Europe