# Frontend

The frontend is a set of static files with an `index.html` at its root. The platform serves them as they are. It never installs packages or runs a build.

## Template without a build step

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My app</title>
    <link rel="stylesheet" href="/_boringcloud/brand.css" />
    <link rel="stylesheet" href="/style.css" />
    <script type="module" src="/app.js"></script>
  </head>
  <body>
    <main id="app"></main>
  </body>
</html>
```

The first stylesheet gives the company's colors. See [Brand](https://boring-cloud.app/docs/brand.md).

## Call the backend

Use relative addresses. The session is sent for you.

```js
const api = async (path, options = {}) => {
  const res = await fetch(`/api${path}`, {
    ...options,
    headers: options.body ? { "content-type": "application/json" } : {},
  });
  if (res.status === 401) {
    location.reload(); // the session ended: the platform asks the person to sign in again
    return new Promise(() => {});
  }
  const data = await res.json().catch(() => ({}));
  if (!res.ok) throw new Error(data.error ?? "Something went wrong. Try again.");
  return data;
};
```

Never store a token or write a login form. Sessions last up to 12 hours.

## Content security policy

Every page has this policy. It cannot be changed.

```
default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; media-src 'self'; object-src 'none'; frame-src 'none'; child-src 'none'; worker-src 'none'; frame-ancestors 'none'; form-action 'self'; base-uri 'none'; manifest-src 'self'
```

| What does not work                                              | Do this                                                    |
| --------------------------------------------------------------- | ---------------------------------------------------------- |
| `<script>` with code inside, `onclick="…"`, `javascript:` links | put code in a `.js` file and use `addEventListener`        |
| `eval`, and libraries that compile templates in the browser     | libraries that do not need it, or a build step             |
| scripts, styles or fonts from a CDN or from Google Fonts        | put the files in the release                               |
| images from another site                                        | put them in the release                                    |
| `fetch` to another site                                         | your backend, with an [approved API](https://boring-cloud.app/docs/external-apis.md) |
| iframes, embedded videos or maps                                | a link that opens in a new tab                             |
| service workers and web workers                                 | code in the page                                           |
| camera, microphone, location                                    | a file input                                               |

Inline styles work. Links to other sites work. With Vite, set `build.modulePreload.polyfill` to `false`.

## Files

- Types: `html css js mjs json map webmanifest txt svg png jpg jpeg gif webp avif ico woff woff2`. Anything else is refused, such as `pdf`, `csv` or `mp4`. Serve those from [files](https://boring-cloud.app/docs/files.md).
- The content must match the extension: valid UTF-8 for text, valid JSON for `json`, the right signature for images and fonts.
- Paths use letters, digits, `-`, `_`, `.`, `@`, `+` and `/`. No spaces, accents, hidden files, `..` or `%`. Two paths that differ only by case are refused.
- Reserved: `api/`, `_boringcloud/`, `sw.js`, `service-worker.js`, `boringcloud.json`.

## Addresses in the app

- An address that matches a file serves it.
- An address without a file extension serves `index.html`, so client-side routes such as `/requests/12` work and survive a reload.
- A missing `.js`, `.css` or image gives a `404`.
- Use addresses that start with `/` in your HTML. Relative ones break on nested routes.

## Quality expected

- Works on a phone (from 360 px wide) and with a keyboard, unless IT turned phones off in the stack. Every field has a label.
- Shows three states everywhere: loading, empty, and error with a way to try again.
- Uses the language of the people who use it.
