async function handleRequest(request) {
// url rewriting
let url = request.url;
let host = request.headers.get("Host");
let path = new URL(request.url).pathname;
// redirect www.
if (url.startsWith("https://www.")) {
return Response.redirect(url.replace("https://www.", "https://"));
}
// if we are api
let isApiOrigin = (path.startsWith("/api/") || path == "/api");
if (isApiOrigin) {
url = url.replace(`https://${host}/api`, `https://api.${host}`);
} else {
// throw out query params for static pages
url = url.split("?")[0];
url = url.replace("https://","https://storage.googleapis.com/");
// if path does not contain an extension (eg /dashboard but not /script.js)
// or is explicitly a directory (eg /dashboard/) add the index.html
if (path.endsWith("/")) {
url = url.concat("index.html");
} else if (!path.split("/").pop().includes(".")) {
return Response.redirect(`https://${host}${path}/`, 308);
};
}
const response = await fetch(new Request(url,
{
body:request.body,
method:request.method,
headers:request.headers,
redirect:request.redirect,
}
));
async function handleRequest(request) {
// url rewriting
let url = request.url;
let host = request.headers.get("Host");
let path = new URL(request.url).pathname;
// redirect www.
if (url.startsWith("https://www.")) {
return Response.redirect(url.replace("https://www.", "https://"));
}
// if we are api
let isApiOrigin = (path.startsWith("/api/") || path == "/api");
if (isApiOrigin) {
url = url.replace(`https://${host}/api`, `https://api.${host}`);
} else {
// throw out query params for static pages
url = url.split("?")[0];
url = url.replace("https://","https://storage.googleapis.com/");
// if path does not contain an extension (eg /dashboard but not /script.js)
// or is explicitly a directory (eg /dashboard/) add the index.html
if (path.endsWith("/")) {
url = url.concat("index.html");
} else if (!path.split("/").pop().includes(".")) {
return Response.redirect(`https://${host}${path}/`, 308);
};
}
const response = await fetch(new Request(url,
{
body:request.body,
method:request.method,
headers:request.headers,
redirect:request.redirect,
}
));