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

    Greg Brimble | Cloudflare Pages

    11/29/2021, 1:02 PM
    Copy code
    ts
    export const onRequest = (context) => {
      return new Response(context.env.SOME_API_KEY)
    }
    Should do it!
  • k

    Kev

    11/29/2021, 1:02 PM
    I tried doing it like I did with a just a regular worker secret, also did not work
  • k

    Kev

    11/29/2021, 1:02 PM
    Gotcha 👍
  • g

    Greg Brimble | Cloudflare Pages

    11/29/2021, 1:02 PM
    context
    has a bunch of things in it, including
    env
    which holds all your bindings.
  • k

    Kev

    11/29/2021, 1:03 PM
    Thank you!
  • o

    oliverjam

    11/29/2021, 1:54 PM
    I also ran into this issue trying to use middleware to append a cookie to an existing response (created by a previous handler). I assumed it was due to
    set-cookie
    being a "Forbidden response header name" 👻 (https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_response_header_name). I had to do something like this to create an entirely new response with the right headers:
    Copy code
    js
    let originalResponse = await next();
    let headers = new Headers(originalResponse.headers);
    headers.append("set-cookie", "test=123");
    let response = new Response(originalResponse.body, { status: originalResponse.status, headers });
  • d

    Deleted User

    11/29/2021, 4:42 PM
    is it possible to prevent certain files from being converted into workers?
  • g

    Greg Brimble | Cloudflare Pages

    11/29/2021, 4:43 PM
    If you don't export the magic handlers (
    onRequest
    ,
    onRequestGet
    etc.) the file is ignored.
  • d

    Deleted User

    11/29/2021, 4:43 PM
    ah. that's nice
  • g

    Greg Brimble | Cloudflare Pages

    11/29/2021, 4:43 PM
    There's a known bug about failing to parse non-JS files (e.g.
    tsconfig.json
    ), but we're working on it, and those will be excluded too.
  • r

    ReaverCelty

    11/29/2021, 4:46 PM
    Since we cant deploy pages + functions using wrangler yet, when it is available, will we be able to make use of
    wasm_modules
    or get access to a resources tab?
  • a

    Adriaan

    11/29/2021, 5:38 PM
    Thanks for the advice @User! With your method, the set-cookie headers have no issue in
    Copy code
    Wrangler2 pages dev
    . It's interesting that when the functions are deployed to the pages platform, you can just append the headers directly (without created a new Header class).
  • o

    oliverjam

    11/29/2021, 5:43 PM
    Yeah I wonder if that's because Wrangler2 uses Miniflare (which uses a fork of
    node-fetch
    to implement things like
    Response
    and
    Headers
    ). I did just look through the source code and I couldn't see anything in there specifically about
    set-cookie
    or forbidden response header names though 🤷
  • o

    oliverjam

    11/29/2021, 5:53 PM
    This is reproducible without middleware actually. This fails to set the cookie (the header isn't present on the response):
    Copy code
    js
    // functions/index.js
    export function onRequestGet() {
      let res = new Response("hello");
      res.headers.append("set-cookie", "test=123");
      return res;
    }
    but this works fine:
    Copy code
    js
    // functions/index.js
    export function onRequestGet() {
      let res = new Response("hello", {
        headers: { "set-cookie": "test=123" }
      });
      return res;
    }
  • o

    oliverjam

    11/29/2021, 6:15 PM
    Ah from looking at my local bundle it appears Wrangler2 might be using Undici now, which does automatically filter out forbidden response headers (https://github.com/nodejs/undici/blob/cd566ccf65c18b8405793a752247f3e350a50fcf/lib/fetch/headers.js#L213-L218). Is it worth opening an issue on the new Wrangler2 repo about the discrepancy between this and deployed Workers?
  • j

    James

    11/29/2021, 6:26 PM
    I'd say it's definitely worth opening an issue, yeah! This might be something with
    miniflare
    specifically and not
    wrangler2
    - I'm not sure, but I imagine emulating as closely as possible with local dev and prod is a goal.
  • w

    Walshy | Pages

    11/29/2021, 6:27 PM
    Ah yeah, Undici forbids set-cookie
  • w

    Walshy | Pages

    11/29/2021, 6:27 PM
  • w

    Walshy | Pages

    11/29/2021, 6:28 PM
    so yeah, report this to Miniflare
  • o

    oliverjam

    11/29/2021, 6:37 PM
    Yeah it is Miniflare, but the code in that repo is appears to be out of date with what's running within Wrangler2 on my machine—I assume there are a bunch of internal/unreleased versions—so I ended up opening this on the Wrangler2 repo: https://github.com/cloudflare/wrangler2/issues/59
  • o

    oliverjam

    11/29/2021, 6:37 PM
    I can re-open on Miniflare if that would be a better place. This might require a wider Workers discussion, since technically the prod environment isn't following the
    fetch
    spec right now 😅
  • j

    James

    11/29/2021, 6:54 PM
    There's a
    v2
    branch in miniflare with the up to date version used in wrangler2 if you wanted to take a look 🙂
  • j

    James

    11/29/2021, 6:54 PM
    That's great though - the issue can always be moved if it makes sense
  • e

    Erwin

    11/30/2021, 12:45 AM
    @User Hey you two.. the
    ctc.waitUntil
    bug should be fixed and released.. Could you please give it another go and let us know if it fixes both your issues?
  • g

    Greg Brimble | Cloudflare Pages

    11/30/2021, 12:45 AM
    Sorry, my fault for confusion there. Should be fixed in the latest
    wrangler@alpha
    , but not quite in production yet.
  • e

    Erwin

    11/30/2021, 12:46 AM
    This is a
    plinko
    thing, not Wrangler right?
  • c

    chrisjmccreadie

    11/30/2021, 7:29 AM
    quick question. I am not sure how to access the KV store I created and assigned to my CG Page in the settings functions. I run the following command (no I have to define the namespace here if so what's the format?) npx wrangler pages dev functions
  • c

    chrisjmccreadie

    11/30/2021, 7:29 AM
    ^ this the code I have written but I excepted the KV variable would show up in the context var
  • r

    rkusa

    11/30/2021, 8:00 AM
    Works for me locally now, thanks 👍 - feel free to ping me again once it got fixed in production and I'll happily give it a test their too
  • g

    geelen

    11/30/2021, 9:41 AM
    yes but new versions of the Pages Functions syntax don't get automatically rolled out to the Pages build images, that has its own release cycle
1...242526...392Latest