config/assets-dir

Note

You need version 1.4.0 or newer.

You, or your themes, may want to include some extra assets. Think things like a style.css, a font, or perhaps some images. Before version 1.4.0, you had to provide your own code in order to make assets available. Since the coming of themes, umejs can handle this.

Getting started is quite easy. Simply create a directory where you want to store your assets and then add assetsDir to the config:

// --- Import Express and umejs ---
const ume = require('@dantenl/umejs');
const express = require('express');
const path = require('path');

// --- Initialise Express ---
const app = express();
const PORT = 3000;

// --- Initialise umejs ---
const umeConfig = ume({
    assetsDir: path.join(__dirname, 'assets'),

    contentDir: path.join(__dirname, 'blog'),
    themes: ['@example/umejs-theme'],
});

// --- Set up the umejs middleware ---
app.use('/blog/{*slug}', umeConfig);

// --- Get the server running ---
const server = app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}`);
});

// --- Fully close umejs ---
process.on('SIGINT', async () => {
    server.close();
    await umeConfig.close();
    process.exit(0);
});

In this example, umejs has made assets available from your assetsDir and from all themes. Note that your assetsDir always overrides the ones created by themes; meaning if both you and your theme are trying to create pack.png, your will override the theme's pack.png.

You can access the assets by going to http://localhost:3000/blog/_ume/assets/<file name>. If no asset can be found there, umejs will just handle the 404 as normal. E.g., if you've made a 404.md, it will serve that.

Tip

You might not know what umejs' path is (e.g. is the user at /blog/ or perhaps /docs/?). umejs provides the variable {_BASE} to show this. This is especially useful when developing a theme! Use this to locate the files in the assets directory. For example, here's how you might link to the custom CSS styling file:

<link rel="stylesheet" href="{_BASE}/_ume/assets/main.css" />

umejs does not handle the caching. You would have to do handle that yourself. For example, you could do something like the following to automagically set the cache headers to your preference:

const umeConfig = ume({/* ... */});

// umeConfig.ctx contains a lot of helpful info, including all the places where the assets are stored
// when someone makes a request to the ume assets directory, add cache headers
for (const dir of umeConfig.ctx.assetsDirs) {
    app.use(
        '/blog/_ume/assets',
        express.static(dir, { maxAge: '1y', immutable: true, etag: true }),
    );
}

// then, do the normal thing for starting ume
app.use('/blog/{*slug}', umeConfig);