Skip to content

Debugging ​

Your view is a web page and the developer tools of the browser work on it inside the webapp too. Most problems of a view come from a short list of mistakes that a fake world reproduces in a few lines.

Your view in the developer tools of the browser ​

Inside the webapp your view is an iframe. The developer tools show the webapp and the iframe and let you choose the one you inspect.

Chrome and Edge

  • Elements: Inspect in the context menu of your view opens the elements of your own page inside the iframe.
  • Console: the context menu at the top of the console shows top (the webapp). Switch it to the frame of your site to run code such as world.get() in your page. The console.log messages of your page appear in every context.
  • Network: a filter on the domain of your site shows your files and the calls to your server.
  • Sources: your files are listed under the domain of your site and take breakpoints as usual.
  • Preserve log (in the settings of the console) keeps the messages across a reload of your view such as the one after the person allows a feature in askFor.

Firefox: the button Select an iframe as the currently targeted document in the toolbar of the developer tools selects your frame. Safari: turn on the Develop menu in the settings and pick your frame in the context menu of the console.

On a phone: the developer tools of a computer connected to the phone inspect your view on its real screen and with its on-screen keyboard. For Android open chrome://inspect in Chrome on the computer. On an iPhone turn on Web Inspector in the Safari settings and use the Develop menu of Safari on a Mac.

See the world your view gets ​

When your view renders something wrong, first inspect the world it received. Most of the time the world differs from what you expected. During development a panel with the world as JSON shows it at a glance:

jsx
import { useWorld } from './useWorld.js';

// A fake world to build with: no account, no network.
const arena = { name: 'Arena', now: { text: 'Your move' }, data: { board: [[0, 1], [1, 0]] } };

const PANEL = { fontSize: 12, maxHeight: 260, overflow: 'auto', padding: 8, background: '#8882' };

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: arena } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// The world as it is now, at every change. JSON leaves out the functions
	// (send, subscribe, ...), and what is left is what the world holds.
	return <pre style={PANEL}>{JSON.stringify(world, null, 2)}</pre>;
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world to build with: no account, no network.
const arena = { name: 'Arena', now: { text: 'Your move' }, data: { board: [[0, 1], [1, 0]] } };

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });

// Only while building: try `world.get()` in the console of the browser
// (when your view is inside the webapp, first pick the frame of your page in the console).
window.world = world;

// The world as it is now, on the page, at every change.
const panel = document.createElement('pre');
panel.style.cssText = 'font-size: 12px; max-height: 260px; overflow: auto; padding: 8px; background: #8882;';
document.body.append(panel);

world.subscribe((node) => {
	// JSON leaves out the functions (send, subscribe, ...), and what is left is what the world holds.
	panel.textContent = JSON.stringify(node, null, 2);
});
Live, on a fake world
Open

JSON.stringify leaves out the functions (send, subscribe, retry…) and prints exactly the fields of the world. Three other practices help:

  • window.world = world in development only. In the console, after selecting your frame, you can then run world.get(), world.get().data and console.table(world.get().messages).

  • Log only what changed. A log of the whole world on every change fills the console in seconds:

    js
    let before = null;
    world.subscribe((node) => {
      if (before) {
        const changed = Object.keys(node.data).filter((name) => node.data[name] !== before.data[name]);
        if (changed.length) console.log('data changed:', changed);
        if (node.messages.length !== before.messages.length) console.log('new message:', node.messages.at(-1));
      }
      before = node;
    });
  • Log errors with their code (console.error(error.code, error)). Errors lists what each code means.

Reproduce it with a fake world ​

A problem seen in the webapp is easiest to fix on your computer where a reload takes a second. Rebuild the situation as a fake world whose data and now hold the values copied from the debug panel or the console and whose onMessage gives the answers that led to the problem. Load your view on it and change one thing at a time until the problem is gone. Testing with fake worlds covers everything a fake world can simulate including failures.

Common mistakes ​

Opened alone: 'no-host' ​

What you see: your page opened in a browser tab shows nothing or connectToHost() rejects with 'no-host'.

Why: outside the webapp no host passes a world to your page. connectToHost() therefore works only inside the webapp or with fake.

Fix: build with a fake world. To try your view in the webapp, write its address in the Own view field of a world of yours (Going live). In your published page show a message with a link to the world when the code is 'no-host' (Errors).

