Skip to content

Recipes ​

The samples serve the folder public/ next to the sample file; Serve Several Folders adds legacy/.

Serve Several Folders ​

Register the plugin once per folder. A request without a file in the first folder goes on to the second.

ts
import { fileURLToPath } from 'node:url';
import { App } from 'routup';
import { assets } from '@routup/assets';

export const app = new App();

// checked first: a file here wins over one of the same name below
app.use(assets(fileURLToPath(new URL('public', import.meta.url))));

// reached when public/ has no matching file
app.use(assets(fileURLToPath(new URL('legacy', import.meta.url))));
bash
curl http://localhost:3000/about
# <!doctype html> ... <h1>About</h1>
curl http://localhost:3000/old-page
# <!doctype html> ... <h1>Old Page</h1>

Serve Under a Path Prefix ​

With a path, the plugin serves only requests under it and looks up the rest of the path in the folder. Requests outside the prefix reach the other routes.

ts
import { fileURLToPath } from 'node:url';
import { App, defineCoreHandler } from 'routup';
import { assets } from '@routup/assets';

export const app = new App();

// GET /static/about serves public/about.html
app.use('/static', assets(fileURLToPath(new URL('public', import.meta.url))));

app.get('/about', defineCoreHandler(() => ({ page: 'about' })));
bash
curl http://localhost:3000/static/about
# <!doctype html> ... <h1>About</h1>
curl http://localhost:3000/about
# {"page":"about"}

Cache Hashed Files ​

Files with a content hash in the name can be cached for a year; the other files are revalidated with their ETag on every use.

ts
import { fileURLToPath } from 'node:url';
import { App } from 'routup';
import { assets } from '@routup/assets';

export const app = new App();

// content-hashed file names never change: cache them for a year
app.use('/assets', assets(fileURLToPath(new URL('public/assets', import.meta.url)), {
    cacheMaxAge: 31536000,
    cacheImmutable: true,
}));

// everything else: browsers revalidate with the ETag on every use
app.use(assets(fileURLToPath(new URL('public', import.meta.url))));
bash
curl -I http://localhost:3000/assets/app.3f9a1c.css
# cache-control: public,max-age=31536000,immutable
curl -I http://localhost:3000/about
# cache-control: public,max-age=0,must-revalidate

Answer Missing Files ​

resolve builds the response for a path without a file, here a JSON 404 instead of passing the request on.

ts
import { fileURLToPath } from 'node:url';
import { App } from 'routup';
import { assets } from '@routup/assets';

export const app = new App();

app.use('/files', assets(fileURLToPath(new URL('public', import.meta.url)), {
    resolve: async (event) => {
        event.response.status = 404;

        return { message: `${event.path} not found` };
    },
}));
bash
curl -i http://localhost:3000/files/nope.txt
# HTTP/1.1 404 Not Found
# {"message":"/files/nope.txt not found"}

Single-Page App ​

A single-page app gets its index.html for every path its client-side router handles, while missing assets and API paths do not: fallback with fallbackIgnores.

ts
import { fileURLToPath } from 'node:url';
import { App, defineCoreHandler } from 'routup';
import { assets } from '@routup/assets';

export const app = new App();

app.use(assets(fileURLToPath(new URL('public', import.meta.url)), {
    // index.html for the paths of the client-side router
    fallback: true,
    // not for API paths; a missing file with an extension is left out too
    fallbackIgnores: ['^/api(/|$)'],
}));

app.get('/api/todos', defineCoreHandler(() => [{ title: 'Write docs' }]));
bash
curl http://localhost:3000/todos/42
# <!doctype html> ... <h1>Home</h1>
curl http://localhost:3000/api/todos
# [{"title":"Write docs"}]
curl -i http://localhost:3000/assets/missing.js
# HTTP/1.1 404 Not Found

The Static Files example on routup.dev shows a larger setup.