function handleRequest(request, response) { if (request.queryString == "reset") { setState("index", "0"); response.setStatusLine(request.httpVersion, 200, "Ok"); response.write("Reset"); return; } let refresh = ""; let index = Number(getState("index")); // index == 0 First load, returns first meta refresh // index == 1 Second load, caused by first meta refresh, returns second meta refresh // index == 2 Third load, caused by second meta refresh, doesn't return a meta refresh let query = new URLSearchParams(request.queryString); if (index < 2) { refresh = query.get("seconds"); if (query.get("crossOrigin") == "true") { const hosts = ["example.org", "example.com"]; let url = `${request.scheme}://${hosts[index]}${request.path}?${request.queryString}`; refresh += `; url=${url}`; } refresh = ``; } // We want to scroll for the first load, and check that the meta refreshes keep the same // scroll position. let scroll = index == 0 ? `scrollTo(0, ${query.get("scrollTo")});` : ""; setState("index", String(index + 1)); response.write( ` ${refresh}

` ); }