Deploy a prototype of dynamic Image rescaling to CloudFlare workers on staging
Context: https://gitlab.com/gitlab-com/gl-infra/infrastructure/issues/8209#note_238007871
Something like this should work
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let url = new URL(request.url);
let fetchParams = {
cf: {
image: {
fit: 'scale-down'
}
};
let width = parseInt(url.searchParams.get("width"), 10);
if (isNaN(width) || !width) {
return fetch(request);
} else {
fetchParams.cf.image.width = width;
}
// When we do not need the `x-with` header anymore:
// return fetch(request, fetchParams);
let response = await fetch(request, fetchParams);
response = new Response(response.body, response)
response.headers.set('x-with', 'cloudflare worker')
return response
}
According to @timzallmann These are the required routes:
- User Avatar URL
https://assets.gitlab-static.net/uploads/-/system/user/avatar/1149402/avatar.png?width=24
- Project Avatar URL
https://assets.gitlab-static.net/uploads/-/system/project/avatar/7764/about_logo.png?width=40
- Group Avatar URL
https://gitlab.com/uploads/-/system/group/avatar/9970/logo-extra-whitespace.png
so the /uploads/-/system/**
pattern should be good.
Edited by Hendrik Meyer