async function handleEvent(event) {
const { request } = event;
const urlInfo = new URL(request.url);
const regexp = /^\\/test\\/([^\\/]+)(?:\\/([^\\/]+))?\\/?$/;
if (regexp.test(urlInfo.pathname)) {
const matches = urlInfo.pathname.match(regexp);
let newPathname = '/test/';
newPathname += matches[1];
if (matches[2]) {
newPathname += '/' + matches[2];
}
newPathname += '/index.html';
urlInfo.pathname = newPathname;
}
const response = await fetch(urlInfo.toString(), {
method: request.method,
headers: request.headers,
redirect: 'manual',
body: request.body,
});
return event.respondWith(response);
}
addEventListener('fetch', (event) => {
handleEvent(event);
});
Was this page helpful?