cloudflarecloudflare-workerscloudflare-pagescloudflare-kv

On Cloudflare Pages KV, how do you seed values for KV keys


I have a Cloudflare Pages project with functions which used KV. I wish to test my functions with KV values that are seeded before the function is called. How do I do this?


Solution

  • The solution: On the root directory of your project … i.e. where the functions directory is, create a file in a directory:

    .wrangler/state/kv/<bind-name>/<key>
    

    <bind-name> is your KV namespace.
    <key> is a file with the name of your key.

    for me bind-name is TEST, key is BUG_TEST. So my file is:

    .wrangler/state/kv/TEST/BUG_TEST
    

    Inside the file place the value for the key.

    Then when you start your wrangler use --persist. For me, using pages, the command is:

    npx wrangler pages dev --kv=TEST --persist -- npm start
    

    Also, if you are using git don’t forget to add .wrangler to .gitignore