# Loading and preloading

Share one cache for images and models, preload the next page on intent, and drive a preloader from the progress of the first screen.

Every image an effect shows, every model and every preload goes through one cache with progress. This guide covers what the cache keeps, how to preload the next page and how to build a preloader with a scroll lock.

## The cache

`start()` adds the cache, and effects use it without code. The same URL decodes once, however many effects show it. An entry lives while something holds a lease on it; when the last lease goes, the entry stays for reuse up to `retain` bytes, 32 MB by default, and the least recently used go first. A model or cover seen on the previous page comes back without a request.

Inside an effect, load through the context: `ctx.texture(url)` for a texture and `ctx.model(url)` for a glTF. Their leases end with the effect, so nothing leaks when the element leaves.

```ts title="src/effects/paper.ts"
import { effect } from '@dioramajs/diorama'
import { texture as sample, uv } from 'three/tsl'

export default effect(
  { grain: 0.2 },
  async ({ surface, texture, u }) => {
    const noise = await texture('/textures/paper.avif', {
      colorSpace: 'linear',
    })
    surface.color((color) =>
      color.mul(sample(noise, uv()).r.mul(u.grain).oneMinus()),
    )
  },
)
```

Outside effects, `app.assets.load(url)` returns a lease at once. The type comes from the extension, or from `type`: `image`, `texture`, `gltf`, `ktx2`, `buffer` or `json`.

```ts
const data = app.assets.load('/data/stations.json', { type: 'json' })
const stations = await data.ready
// Later, when the page no longer needs it:
data.release()
```

`ready` rejects with `ASSET_LOAD`, `ASSET_PARSE`, `ASSET_TYPE` or `ASSET_DECODER`; the same failure is reported once through the app. `ready(app)` from `@dioramajs/diorama` resolves when the app runs, so `app.assets` exists.

## Preload the next page on intent

Put the files of the next page in `data-dio-preload` on the link, separated by spaces. Hovering the link for 65 ms, focusing it or touching it puts them in the cache, so the cover is decoded by the time the flight lands.

```html title="work.html"
<a
  href="/work/aurora"
  data-dio-preload="/img/aurora-cover.avif /models/aurora.glb"
>
  <img
    src="/img/aurora-thumb.avif"
    data-dio="image"
    data-dio-key="aurora"
  />
</a>
```

Images from another origin that the page shows with `crossorigin` must be preloaded in the same CORS mode, or the cache misses and the cover loads twice. Add `crossorigin` to the link; models in the same list load as usual.

```html
<a
  href="/work/aurora"
  data-dio-preload="https://cdn.example.com/aurora.avif"
  crossorigin
>Aurora</a>
```

Under Save-Data and on 2G connections nothing preloads.

## Preload from router code

When the router has already fetched the next page, pass its document to `app.assets.preload()`. It takes the images with `data-dio` or `data-dio-key` and the URLs in `data-dio-preload`. An image inside `<picture>` picks the same source and size the new page picks.

```ts
const html = await (await fetch(url)).text()
const next = new DOMParser().parseFromString(html, 'text/html')
app.assets.preload(next, { base: url })
```

Pass the URL of the next page as `base`. A parsed document has the address of the current page, so `img/cover.avif` on `/work/aurora/` would otherwise resolve against `/work`. A page URL is not an asset: `preload('/work/aurora')` warns with `ASSET_TYPE` in development.

- Only the latest preload of each kind is held: one from markup and one from code. The previous one is released, and its entries stay in the cache within `retain`.
- Preloads run at low priority and wait while a flight is in the air, so the flight’s frames stay smooth. A cover the landing target already needs decodes at once.
- A failed preload does nothing visible: the page loads the file itself and reports a failure then.

