How-to
Website
Web app
Desktop / phone
Call WF from a script
The runtime is window.WF. A script under src/, the console, or a library's callback can call it. This page has a button that copies the page's address, and the script it calls says so with the runtime's toast:
Needs: the script src/share.js, shown below.
page Article(path: "/article", title: "Slow roasting", description: "Why we take eleven minutes over it.") {
Heading("Slow roasting").h1
Text("Eleven minutes, and why it matters.")
Button("Copy the link") { on click { shareLink() } }
}
The script is a plain file under src/ (Call your own JavaScript):
// src/share.js
/**
* Copy this page's address, and say so in a toast.
*/
async function shareLink() {
try {
await navigator.clipboard.writeText(location.href);
WF.toast("Link copied", { tone: "success" });
} catch (e) {
WF.toast("Could not copy the link", { tone: "danger" });
}
}
How it works
WFis onwindowonceapp.jshas run, so any script may call it while the page is open.WF.toast(message, { tone, duration })shows a toast in a tone ("info"by default) fordurationmilliseconds (3000 by default);action:andonAction:add a button to it, astoast(…)does in a handler.- Other calls a script makes the same way:
WF.navigate("/about")moves to another route, andWF.storeSnapshot("Cart")reads what a store holds. The runtime reference lists every function. - A build keeps only the runtime modules its code reaches, and it reads your scripts under
src/for that too: theWF.toastwritten insrc/share.jskeeps the toast module in the build, though no.wfcode uses it.
Warning
A call the build cannot read is not counted: a script in public/ or from another origin, a WF[name] built at run time, or the console. If it reaches a module nothing else uses, set "build": { "runtime": "full" }, or the call fails in the browser.
A script from another origin must be listed in meta.scripts, which widens the policy for it. The build's CSP check refuses an inline <script> (Security).
Troubleshooting
| What you see | What to do |
|---|---|
|
|
The build left the module out: write the call in a script under |
|
|
The script ran before |