update request url with workers static assets

seems like this should be simple, but I can't find any examples. Say I'm using workers static assets, and I have enabled run_worker_first. It seems that env.ASSETS.fetch wants the incoming request passed through - but what if I want to change which file gets fetched?
// (example using hono)
app.use('/*', async (c, next) => {
const url = new URL(c.req.url);
const urlPath = url.pathname;
if (urlPath === '/a) {
// i want to return a different file
return c.env.ASSETS.fetch('/b.html');
}
return next();
})
// (example using hono)
app.use('/*', async (c, next) => {
const url = new URL(c.req.url);
const urlPath = url.pathname;
if (urlPath === '/a) {
// i want to return a different file
return c.env.ASSETS.fetch('/b.html');
}
return next();
})
5 Replies
Walshy
Walshy2mo ago
env.ASSETS.fetch('https://assets.local/b.html);
theo
theoOP2mo ago
ok great - I figured there must be an easy way, but could not find anything in the docs or examples anywhere
Avi
Avi2w ago
can you just put any hostname in there? like example.com, and google.com, and it'll work the same...? that's how it seems to be working in my testing so far, which is very surprising behavior to me
Walshy
Walshy2w ago
yes It just needs a hostname But the hostname isn't used so it can be anything but we still expect a valid url
Avi
Avi7d ago
i assume this is part of the impetus for the unstable “getByPathname” API?

Did you find this page helpful?