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

    grant

    12/08/2021, 12:53 AM
    I'm using the new cloudflare adapter for sveltekit and I'm trying to get a KV namespace available in one of my endpoints. I'm not sure how I can have it available in my ts file
  • e

    Erwin

    12/08/2021, 12:54 AM
    Ahh.. yes, that makes sense. I am not too familiar with the sveltekit adaptor. Do you write a regular worker and import the adaptor to work with it?
  • g

    grant

    12/08/2021, 12:55 AM
    I'm using sveltekit's way of building endpoints and the adapter transforms them into functions (
    _worker.js
    ). I may be misunderstanding what the adapter is doing, but it's working as expected in my testing. I just can't quite make the connection to get the namespaces/env variables available in them
  • e

    Erwin

    12/08/2021, 12:58 AM
    Do you have a link to the docs for them?
  • g

    grant

    12/08/2021, 12:58 AM
    Yep! https://kit.svelte.dev/docs#routing-endpoints
  • g

    grant

    12/08/2021, 12:59 AM
    Here is my endpoint, `me.json.ts`:
    Copy code
    ts
    import type { EndpointOutput } from "@sveltejs/kit"
    
    export async function get(): Promise<EndpointOutput> {
      const res: Response = await fetch('https://jsonplaceholder.typicode.com/users/1')
      const user = await res.json()
    
      return {
        body: user
      }
    }
  • g

    grant

    12/08/2021, 1:00 AM
    You can see it outputs correctly in my pages build - https://radicalmath.pages.dev/me.json
  • g

    grant

    12/08/2021, 1:01 AM
    But, I'm going to need some env variables and namespaces. I've found how others are getting them using
    VITE_
    prefixes, but not having a ton of luck
  • g

    grant

    12/08/2021, 1:04 AM
    Ok, I can get it when I use a
    .env
    file +
    VITE_
    prefix locally. Going to test deployment to see what it looks like
  • e

    Erwin

    12/08/2021, 1:07 AM
    Hmm.. I am looking at the worker file that is being generated and it doesn't seem to be supported? (https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/files/worker.js) But maybe I am missing something.. Maybe @User has more info, but he should be fast asleep now, so might take a couple hours.
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:11 AM
    Erwin’s right. I don’t think the
    env
    object (which contains your env vars and namespaces) is being passed to the renderer.
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:11 AM
    I’m not a Svelte guy myself, but usually frameworks have the notion of a ‘context’ or something which you can load up with stuff like this.
  • e

    Erwin

    12/08/2021, 1:12 AM
    The other thing Erwin was right about is that you should be asleep 😄
  • j

    Josh

    12/08/2021, 1:12 AM
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:13 AM
    And it was actually @lukeed who wrote this adapter, so he may know more about how it could possibly be passed through
  • g

    grant

    12/08/2021, 1:13 AM
    Yeah I did see that the
    env
    is getting pushed along, which may be the missing piece?
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:15 AM
    Is, or is not?
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:15 AM
    I don’t see it (but I might be blind)
  • g

    grant

    12/08/2021, 1:16 AM
    It builds svelte's
    IncomingRequest
    in these lines - https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/files/worker.js#L15-L22
  • g

    grant

    12/08/2021, 1:17 AM
    If I'm understanding it correctly. And in that function there is the
    env
    for the worker, but it doesn't pass it along or make it available anywhere
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:17 AM
    Ah, does not. We do agree.
  • g

    grant

    12/08/2021, 1:18 AM
    I did just test using environment variables in the Pages console with a
    VITE_
    prefix and it does work using [this workaround](https://dev.to/danawoodman/storing-environment-variables-in-sveltekit-2of3)
  • g

    grant

    12/08/2021, 1:19 AM
    So.... It may work with namespaces? If I prefix with
    VITE_
    ... worth a shot to try I guess
  • g

    grant

    12/08/2021, 1:20 AM
    I would be surprised, though
  • g

    grant

    12/08/2021, 1:24 AM
    I think it would be worth it to extend the
    IncomingRequest
    interface to add
    env
    ... and maybe even
    context
    ? Thoughts?
  • g

    grant

    12/08/2021, 1:24 AM
    *in the adapter
  • g

    Greg Brimble | Cloudflare Pages

    12/08/2021, 1:31 AM
    If it’s doable and doesn’t try to get serialized into the client bundle, then that sounds good to me!
  • a

    Amenadiel

    12/08/2021, 2:36 AM
    today I noticed a glitch in a vitedge deploy that was, you guessed it right, because I tried to pass VITEDGE_FOO_BAR and it should have been VITE_VITEDGE_FOO_BAR
  • s

    stonys

    12/08/2021, 10:51 AM
    Hi, spent some time figuring this out until I found your gist :) Just to be clear, in local development, actual KV values cannot be fetched from CF but has to be mocked? Line 34, where you put values in KV
  • c

    chrisjmccreadie

    12/08/2021, 10:57 AM
    Yes exactly (I can't be sure but I think wrangler just creates the kv store locally) keeping it the same name just means it flows to production without any code change required
1...333435...392Latest