The [routers guide](/docs/guides/routers#recipes) has the preload line for Astro and Swup.

## Progress of the first screen

`app.assets.progress` is a signal from 0 to 1 for the current wave of work. During `start()` effects put their first screen into it: the engine, the renderer and the images of effects near the viewport. Models and textures loaded at normal or high priority join the wave; preloads do not move it. Within a wave progress never goes back, and it reaches 1 when `app.assets.ready()` resolves.

Add your own work to the wave with `track()`, for example web fonts. `weight` is in bytes, 100 000 by default.

```ts
app.assets.track(document.fonts.ready, { weight: 60_000 })
```

## Build a preloader

The page below shows a bar while the first screen loads, locks scroll and fades the preloader out once everything is ready.

```html title="index.html"
<div id="preloader">
  <div id="bar"></div>
</div>
```

```css title="src/preloader.css"
html.is-loading {
  overflow: hidden;
}
#preloader {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: #0b0b10;
  transition: opacity 0.6s;
}
#bar {
  width: 40vw;
  height: 2px;
  background: #f4efe6;
  transform: scaleX(0);
  transform-origin: left;
}
```

```ts title="src/main.ts"
import { ready, start } from '@dioramajs/diorama'

const root = document.documentElement
root.classList.add('is-loading')

const preloader = document.querySelector<HTMLElement>('#preloader')
const bar = document.querySelector<HTMLElement>('#bar')

export const app = start()

async function boot(): Promise<void> {
  try {
    await ready(app)
    app.assets.track(document.fonts.ready)
    const off = app.assets.progress.subscribe((p) => {
      if (bar) bar.style.transform = `scaleX(${p})`
    })
    // The engine, first-screen images, models and fonts. Never rejects.
    await app.assets.ready()
    off()
  } finally {
    // Unlock even if the app fails: the page works as plain HTML.
    root.classList.remove('is-loading')
    preloader?.addEventListener(
      'transitionend',
      () => preloader.remove(),
      { once: true },
    )
    if (preloader) preloader.style.opacity = '0'
  }
}
void boot()
```

A preloader over three prints: once the engine is ready its bar fills a third for each print that arrives over a slow network, then the preloader fades out.

```html title="index.html"
<div class="page">
  <div class="shots"></div>
  <div id="preloader">
    <div id="bar"></div>
  </div>
</div>
```

```html title="panel.html"
<button class="again" type="button">Load again</button>
<p>The example reads the images at 48 KB a second, as a slow network would.</p>
```

```ts title="script.ts"
import { ready } from '@dioramajs/diorama'
import { app } from '../../docs/diorama.ts'

const IMAGES = [
  '/media/prints/dawn.jpg',
  '/media/prints/dusk.jpg',
  '/media/prints/night.jpg',
]
// Stands in for a slow network: every body is read at 48 KB a second, all of them in turn.
const RATE = 48_000
let line = Promise.resolve()

async function slowBlob(url: string): Promise<Blob> {
  const response = await fetch(url)
  const reader = response.body?.getReader()
  const chunks: Uint8Array<ArrayBuffer>[] = []
  for (
    let read = await reader?.read();
    read && !read.done;
    read = await reader?.read()
  ) {
    const chunk = read.value
    line = line.then(
      () =>
        new Promise((done) =>
          setTimeout(done, (chunk.length / RATE) * 1000),
        ),
    )
    await line
    chunks.push(chunk)
  }
  return new Blob(chunks, {
    type: response.headers.get('content-type') ?? 'image/jpeg',
  })
}

async function addImage(shots: Element, url: string): Promise<void> {
  const img = document.createElement('img')
  img.alt = ''
  img.src = URL.createObjectURL(await slowBlob(url))
  shots.append(img)
  await img.decode()
}

// src/main.ts of the guide, run on the box of the example instead of the whole page.
export default (example: HTMLElement) => {
  const preloader = example.querySelector<HTMLElement>('#preloader')
  const bar = example.querySelector<HTMLElement>('#bar')
  const shots = example.querySelector('.shots')
  const again = example.querySelector('.again')
  if (!preloader || !bar || !shots) return

  async function boot(): Promise<void> {
    try {
      await ready(app)
      // The weight is in bytes, about the size of each print.
      for (const url of IMAGES)
        app.assets.track(addImage(shots as Element, url), {
          weight: 45_000,
        })
      const off = app.assets.progress.subscribe((p) => {
        if (bar) bar.style.transform = `scaleX(${p})`
      })
      // The engine and the three prints. Never rejects.
      await app.assets.ready()
      off()
    } finally {
      if (preloader) preloader.style.opacity = '0'
    }
  }

  const replay = () => {
    for (const img of shots.querySelectorAll('img')) {
      URL.revokeObjectURL(img.src)
      img.remove()
    }
    preloader.style.transition = 'none'
    preloader.style.opacity = '1'
    bar.style.transform = 'scaleX(0)'
    void preloader.offsetWidth
    preloader.style.transition = ''
    void boot()
  }
  again?.addEventListener('click', replay)
  void boot()
  return () => again?.removeEventListener('click', replay)
}
```

Without WebGPU or WebGL 2, or under Save-Data, the engine does not load and the wave ends at once, so the preloader does not hang.

With Lenis, `overflow: hidden` on the root does not stop wheel scrolling. Stop Lenis instead and start it in the same `finally`.

```ts
import { ready, start } from '@dioramajs/diorama'
import Lenis from 'lenis'

const lenis = new Lenis()
export const app = start({ lenis })

lenis.stop()
await ready(app)
  .then(() => app.assets.ready())
  .finally(() => lenis.start())
```

## A bar without JavaScript

Mark any element with `data-dio-progress` and it gets the custom property `--dio-progress`. Diorama writes nothing else into it.

```html
<div
  data-dio-progress
  style="transform: scaleX(var(--dio-progress, 0))"
></div>
```

The bar follows the page loading: the first wave, and the wave of each page change, from 0. Pictures that load later as the visitor scrolls move `app.assets.progress` but not the bar, so it never jumps back in the middle of a scroll.
