Hello! So I have a react/SPA app that pulls files from Cloudflare's R2 service and it works fine. I am trying to port that over to react-router (using the same Vite plugin). The R2 get() seems to be working fine, but I can't work out the secret sauce to process the returned R2ObjectBody. According to the docs here (https://developers.cloudflare.com/r2/api/workers/workers-api-reference/#r2objectbody-definition) .blob() returns a promise. I deal with this promise directly in fetch() in javascript, but I am unsure how so set this up in the loader() function?
export async function loader({ context }: Route.LoaderArgs) {
const r2Map: R2ObjectBody = await context.cloudflare.env.BUCKET.get(key);
if (!r2Map) {
throw new Response("R2 object not found");
}
r2Map.blob()
.then((mapBlob) => URL.createObjectURL(mapBlob))
.then((mapImage) => {
return { mapImage };
})
}
export default function Maps({ loaderData }: Route.ComponentProps) {
return (
<>
<div>
<img src={loaderData.mapImage} />
</div>
</>
)
}
Of course loaderData in the <img> tag is possibly undefined, which I understand. I think I am missing something simple. In a react SPA I would use setMapImage() to set this value. How do I make this happen in loader()?