Skip to content
Visitors waking the machine

Visitors waking the machine

On a portal with anonymous access, a visitor’s page load never resumes a suspended machine, and neither does a script’s fetch: it gets 503 with Retry-After. That keeps crawlers and link previews from costing you resumes, but it also means a shop’s checkout or a contact form fails while the machine sleeps.

The portal serves a small script that fixes this for requests your own pages make. The visitor’s browser solves a short proof-of-work puzzle (under a second, in the background, no captcha to click and nothing sent to anyone else) and gets a cookie valid for 15 minutes. A request with that cookie resumes the machine and waits for it, instead of the 503.

Using it

Load the script from your portal’s own address and use AgentSpacesWake.fetch where you’d use fetch for a request that must get through:

<script src="/_portal/wake.js"></script>
<script>
  // Optional: solve early, e.g. when something goes into the cart.
  AgentSpacesWake.ready();

  async function checkout(order) {
    const r = await AgentSpacesWake.fetch("/api/checkout", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify(order),
    });
    if (!r.ok) throw new Error("checkout failed: " + r.status);
    return r.json();
  }
</script>
  • AgentSpacesWake.fetch(url, init) works like fetch (same-origin cookies included). If the machine is suspended it solves the puzzle (once per 15 minutes), then sends the request again; the second request resumes the machine and waits for your service’s answer. Any other answer, your service’s own 503s included, comes back as it is.
  • AgentSpacesWake.ready() solves the puzzle now, so the first real request doesn’t wait for it. It returns a promise.
  • The edge marks its own “suspended” answer with x-agentspaces-asleep: 1, so you can tell it apart from your service’s 503s if you call fetch yourself.
  • The script is at /_portal/wake.js on every address of a portal with anonymous access, whether the machine runs or not; on other portals it is 404.

Limits

  • Visitors resume the machine at most 60 times an hour per portal, and 10 times a minute per visitor address (shared with the Activate button). Past that, requests get the 503 with Retry-After again.
  • Resumes count towards your organization’s resume rate, like any other.
  • The cookie (asp_wake) never reaches your service, and doesn’t sign anyone in: your service sees an anonymous request, without x-agentspaces-identity.
  • Webhooks from other services can’t run this script: give them a webhook address instead.
Last updated on