A game on the web downloads its images and sounds as it needs them, which can make a scene appear late or a sound start after its line on a slow connection. Monogatari has two ways to have them ready: preloading, which downloads them behind a loading screen before the main menu, and the service worker, which keeps a copy of the game’s files in the player’s browser for the next visits.

Both are for a game served from a website or a local server. A game opened from its index.html file (file://) uses neither: see what works opened from a file.

The settings

Setting Type Default Starter game What it does
Preload true or false true same Load assets behind the loading screen before the first screen.
ServiceWorkers true or false true same Register the starter game’s service-worker.js, which keeps the game’s files for later visits and offline play.

Both are read once, while the game loads. Set them in js/options.js and reload.

When preloading runs

Preloading runs once, after the game has set itself up and before its first screen (the splash screen or the main menu). It’s skipped, with no loading screen, when:

  • Preload is false;
  • the game was opened from a file;
  • the game runs as a desktop app (Electron or Electrobun) or a Cordova app, where the files are already on the device.

A light gray screen with the title Loading, a bar filled a little over half way and the text "Wait while the assets are loaded"
The loading screen while the game preloads, five of its eight files in.

The loading screen shows Loading, a bar and Wait while the assets are loaded. The bar fills as each file arrives, and the screen closes when every file has finished.

If a file can’t be loaded, because it’s missing or misspelled, the others still load. When they’re all done, the game moves on to its first screen, the browser’s console shows the error for each file that failed, and, with the debug file loaded, an error dialog lists them: Some of your game’s files could not be preloaded (see Diagnosing Errors). The game still plays, and a missing file is asked for again when the story uses it.

Choose what to preload

Without a default block: every asset

By default, the game preloads every file it knows about: each asset of every kind you registered with monogatari.assets() (music, voices, sounds, videos, images, scenes, gallery) and each sprite, expression and layer of every character, plus a default_expression that names a file rather than one of the character’s expressions. A file declared twice, like a sprite that is also an expression, is downloaded once. Images are downloaded and decoded; other files are downloaded so the browser has them in its cache.

This is the right choice for a small game. For a large one, the loading screen may take long, and every video is downloaded before the main menu.

With a default block: the assets the game needs first

A default block, set up with the preload action’s blocks(), lists the assets to load before the main menu instead. The rest load when your script asks for them with preload statements, or when the story first uses them:

monogatari.action('Preload').blocks({
  'default': {
    'scenes': ['afternoon'],
    'music': ['theme'],
    'sounds': ['bell'],
    'characters': {
      'y': ['happy', 'smiling'],
    },
  },
});

Put it in js/script.js, next to your assets. The block’s lists hold the names you gave the assets in monogatari.assets(), and for characters, the names of their sprites.

Key Holds
scenes, images Scene and image names. They’re downloaded, decoded and kept ready to show.
music, sounds, voices Audio names. They’re downloaded, decoded and kept ready to play. They’re also stored, decoded, in a browser database named after your game (my-visual-novel_AudioCache), so the next visit doesn’t download them again.
characters An object of character identifiers, each with a list of sprite names.

Use these keys exactly. A singular key such as 'sound' is skipped, with Preload: No loader registered for category “sound” in default block in the console. Videos and the gallery can’t be in a default block.

A name that isn’t declared (a typo, a file name such as 'afternoon.jpg' instead of the name 'afternoon', or a sprite the character doesn’t have) has nothing to load, and is skipped. While you develop, the dialog The preload block “default” names files your game never declared lists every such name, and the console lists them too. Before 2.9.0 they were skipped without a word.

Turn preloading off

monogatari.settings({
  'Preload': false,
});

The game goes straight to its first screen, and each file downloads when the story first uses it.

The service worker

The starter game comes with service-worker.js, a script the browser runs next to the game to answer its requests for files. Once registered, it keeps a copy of the game’s files in the browser, so later visits load faster and the game can be played offline after a first visit.

The game registers it when ServiceWorkers is true and the game is served over https, or from your own computer as localhost or 127.0.0.1. Otherwise it plays without it and the console says why:

Where the game runs Console message
Opened from a file Offline play is off: browsers only run the service worker (service-worker.js) for a game served over https, or from localhost or 127.0.0.1 on your own computer, not for an index.html opened from a file. …
Served over plain http:// from another address (a computer on your network, for example) Offline play is off: browsers only run the service worker (service-worker.js) for a game served over https, or from localhost or 127.0.0.1 on your own computer. This page comes over plain http from another address, so the game needs a connection to play. …
A browser without service workers, or with them turned off in its settings Offline play is off: this browser doesn’t run service workers (service-worker.js), or they are turned off in its settings, so the game needs a connection to play.
Desktop or Cordova app Nothing: the app doesn’t use it.

What it does with each file

Files What the service worker does
The files in its FILES list Stores them when it installs, on the first visit: index.html, the engine, your js and style files, the manifest and the icons. A file in the list that’s missing on the host is skipped, and the console names it.
Files in the game’s assets folder Answers with its copy when it has one, without waiting for the network, and asks the host in the background, once each time the game loads, whether the file changed. A changed file replaces the copy, for the next load. Files it doesn’t have yet come from the network and are stored.
Everything else from the game’s site: the page, the engine, your js and style files Asks the host first, so a changed file is used at once, and uses its copy when the player is offline.
Files from other websites Left to the browser: not stored, not available offline.
A page of the game’s site it never kept, asked for offline A short page that says it couldn’t load without a connection.

So a player gets your changed script or stylesheet on their next visit, and a picture, sound or font you replaced under the same file name by the visit after that, without a new file name.

Asking the host whether a file changed costs a few bytes for each file that didn’t: the service worker sends what the host said about its copy (the ETag or Last-Modified header), and a host that sees the same file answers “not modified” instead of sending it again. GitHub Pages and Netlify send those headers. On a host that sends neither, the files in assets stay as first stored; to make every player download everything again, change the VERSION near the top of service-worker.js:

// The name of your game, no spaces or special characters.
const name = 'Monogatari';

// The cache version. You don't need to change it when you edit or replace a
// file: players get the new one on their next visit. Changing it makes every
// player download the whole game again.
const version = '0.1.1';

The next time a player opens the game, the new service worker installs, deletes its old copy and the game shows There is new content available, reload the page to get the latest version. at the top of the screen.

Change NAME too, to your game’s name without spaces. The copy is named after NAME and the game’s folder on the website, so two games in two folders of one website keep separate copies even when both keep 'Monogatari'. If you changed the root of AssetsPath in js/options.js, set ASSETS, below VERSION, to the same folder.

A game in a folder of a website

A game in a folder (https://example.com/my-game/) works offline like one at the front of a site, even when the website’s front page doesn’t exist: every path in the service worker is relative to its own folder.

A game started before 2.9.0 has the older service-worker.js, which doesn’t install there; Web shows how to update it.

Turn it off

monogatari.settings({
  'ServiceWorkers': false,
});

This stops the game from registering it. It doesn’t remove a service worker a player’s browser already has from an earlier visit: that one keeps answering until the player clears the site’s data.

Try it on your own computer

The service worker needs the game served. Putting it online is the way that needs no tools (see Share your game); Open your game lists other ways to serve it while you work. Preloading and the service worker then behave as they will for your players.

Assets on another site

AssetsPath can point at another website, such as a storage service for your files:

monogatari.settings({
  'AssetsPath': {
    'root': 'https://cdn.example.com/my-game',
  },
});

That site must:

  • use https://. The Content-Security-Policy line at the top of the starter game’s index.html only allows the game’s own address and https:// addresses; anything else is blocked, and the console says the content security policy refused it;
  • send an Access-Control-Allow-Origin header that allows your game’s address, for music, sounds and voices, which the game downloads to decode them. Without it, they play through a plain audio player, without their audio effects, and they can’t be preloaded. Images show without it, but save slot pictures are black where they are.

The service worker doesn’t keep files from another address for offline play.

  • Preload: loading assets during the story, and the blocks.
  • Loading Screen: the screen itself.
  • Web: publishing a web game.