https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • j

    jschlesser

    12/03/2021, 11:51 PM
    has anybody used the wrangler2 miniflare integration to get KV working in your pages-functions for local testing and development? If so, what config magic did you have to do to tell miniflare to set up local KV namespaces? From the blog post announcement "Backed by Miniflare, you can run your entire application locally with support for mocked secrets, environment variables, and KV (Durable Objects support coming soon!)." I assume this means that KV works today, but im not sure if I need to create a wrangler.toml in a pages project or if there is something else.
  • j

    jschlesser

    12/04/2021, 12:00 AM
    npx wrangler pages dev --help
    has the answers
  • e

    Erwin

    12/04/2021, 12:04 AM
    It does indeed.. Did you get it working?
  • j

    jschlesser

    12/04/2021, 12:05 AM
    Working through it now, ill report back on my findings when I know its either working or not
  • j

    jschlesser

    12/04/2021, 12:26 AM
    I haven't been able to get it working. The --help docs to me are ambiguous. My command to start my dev environment works without the --kv option, but im not sure where im supposed to put the --kv option and not sure what the array format needs to look like. My non KV command is
    npx wrangler pages dev -- npm run dev
    . That successfully starts up my sveltekit based pages local instance with functions working and hot reload etc... I tried the following and it didnt work
    npx wrangler pages dev --kv=MYKV -- npm run dev
    . That started up fine but crashes on the first page load. when I move the --kv option to right after wrangler it also crashes on the first page load.
  • e

    Erwin

    12/04/2021, 12:36 AM
    Hmm.. what does the crash say? (I am going to be afk on and off for the next few hours.. so delayed responses)
  • j

    jschlesser

    12/04/2021, 12:38 AM
    Note that my my actual KV namespace is different than MYKV.
    Copy code
    /Users/js/Projects/surfthingy-pages/node_modules/wrangler/wrangler-dist/cli.js:27069
                throw ex;
                ^
    ReferenceError: CDIPFiles is not defined.
    Attempted to access binding using global in modules.
    You must use the 2nd `env` parameter passed to exported handlers or Durable Object constructors.
        at EventTarget.get (file:///Users/js/Projects/surfthingy-pages/node_modules/@miniflare/core/dist/src/index.js:941:19)
        at onRequestGet (/var/folders/3p/8_1vh8sd2qg42gzpyf4zlgb80000gn/T/functionsWorker.js:3:3)
        at next (/var/folders/3p/8_1vh8sd2qg42gzpyf4zlgb80000gn/T/functionsWorker.js:364:32)
        at Object.fetch (/var/folders/3p/8_1vh8sd2qg42gzpyf4zlgb80000gn/T/functionsWorker.js:369:14)
        at file:///Users/js/Projects/surfthingy-pages/node_modules/@miniflare/core/dist/src/index.js:1004:19
        at file:///Users/js/Projects/surfthingy-pages/node_modules/@miniflare/shared/dist/src/index.js:150:9
        at EventTarget.<anonymous> (file:///Users/js/Projects/surfthingy-pages/node_modules/@miniflare/shared/dist/src/index.js:173:9)
        at EventTarget.[nodejs.internal.kHybridDispatch] (node:internal/event_target:562:20)
        at EventTarget.dispatchEvent (node:internal/event_target:504:26)
        at EventTarget.dispatchEvent (file:///Users/js/Projects/surfthingy-pages/node_modules/@miniflare/shared/dist/src/index.js:165:18)
    
    Node.js v17.1.0
    npm ERR! code 7
    npm ERR! path /Users/js/Projects/surfthingy-pages
    npm ERR! command failed
    npm ERR! command sh -c wrangler "--kv=CDIPFiles" "pages" "dev" "--" "npm" "run" "dev"
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /Users/js/.npm/_logs/2021-12-04T00_20_03_821Z-debug.log
  • j

    jschlesser

    12/04/2021, 12:46 AM
    Also thanks for responding at all. I get that its beta and it's Friday. This is a hobby project that I'm migrating from a workers site project before I do a few Christmas feature upgrades (a present for my friends). And for what it's worth, the new wrangler local development and pages with functions is so so so much better as a developer experience.
  • e

    Erwin

    12/04/2021, 2:27 AM
    Ahh.. it is already very much Saturday over here in Australia 🙂
  • e

    Erwin

    12/04/2021, 2:32 AM
    Right.. the issue seems to be that you are using global variables to reach your KV Namespace in your code. If I remember correctly it was
    context.env.CDIPFiles
  • o

    oliverjam

    12/04/2021, 11:13 AM
    The error hasn't been updated for Pages Functions I assume (
    You must use the 2nd env parameter passed to exported handlers or Durable Object constructors.
    ). You want to access the KV namespace like this:
    Copy code
    js
    export function onRequestGet({ env }) {
      console.log(env.CDIPFiles);
    }
  • d

    dis01

    12/04/2021, 4:36 PM
    I have been able to get KV to work with Pages - Functions - locally: The command used was: > npx wrangler@User pages dev .\.svelte-kit\cloudflare\ --kv=CFKVDB --binding=ALLOW_TEST="True" The code was in /functions/query/[[path]].js > export async function onRequest(context) { > const ALLOW_TEST = (context.env.ALLOW_TEST === "True"); > const KV = context.env.CFKVDB; > ... A couple of suggestions or requests for enlightenment: 1). wrangler.toml is already setup with these, it would be nice if that could be used. (tried using --config with both absolute and relative paths - on Windows) 2). Also note the code path above. This appears to be escaped incorrectly when generated. The generated functionsWorkers.js has > routePath: "/query:path*", If changed to the following it works fine > routePath: "/query/:path*",
  • i

    ian

    12/04/2021, 7:48 PM
    Is there a way to export a Durable Object class from a Pages worker and then bind it? Or do I need to deploy Durable Objects completely separately from Pages functions?
  • v

    VAN

    12/04/2021, 11:01 PM
    I know miniflair lets you make a persistent local copy of a KV store. is this possible in the wranger@beta? adding --kv-persist didn't seem to do anything
  • a

    albedo

    12/05/2021, 2:31 AM
    has anyone sucessfully tried to make a protected api using cloudflare access and pages functions?
  • m

    Mortal

    12/05/2021, 4:20 AM
    lmao, you triggered the man
  • g

    Greg Brimble | Cloudflare Pages

    12/05/2021, 10:51 AM
    Currently needs to be deployed separately. We’re thinking hard about how to simplify this, but no ETA just yet.
  • e

    Erwin

    12/05/2021, 10:54 AM
    Not in particular with pages + functions.. but have with regular workers.. what are you wanting to do?
  • c

    chrisjmccreadie

    12/05/2021, 10:54 AM
    Thanks a lot @User I have been stuck on this for a week. One other thing I noticed is that when you use put to store in the KV is does not show in the Cloudflare dashboard but if you use list you see them.
  • e

    Erwin

    12/05/2021, 10:55 AM
    If you put something in KV during local development it is only stored in local memory, not your actual KV store
  • c

    chrisjmccreadie

    12/05/2021, 10:57 AM
    ah, that makes sense thanks for the clarifcation
  • e

    Erwin

    12/05/2021, 10:57 AM
    Which makes perfect sense if you think about it. But I have been caught out by it before..
  • c

    chrisjmccreadie

    12/05/2021, 10:58 AM
    the thing that stumped me was I did not know you had to add --kv and --binding in the command line the documentation says one or the other. Maybe that could be updated to be a little more clear.
  • c

    chrisjmccreadie

    12/05/2021, 10:59 AM
    I knows it's early days for functions and all these little things will be iterated on
  • n

    Nik

    12/05/2021, 2:18 PM
    Hey everyone, I am currently trying to implement a pages function but I can't get the functions route to work/be recognised correctly. I already started a thread in the cloudflare community forum. Maybe one of you guys got an idea on what I'm doing wrong: https://community.cloudflare.com/t/can-pages-and-workers-be-combined/329002
  • c

    chrisjmccreadie

    12/05/2021, 2:32 PM
    @Nik is the worker in the functions folder?
  • c

    chrisjmccreadie

    12/05/2021, 2:36 PM
    I looked at the issue you posted when you got to mydomain.com/mytest/test/
  • c

    chrisjmccreadie

    12/05/2021, 2:36 PM
    you get nothing ?
  • c

    chrisjmccreadie

    12/05/2021, 4:50 PM
    hay all, After struggling with KV and Cloudflare Pages I thought I would put together this quick Gist so people can get up and running a little quicker. I think it covers all the gotchas. https://gist.github.com/cryptoskillz/98b8e7090b7cc8d51531bb9dcfe7654a
  • c

    chrisjmccreadie

    12/05/2021, 4:52 PM
    needless to say a lot of the stuff in the this Gist was answers a lot of people gave me when I got stuck.
1...303132...392Latest