addEventListener('fetch', event => {
event.passThroughOnException();
event.respondWith(handleEvent(event));
});
async function handleEvent(event) {
const { request } = event;
const urlInfo = new URL(request.url);
const userAgent = request.headers.get('user-agent');
if (!/\\.(jpe?g|png)$/.test(urlInfo.pathname)) {
return fetch(request);
}
let width = 480;
const isPcClient = isPc(userAgent);
if (isPcClient) {
width = 1280;
}
const response = await fetch(request, {
eo: {
image: {
width,
}
}
});
response.headers.set('x-ef-client', isPcClient ? 'pc' : 'mobile');
return response;
}
function isPc(userAgent) {
const regex = /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i;
if(regex.test(userAgent)) {
return false;
}
return true;
}
本页内容是否解决了您的问题?