Static files
Serve the public directory, build the URLs of static files, map, merge and resize files, answer with file routes and localize static files.
On this page
The public directory
Every request whose path ends with a file extension is a static-file request. The framework looks for the file in /public/ (the config key directory_public) and serves it when the extension is allowed in static_accepts. Other extensions answer 404. Paths that contain ../ are refused.
/public/css/default.css --> GET /css/default.css
/public/img/logo.png --> GET /img/logo.pngAdd an extension with ACCEPT(), or with static_accepts_custom in the config file:
ACCEPT('.glb', 'model/gltf-binary');static_accepts_custom : glb, usdzWhat happens to a file before it is sent:
.js,.mjsand.cssfiles are minified whenallow_compileis on (default). Files named like*.min.jsor*-min.cssare sent as they are.- Text types (HTML, CSS, JavaScript, JSON, XML, SVG, Markdown, plain text) are compressed with GZIP when
allow_gzipis on and the client accepts it. .htmland.htmfiles are localized whenallow_localizeis on (default).- In release mode, the responses have
Cache-Control: public, max-age=11111111, anExpiresheader one year ahead,Last-Modified, and anETagbuilt frometag_version. A request with a matchingIf-Modified-Sincegets 304. In debug mode, files are sent withno-cache.
The framework keeps the processed files in /tmp/. TOUCH(url) forgets one cached file, and TOUCH() forgets all:
TOUCH('/css/default.css');URLs of static files
F.public_js(), F.public_css(), F.public_image() and the others build URLs from the config keys static_url_script, static_url_style, static_url_image, and so on. They also apply the /versions file, so you can change the URL of a file without changing your views:
F.public_js('app.js'); // /js/app.js
F.public_image('logo.png'); // /img/logo.png// /versions
/js/app.js : /js/app-2.js
/css/style.css : autoauto adds a hash of the file content to its name when the application starts. PATH.public(filename) gives the absolute path of a file in the public directory.
Mapping, merging and resizing
// A URL served from another file or directory
MAP('/favicon.ico', '~/assets/favicon.ico');
// Several files served as one
MERGE('/js/bundle.js', '/js/a.js', '/js/b.js');
// /img/small/*.jpg is made from /public/img/*.jpg
RESIZE('/img/small/*.jpg', function(image) {
image.resize(200, 200);
image.quality(80);
}, ['/img/']);File routes
A file route answers static-file requests with your own function. Use ROUTE('FILE ...') or FILE(). The action receives req and res, not a controller:
exports.install = function() {
ROUTE('FILE /docs/*.pdf', pdf, ['#apikey']);
ROUTE('FILE /thumbnails/*.jpg', thumbnail);
ROUTE('FILE /favicon.ico', PATH.public('img/favicon.ico'));
};
function pdf(req, res) {
res.file(PATH.root('documents/' + U.getName(req.uri.pathname)), 'document.pdf');
}
function thumbnail(req, res) {
res.image(PATH.public('photos/' + U.getName(req.uri.pathname)), function(image) {
image.resize(150, 150);
});
}The URL can end with *.ext (one extension), * or *.* (any file under the path), or a full file name. When the second argument is a string, the route sends that file (an absolute path) with res.file(), or proxies an http(s):// URL.
In a file route, use res.file(), res.image(), res.filefs(), res.imagefs(), res.stream(), or res.continue() to serve the file from public/ as usual. Middleware in file routes goes in the flags array (['#name']). FILE404(fn) handles static files that do not exist:
FILE404(function(req, res) {
if (req.extension === 'png') {
res.file(PATH.public('img/empty.png'));
return true;
}
});Localized static files
LOCALIZE(url) sends the files under url through the translator, so @(Text) markers become texts in the language of the request. Set the language with LOCALIZE(fn):
LOCALIZE(function(req, res) {
return (req.headers['accept-language'] || 'en').substring(0, 2);
});
LOCALIZE('/templates/*.html');
LOCALIZE('/texts/*.md', ['nocompress']);Change content before compiling
MODIFY([filename], fn) registers a function that changes the content of views, styles, scripts and localized static files before Total.js compiles them. It changes only the copy in memory, never the file on the disk. Return the new content:
MODIFY(function(type, filename, value) {
if (type === 'view')
return value.replace('{year}', NOW.getFullYear() + '');
});