Show a message outside the webapp
A person who follows an old link or a search engine can open the address of your view in a browser tab without going through the webapp. Outside the webapp there is no world and connectToHost() rejects with the code 'no-host'. Instead of leaving the page empty, say what the page is and link to the world in UNaIVERSE.
The example has no fake world and makes the real call. This documentation page is not the webapp and the call waits 5 seconds for an answer before the example shows its message.
import { useWorld } from './useWorld.js';
// The address of your world in UNaIVERSE, for people who open your view on its own.
const WORLD_LINK = 'https://unaiverse.io';
export default function App() {
// This recipe is about the real call, as in the webapp, and has no fake world.
const { world, error } = useWorld();
// A page opened alone, outside the webapp, has no world, and the code is 'no-host'.
if (error?.code === 'no-host') {
return (
<>
<h2>This page is the view of Arena</h2>
<p>It works inside UNaIVERSE, where Arena lives. Open Arena there to take part.</p>
<a href={WORLD_LINK} target="_blank" rel="noopener">Open Arena in UNaIVERSE</a>
</>
);
}
// The other code, 'unsupported', comes from a webapp that does not speak this version of the kit.
if (error) return <p>This view cannot start here ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
return <h2>{world.name}</h2>;
}import { connectToHost } from '@unaiverse/gui-kit';
// The address of your world in UNaIVERSE, for people who open your view on its own.
const WORLD_LINK = 'https://unaiverse.io';
const status = document.createElement('p');
status.textContent = 'Connecting…';
document.body.append(status);
try {
// This recipe is about the real call, as in the webapp, and has no fake world.
const world = await connectToHost();
const title = document.createElement('h2');
status.replaceWith(title);
world.subscribe((node) => {
title.textContent = node.name;
});
} catch (error) {
if (error.code === 'no-host') {
// A page opened alone, outside the webapp, has no world, and the code is 'no-host'.
const title = document.createElement('h2');
title.textContent = 'This page is the view of Arena';
const text = document.createElement('p');
text.textContent = 'It works inside UNaIVERSE, where Arena lives. Open Arena there to take part.';
const link = document.createElement('a');
link.href = WORLD_LINK;
link.target = '_blank';
link.rel = 'noopener';
link.textContent = 'Open Arena in UNaIVERSE';
status.replaceWith(title, text, link);
} else {
// The other code, 'unsupported', comes from a webapp that does not speak this version of the kit.
status.textContent = `This view cannot start here (${error.code}).`;
}
}How it works
connectToHost() rejects with an error whose code says why. The code is 'no-host' when the page is not inside the webapp or when the webapp did not answer within timeoutMs (5 seconds by default). A page opened alone in a tab gets 'no-host' at once because it has no parent window to wait for. The other code, 'unsupported', comes from a webapp that does not accept the version of your kit. That code concerns the developer more than the person and needs only a short message.
The link opens in a new tab (target="_blank") and leaves the page as it is. Put the address of your world in WORLD_LINK.
See also
connectToHost: every error code.- Errors and the person's controls.
- Show a loading screen.