core
core copied to clipboard
KV fails on prerendering
Describe the bug
When I deploy my code to cloudflare using hub's deploy command, api always returns an empty response. 👇
export default eventHandler(async () => {
return (await hubKV().get('example'))
});
whereas if I skip it during prerendering, it succeeds 👍
export default eventHandler(async () => {
// Skip during prerendering
if (import.meta.prerender) return
return (await hubKV().get('example'))
});
In KV examples and documentation this solution is not observed at all. Am I doing anything wrong? I need KV to be available during prerendering as well.
It is expected as actually when you build & pre-render, most of the time it is within a CI which has a fresh new directory, meaning that the KV store will be empty.
What data do you expect to read from?
What data do you expect to read from?
I'm expecting to read up to date data, which could also be retrieved by a database but i decided to use KV for performance benefits. Would I have this issue with cloudflare's database as well?
It is expected as actually when you build & pre-render, most of the time it is within a CI which has a fresh new directory, meaning that the KV store will be empty.
Is there any way to defeat this? Should I switch to Cloudflare's rest api? In that case I couldn't understand how I can retrieve the secrets during prerendering. I only found this documentation here
Sorry for the late answer, so you will need to connect to the production remote KV when pre-rendering?
Sorry for the late answer, so you will need to connect to the production remote KV when pre-rendering?
hey, yes, at least I expected that from the documentation. I was surprised when it didn't work.