world.setPath(path)
Writes the path of your view into the address of the webapp. A link to that address then opens your view at the same place.
setPath(path: string): Promise<boolean>
js
await world.setPath('/rooms/3');Parameters
| Parameter | What it is |
|---|---|
path | A path of at most 512 characters on the site of your view. It starts with one / and may have a query and a hash ('/rooms/3?tab=chat#top'). |
Returns
A Promise that resolves to:
truewhen the webapp accepted the pathfalsewhen the webapp ignored a path that does not start with exactly one/('rooms/3','//other.site/x'and'/\x'are ignored) or is longer than 512 characters.
Usage
Keep the open tab in the address
The webapp adds ?gui= followed by your path to its address (as in https://unaiverse.io/.../?gui=/rooms/3). Anyone who opens that address from a link, from a bookmark or by reloading the page sees your view loaded at https://your.site/rooms/3.
js
function openRoom(number) {
showRoom(number);
world.setPath(`/rooms/${number}`);
}Open your view at that place
The webapp loads your page at that path and your page reads the path on start:
js
const match = /^\/rooms\/(\d+)/.exec(location.pathname);
if (match) showRoom(Number(match[1]));Caveats
- The path starts from the root of your site. A view at
https://your.site/arena/writes'/arena/rooms/3'instead of'/rooms/3'. - No new step in the history. The address changes in place and the back button does not return to the previous path. To close a panel with the back button, use
onBack. - Your server must serve every path you write. Since the webapp loads
https://your.site/rooms/3directly, that path must return your view (a single-page app usually sends itsindex.htmlfor every path). - At most 10 a second. Beyond that rate the webapp skips the paths in between and applies only the last one after a wait.
setPathstill resolvestruefor every call. - Only while the person is on the page of your world. When the person is elsewhere in the webapp the address does not change. The webapp still keeps your last path and loads your view there the next time (after the person accepts an
askFor, for example). - Do not add steps to the history of your own page (
history.pushState) for this. Inside the webapp those entries would interfere with the back button of the webapp.setPathinstead changes the address that the person sees and shares. - A fake world changes no address. Its
setPathresolvestruefor any path including those the webapp would ignore. Check the shape of your paths yourself.
Troubleshooting
| Problem or code | Why | What to do |
|---|---|---|
It resolves false. | The path is not a path of your site (Returns). | Start it with one / and keep it within 512 characters. |
| The address does not change. | The person is on another page of the webapp or your view writes more than 10 paths a second. | Nothing. The webapp keeps the last path. |
'not-allowed-verb' | This webapp is older than your kit and does not know setPath. | Continue without it. Your view still works and only the address stays as it is. |
See also
world.onBack(handler): the back button.- Links and the back button with the recipes Keep the open tab in the address and Open your view at a place.