CSS & JavaScript
How to handle assets for use in a web browser
Browser assets are stored in two distinct folders, based on whether these assets require processing or not.
Static assets
Any assets that do not require processing (e.g. fonts, robots.txt, icons) should be placed in the public directory. They will be served as static files from the root directory. Dotfiles (e.g. public/.well-known/security.txt) are only served if you allow them via FASTIFY_SEND_OPTIONS (see configuration).
Static files are only served for GET and HEAD requests. Other methods (e.g. a POST to /form) go to your routes, even if a file with that path exists. If a file is missing or ignored, your routes answer instead. Static files are checked right after the guards, so a file wins over a named route with the same path (see order of execution).
Please note: in production, jeasx build creates an index of all files in public and dist. Only files that existed at build time are served, so files added to public later (e.g. uploads) aren't available until the next build. Symbolic links are skipped in production builds.
| File path | URL |
|---|---|
| public/robots.txt | /robots.txt |
| public/fonts/ubuntu.woff2 | /fonts/ubuntu.woff2 |
CSS & JavaScript
CSS, JavaScript, or TypeScript files that require processing and minification should be stored in the src directory and must be named as index.[css|js|jsx|ts|tsx]. Only index files serve as bundle entries and are accessible to the client. This approach offers flexibility in creating a custom directory layout.
Please note: Jeasx doesn't specify any hardcoded outbase directory for esbuild, so it defaults to the lowest common ancestor directory for all your browser assets. So when you put all your assets into a dedicated folder (e.g. src/browser), browser will be removed from the resulting path.
| File path | URL |
|---|---|
| src/index.css | /index.css |
| src/custom/index.js | /custom/index.js |
| src/utils/date.js | This file is not available via a URL. |
Please note: the dist directory is cleaned before every build, so never put files into dist by hand. Use public instead. If a file exists in both dist and public, the file from dist wins.
Please note: as JavaScript or TypeScript is compiled to ECMAScript modules (ESM) via esbuild, you should add type="module" to your script tags to avoid subtle errors.
Cache busting
For best performance, let browsers cache your assets for a long time via FASTIFY_SEND_OPTIONS (e.g. immutable: true and maxAge: "365d", see configuration). Then you need a new URL for each deployment, so browsers load the new files. Jeasx doesn't add one for you, but a build id is easy to define in jeasx.config.js:
export default {
/** @type {() => import("esbuild").BuildOptions} */
ESBUILD_SERVER_OPTIONS: () => ({
// A new value for each build, available in all routes and components.
define: { "process.env.BUILD_ID": `"${Date.now().toString(36)}"` },
}),
};
Append it to your asset URLs, e.g. in your layout:
<link rel="stylesheet" href={`/index.css?v=${process.env.BUILD_ID}`} />
<script type="module" src={`/index.js?v=${process.env.BUILD_ID}`}></script>
Static files ignore the query string, so no further setup is needed. Assets referenced from your CSS (e.g. fonts or images loaded via esbuild's file loader) get a content hash from esbuild anyway.
Interactivity in the browser
Jeasx renders HTML on the server and sends no JavaScript by default. For interactive parts, pick the smallest tool that does the job:
- Plain JavaScript in
index.jsfor small enhancements, e.g. a confirmation dialog (see the tutorial). - htmx to update parts of a page with HTML fragments rendered by your routes. This fits Jeasx very well, as every route can return a fragment.
- Islands with a client-side library like Preact for rich widgets. Render a placeholder on the server and mount the widget in the browser.
Pass data from the server to browser code via data-* attributes. Objects are serialized to escaped JSON automatically:
// Server: render a placeholder with initial data
<div class="counter" data-props={{ start: 5 }}></div>
// Browser: src/browser/index.js
document.querySelectorAll(".counter").forEach((element) => {
const { start } = JSON.parse(element.dataset.props);
// mount your widget here
});
The Expo shows complete examples for htmx, Preact and more.
Intercept static assets with reply.file
All static files - whether stored in the public or the dist directory - are delivered via the Jeasx route pipeline immediately following the guard processing stage. This architecture allows you to intercept static file requests seamlessly. You can access static file metadata directly within guards via reply.file. If a file exists at the requested path, this object is pre-populated with headers, statusCode, and stream. This enables you to apply or bypass authentication, modify headers, or replace content on the fly. The header names in reply.file.headers use the capitalization of @fastify/send (e.g. ETag, Cache-Control, Last-Modified), so use the exact spelling when you change or delete them.
export default async function ({ request, reply }) {
if (reply.file) {
console.log(reply.file.statusCode, reply.file.headers, reply.file.stream);
// Delete header
delete reply.file.headers["ETag"];
// Overwrite existing header
reply.file.headers["Cache-Control"] = "public, no-cache";
}
// Don't respond with static "/robots.txt"
if (request.path === "/robots.txt" && reply.file) {
reply.file.stream.destroy();
reply.file = undefined;
}
}
Please note: the file stream in reply.file.stream is opened before your guards run. If you remove or replace reply.file (like above), destroy its stream first, otherwise the file handle stays open.