KV Assets (site bucket) __STATIC_CONTENT is are su...
# workers-help
i
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:
Copy code
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?
j
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?
i
@James 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:
Copy code
ts
declare module "__STATIC_CONTENT_MANIFEST" {
  export default string;
}
j
Ah perfect, glad you've figured it out 🙂
i
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 😉