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

    Isaac McFadyen | YYZ01

    02/26/2022, 6:21 PM
    ^ True
  • j

    JustinNoel

    02/26/2022, 11:04 PM
    No clue really. Maybe replace the spaces with something very obvious. Like
    is replaced by
    ~~~~
    . Then, in your code, replace a string of 4 tilde's with a space?
  • r

    RaifY

    02/27/2022, 2:42 PM
    How to write tests for pages function?
  • l

    Larry

    02/27/2022, 6:08 PM
    I'm using cypress.io to test my functions. Even though cypress.io is mostly an E2E testing framework, it has
    cy.request()
    which allows you to hit the endpoint directly and observe the results. I'm sure it'll work the same with any other testing framework that allows you to hit endpoints. What do you normally use to test APIs?
  • t

    Taco Milkshake

    02/28/2022, 12:20 AM
    npx wrangler pages dev build --kv MY_NAMESPACE
    Copy code
    export async function onRequest({ env }) {
      const value = await env.MY_NAMESPACE.get('mykey')
      return new Response(value)
    }
  • t

    Taco Milkshake

    02/28/2022, 12:21 AM
    Anyone know why this KV binding works in prod, but not when using the above wrangler command locally? An empty MY_NAMESPACE is created in dev, but in prod, it can access the cloud-stored KV values. I have a binding setup in the Function settings web interface.
  • t

    Taco Milkshake

    02/28/2022, 12:39 AM
    So it appears that Miniflare is what's powering the local instance; it creates a .mf folder that stores the namespace locally.
  • t

    Taco Milkshake

    02/28/2022, 12:39 AM
    Is there no way to access a cloud KV namespace from a local dev instance of a pages site with functions?
  • t

    Taco Milkshake

    02/28/2022, 12:44 AM
    https://discord.com/channels/595317990191398933/799437470004412476/936887615137910824
  • t

    Taco Milkshake

    02/28/2022, 12:45 AM
    It seems I've found an answer. However, this is something i'd love to be able to do in the future. With workers, I can dev locally and access cloud KV instances. With functions developing locally, I cannot.
  • h

    HardAtWork

    02/28/2022, 2:22 AM
    If you use wrangler dev, you can connect and upload your Worker to a real Sandboxed Worker in the cloud. It isn’t being run locally, so it can access KV
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 2:22 AM
    ^ To add to that: wrangler2 defaults to local, so if you're using that you'll have to specify
    --local false
    .
    Apparently I'm incorrect, if you want remote development you'll need to use
    wrangler
    for now.
  • m

    minimusubi

    02/28/2022, 10:59 AM
    Regarding routing of function endpoints, in this example from the docs,
    Copy code
    /api/time => ./functions/api/time.ts
    /api/todos => ./functions/api/todos/index.ts
    /api/todos/* => ./functions/api/todos/[id].ts
    /api/todos/*/** => ./functions/api/todos/[[path]].ts
    /*/profile => ./functions/api/[username]/profile.ts
    /** => ./functions/api/[[path]].ts
    Are the last two lines incorrect? Should they instead read
    Copy code
    /api/*/profile => ./functions/api/[username]/profile.ts
    /api/** => ./functions/api/[[path]].ts
  • t

    Taco Milkshake

    02/28/2022, 6:01 PM
    Thanks. Unfortunatly using local=false with Wrangler2 is reporting
    Only local mode is supported at the moment.
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 6:01 PM
    Oh, interesting. Then yeah, you might have to use
    wrangler
    .
  • d

    Deleted User

    02/28/2022, 6:54 PM
    I'm sort of learning TS as I go here... but would someone be so kind as to explain the
    Copy code
    PagesFunction<{
      IMAGES: KVNamespace;
      DOWNLOAD_COUNTER: DurableObjectNamespace;
    }>
    return type? Is that object notation in the generic type indicating the types within
    env
    in use?
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 6:55 PM
    Yeah, that Object inside the generic indicates the keys that would be present in
    env
    , and their type so that Typescript knows, when you enter
    env.IMAGES
    for example, that it's a KV namespace.
  • d

    Deleted User

    02/28/2022, 6:56 PM
    @User okay perfect. now
    env
    is present within
    context
    (right?) so does that type have any bearing on other elements of the context?
  • d

    Deleted User

    02/28/2022, 6:56 PM
    e.g.
    request.cf
  • d

    Deleted User

    02/28/2022, 6:56 PM
    is there a type for that object too?
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 6:59 PM
    Yes,
    env
    is inside
    context
    with Pages Functions. So the other parts of the Function are included inside the PagesFunction type, since the user can't change them.
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 6:59 PM
    For example,
    request.cf
    , yeah.
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 6:59 PM
    Or
    waitUntil
    .
  • d

    Deleted User

    02/28/2022, 7:00 PM
    ahhh okay. so the importance of declaring env types is that those can be changed by the user
  • d

    Deleted User

    02/28/2022, 7:00 PM
    amazing, thanks!
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 7:00 PM
    Yup, the user can do their own things inside env (currently
    Durable Objects
    ,
    Environment Variables
    , and
    KV Namespaces
    , plus something in Beta I think, so that's the part that's variable).
  • d

    Deleted User

    02/28/2022, 7:02 PM
    love it. I was reluctant to use TS and workers-types but this is brilliant; no need to go digging for object explanations because the LSP completes everything
  • n

    Noel Jacob

    02/28/2022, 7:23 PM
    Hey what are the trade-offs between using pages functions and svelte endpoints
  • d

    Deleted User

    02/28/2022, 7:26 PM
    what does a request's
    fetcher
    do?
  • b

    balage

    02/28/2022, 7:37 PM
    hi! is it possible to server side render dynamic content using pages functions?
1...838485...392Latest