A localhost address that does not answer ​

What you see: with an address such as http://localhost:5173/ in the Own view field of your world the webapp shows the UNaIVERSE view after 20 seconds with a notice saying that the view of this world is not responding.

Why: the webapp loads the address on the computer of whoever opens the world. On your computer one of three things went wrong: the development server is not running, it runs on another port or the browser refuses an http://localhost page inside the https: page of the webapp. Any other computer has nothing that answers at that address.

Fix: start your development server on the port of the address. When the browser refuses the page, the console of the webapp (context top) gives the reason. When the browser keeps refusing localhost inside the webapp, publish a test version of your view at an https: address of yours and write that address in the field instead. Test on a world that only you use and write the address of your server once the view is ready (Going live).

Connecting too late: the 20 seconds ​

What you see: in the webapp, sometimes or always, the UNaIVERSE view with a notice saying that the view of this world is not responding and a little later the notice "The view of this world is ready."

Why: the webapp waits 20 seconds for your view to connect. Your page misses this limit in two cases: (1) it connects only after something slow (a big bundle, fonts with await document.fonts.ready, settings from your own server, a splash animation or a "Start" button) or (2) it throws an error before it reaches connectToHost() and never connects.

Fix: call connectToHost() at the top of your code and load the rest in the meantime. In React call useWorld() in your top component instead of a component that mounts after a loading step. Check the console for errors thrown before the connection.

The text of the world in innerHTML ​

What you see: usually nothing until someone sends a message like <img src=x onerror="…">.

Why: the text of the world (messages, names, now.text, values of data) comes from others such as agents, other people and the rules of the world. In innerHTML it becomes HTML whose scripts run in your view on your site. There they can send messages and request actions in the name of the person, use the browser features the person allowed, read the storage of your view and call your server.

Fix: write text with textContent and never with innerHTML. In React {message.text} is safe while dangerouslySetInnerHTML with the text of the world is not. For formatting (Markdown, links) use a Markdown library together with a sanitizer such as DOMPurify.

js
line.textContent = message.text;  // safe: always text
line.innerHTML = message.text;    // unsafe: the sender's HTML runs in your view

Keeping copies of the world ​

What you see: your view shows the world as it was at the start or a button acts on an old message.

Why: the world you get is a snapshot whose fields never change. The snapshot returned by connectToHost() keeps describing the world at the moment of the connection. Every change produces a new snapshot.

js
const world = await connectToHost();
const name = world.name;         // fixed at the start
setInterval(() => {
  draw(world.messages);          // always the messages of the start
}, 1000);

Fix: render inside your subscribe listener because each call receives the newest world. Everywhere else (a click, a timer) read world.get(). Calling world.send(...) on the first world is always correct because the functions (send, act, setPath…) work from any snapshot. In React use the world that useWorld() returns in the current render and never keep it in a ref or in state of your own.

Changing the world ​

What you see: TypeError: Cannot add property or Cannot assign to read only property or a change that disappears a moment later.

Why: the world is read-only. Its objects are frozen or replaced on the next change.

Fix: keep your own state (what is open, what the person typed) next to the world and change the world through send, answer or act. A message that the person is still typing belongs in your state and never in world.messages.

Subscribing again and again ​

What you see: the view gets slower and slower or draws everything twice and then three times.

Why: your code calls subscribe again (in a click handler, in a listener, each time a panel opens) without stopping the old listener (Performance).

Fix: subscribe once at startup. A component that subscribes on its own calls the function returned by subscribe when it is removed.

A blank view in the webapp ​

What you see: inside the webapp an empty rectangle in place of your view and the UNaIVERSE view after 20 seconds.

Why: the browser refuses to show your page inside the webapp. With the console context on top you see a message such as Refused to display … in a frame because it set 'X-Frame-Options' to 'deny' or … violates the following Content Security Policy directive: "frame-ancestors 'self'".

Fix: serve the pages of your view with the headers listed in Serve your view for each server. Also check that your page and everything it loads use https://.

Steps in the history of the browser ​

What you see: the back button changes something inside your view without running your onBack handler or has to be pressed several times to leave.

Why: your view or its router adds history entries with history.pushState. Inside an iframe these entries mix with those of the webapp.

Fix: use setPath for the address and onBack for the back button (Links and the back button).

Next steps ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.