core icon indicating copy to clipboard operation
core copied to clipboard

KV fails on prerendering

Open TunaYagci opened this issue 1 year ago • 4 comments

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.

TunaYagci avatar Dec 01 '24 17:12 TunaYagci

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?

atinux avatar Dec 02 '24 09:12 atinux

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

TunaYagci avatar Dec 02 '24 10:12 TunaYagci

Sorry for the late answer, so you will need to connect to the production remote KV when pre-rendering?

atinux avatar Feb 04 '25 13:02 atinux

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.

TunaYagci avatar Feb 04 '25 14:02 TunaYagci