KV Assets (site bucket) __STATIC_CONTENT is are suffixed with hashes (05c5fa8db8)

Hi all, We're trying to use a workers sites bucket defined in our API wrangler to host some (temporary) image assets in to show in our app, but once we attempt to fetch these we see that they are suffixed with hashes. What is the recommended way to continue here so we can access assets in these buckets through e.g. /public/some-temp-image.png? We use getAssetFromKV:
return await getAssetFromKV(
{
request: ctx.req.raw,
waitUntil: ctx.executionCtx.waitUntil.bind(ctx.executionCtx),
},
{
ASSET_NAMESPACE: ctx.env.__STATIC_CONTENT,
mapRequestToAsset: (req) => {
const url = new URL(req.url);
url.pathname = url.pathname.replace(/^\/public/, "");
return new Request(url.toString(), req);
},
},
);
} catch (e) {
if (e instanceof Error) {
console.error(e);
if ("status" in e) {
if (e.status === 404) {
return ctx.notFound();
}
}
}
throw e;
}
return await getAssetFromKV(
{
request: ctx.req.raw,
waitUntil: ctx.executionCtx.waitUntil.bind(ctx.executionCtx),
},
{
ASSET_NAMESPACE: ctx.env.__STATIC_CONTENT,
mapRequestToAsset: (req) => {
const url = new URL(req.url);
url.pathname = url.pathname.replace(/^\/public/, "");
return new Request(url.toString(), req);
},
},
);
} catch (e) {
if (e instanceof Error) {
console.error(e);
if ("status" in e) {
if (e.status === 404) {
return ctx.notFound();
}
}
}
throw e;
}
Can we skip workers sites from adding these hashes?
4 Replies
James
Jamesā€¢13mo ago
What exactly do you mean by suffixed with hashes? Yes, behind the scenes in KV they are stored as some-temp-image.[hash].png for example, but you can serve that image at some-temp-image.png without the hash just fine. That's pretty much exactly the point of what kv-asset-handler does šŸ¤” Is there something I'm missing or you can clarify?
ItsWendell
ItsWendellā€¢13mo ago
@cherryjimbo thanks for your quick response, I've actually managed to find it by just reading the docs of getAssetFromKV a bit better. I forgot to add ASSET_MANIFEST as a property to getAssetFromKV, and import it like this: import manifestJSON from "__STATIC_CONTENT_MANIFEST"; For the typescript people, I also added the following declaration file:
declare module "__STATIC_CONTENT_MANIFEST" {
export default string;
}
declare module "__STATIC_CONTENT_MANIFEST" {
export default string;
}
James
Jamesā€¢13mo ago
Ah perfect, glad you've figured it out šŸ™‚
ItsWendell
ItsWendellā€¢13mo ago
Thanks! This behavior isn't in the local wrangler server, have a list of a couple of other small points that differ between cloudflare / wrangler / miniflare / workerd, I will add some issues for these soon on Github so it'll be easier for the next person šŸ˜‰