---
title: Static files
project: Total.js V4
version: 0.0.99
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/guides/static-files/
updated: 2026-11-16T15:41:05.817Z
---

# 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.

## 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.

```text
/public/css/default.css  -->  GET /css/default.css
/public/img/logo.png     -->  GET /img/logo.png
```

Add an extension with `ACCEPT()`, or with `static_accepts_custom` in the config file:

```js
ACCEPT('.glb', 'model/gltf-binary');
```

```text
static_accepts_custom : glb, usdz
```

What happens to a file before it is sent:

- `.js`, `.mjs` and `.css` files are minified when `allow_compile` is on (default). Files named like `*.min.js` or `*-min.css` are sent as they are.
- Text types (HTML, CSS, JavaScript, JSON, XML, SVG, Markdown, plain text) are compressed with GZIP when `allow_gzip` is on and the client accepts it.
- `.html` and `.htm` files are localized when `allow_localize` is on (default).
- In release mode, the responses have `Cache-Control: public, max-age=11111111`, an `Expires` header one year ahead, `Last-Modified`, and an `ETag` built from `etag_version`. A request with a matching `If-Modified-Since` gets 304. In debug mode, files are sent with `no-cache`.

The framework keeps the processed files in `/tmp/`. `TOUCH(url)` forgets one cached file, and `TOUCH()` forgets all:

```js
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:

```js
F.public_js('app.js');      // /js/app.js
F.public_image('logo.png'); // /img/logo.png
```

```text
// /versions
/js/app.js     : /js/app-2.js
/css/style.css : auto
```

`auto` 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

```js
// 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:

```js
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:

```js
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)`:

```js
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:

```js
MODIFY(function(type, filename, value) {
	if (type === 'view')
		return value.replace('{year}', NOW.getFullYear() + '');
});
```
