Écrire une app de page
A page app is a space app with frontend = "page": one HTML file that Ivy shows in place of its own viewer for the files the app opens. The page runs in a sandboxed frame with no network and no credentials. It never fetches the file or saves it itself: Ivy hands it the file through window.ivy, and saves what the page gives back. A page that waits for a file any other way never gets one.
The manifest
The manifest names the page and the files it opens. With open, opening the app by itself, from the space’s Apps tab or when the assistant starts it, opens that file in it, so an app with one data file needs no file chosen first:
# notes/notes.ivy-app.toml
frontend = "page"
page = "notes/notes.html"
open = "data/notes.notes.json"
access = "read"
[name]
en = "Notes"
de = "Notizen"
fr = "Notes"
es = "Notas"
it = "Note"
[description]
en = "Keeps the team’s notes in one file."
de = "Hält die Notizen des Teams in einer Datei."
fr = "Garde les notes de l’équipe dans un fichier."
es = "Guarda las notas del equipo en un archivo."
it = "Tiene le note del team in un file."
[[opens]]
extensions = ["notes.json"]
edits = true
page is a path from the top of the space. open is relative to the app’s root, else to the manifest’s folder: here notes/data/notes.notes.json. It must stay inside that folder and outside hidden folders, have an extension the app opens, and not be the page, the manifest or a template. The app manifest reference lists every field.
A space’s own app runs only once a person approved its manifest, its page and the files under its root, and every later edit to them needs approving again. The default file is the app’s data: editing, moving or deleting it needs no new approval, unless it moves to another path under the app’s root, where it is one of the files the app is made of. While it is missing, the app stays valid and still opens the files it opens through “Open with…”; the Apps tab says “Its default file … is missing” instead of offering Open, the manifest’s check reports the warning default_file_missing, and the assistant tells the person. Putting a file back at that path makes it the default again.
What the page gets
Before the page’s scripts run, window.ivy is defined. Register the handlers at the top level of a script, not after an await, so the page is ready when the host speaks.
window.ivy |
What it does |
|---|---|
onOpen(({ app, file, content, mode, editable }) => …) |
The file the person opened: file has its name, mimeType, size and folder, content is its bytes as a Blob, mode is "edit" when this person may change the file with this app and "view" otherwise, and editable says whether they may save it. Called again with new content. |
onMode((mode) => …) |
mode changed while the file is open, for example because the person’s permissions changed. Not called when the file opens. |
onContext(({ theme, locale }) => …) |
Ivy’s theme ("light" or "dark") and language, at once and whenever they change. |
setDirty(dirty) |
Tells Ivy the page has unsaved changes, or no longer has. Ivy enables Save while there are changes and the person may save. |
onSerialize(() => content) |
Returns the draft as a string or Blob (or a promise of one) when the person saves. Ivy saves it as a new version of the file. |
openLink({ url }), openLink({ path }) |
Asks Ivy to open a web or mail address in a new tab, or a file of the space by its path. The page never navigates itself. |
callTool(name, input) |
Calls one of the app’s own tools on its running instance, as the person viewing: { content, isError }, or a ToolCallError with Ivy’s code. |
records.query(name, query) and get, create, … |
The collections the manifest declares in [[collections]]: schema, query, get, create, update, delete, comment and subscribe. A failure is a RecordsError with code and details. |
asset(path) |
A file of the app, by its path from the app’s root, as a Blob, from the approved version the page came from: a script, a font, data. Each path loads once. A failure is an AssetError with code, such as not_found. |
assetUrl(path) |
The same file as a blob: address, for a worker, an image, a font or media. A script runs only inline: read it with asset and add a <script> with its text. |
The page sees only the file it was opened with, the collections it declares and its own app’s files under its root; it reads nothing else of the space, and never its default file through asset.
Limits
- No network. The page can’t fetch, load scripts, styles or fonts from elsewhere, or reach Ivy’s API. Put what it needs inline in the HTML file, or under the app’s
rootand load it withasset. - Workers and WebAssembly from its own files. A page may start a worker from a
blob:address, such asassetUrlgives, and compile WebAssembly. A worker has no network either. - At most 2 MiB for the page, scripts and styles included, and for each file of the app; at most 8 MiB for the app’s files together.
- No credentials. The page holds no token and no cookie of Ivy’s; Ivy acts for it only through
window.ivy, as the person viewing. - Saving is Ivy’s. The page returns its draft from
onSerialize; Ivy saves it, checks the file hasn’t changed meanwhile and keeps the version history.
Ivy Core’s apps as examples
Every viewer Ivy opens files with is an app of the same kind, declared by a manifest. On the Public tab of All spaces, open Ivy Core and choose View manifest on an app to read its *.ivy-app.toml, for example how the CSV app names its page, the files it opens and the file it adds to the “New file” menu.
A complete example
This page edits a JSON file. It opens ready to edit for someone who may change the file and read-only for someone who may only view it, and saves only valid JSON:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
margin: 0;
font:
14px system-ui,
sans-serif;
background: #fff;
color: #111;
}
body.dark {
background: #111;
color: #eee;
}
textarea {
box-sizing: border-box;
width: 100%;
height: 100vh;
border: 0;
padding: 12px;
font:
13px ui-monospace,
monospace;
background: inherit;
color: inherit;
}
p {
margin: 0;
padding: 4px 12px;
color: #b00;
}
</style>
</head>
<body>
<p id="error" hidden></p>
<textarea id="text" readonly spellcheck="false"></textarea>
<script>
const text = document.getElementById("text");
const error = document.getElementById("error");
let saved = "";
window.ivy.onOpen(async (opened) => {
saved = await opened.content.text();
text.value = saved;
text.readOnly = opened.mode !== "edit";
window.ivy.setDirty(false);
});
window.ivy.onMode((mode) => {
text.readOnly = mode !== "edit";
});
window.ivy.onContext(({ theme }) => {
document.body.classList.toggle("dark", theme === "dark");
});
text.addEventListener("input", () => {
try {
JSON.parse(text.value);
error.hidden = true;
} catch (e) {
error.textContent = e.message;
error.hidden = false;
}
window.ivy.setDirty(text.value !== saved);
});
window.ivy.onSerialize(() => JSON.stringify(JSON.parse(text.value), null, 2) + "\n");
</script>
</body>
</html>
When onSerialize throws, as JSON.parse does on invalid JSON, Ivy doesn’t save and the person keeps their draft. Once a save succeeds, Ivy calls onOpen with the saved content, which resets saved; a save that fails leaves the draft unsaved and the page still dirty.