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

    Vero 🐙

    01/03/2023, 11:18 AM
    Please check this to learn how to deploy functions - https://developers.cloudflare.com/pages/platform/functions/get-started/ and if you have more questions, let us know 🙂
  • Workers, and their CPU time limit
    j

    Jakob

    01/03/2023, 1:09 PM
    Hi! I'm currently working with functions & pages for the first time, and I have a question regarding their cap for CPU time. We have a function that has a median CPU time of 200ms (althought with very low req/s number). But the docs for the free plan, which we can hopefully stay on, says up to 10ms CPU time per request. My question is, how does that work? We're already breaking that cap by 20x. Is it accumulative per month or something? Or are we going to be forced somehow cut that down to 50ms, which is the starting tier for paid plans?
    e
    • 2
    • 2
  • c

    coldhands

    01/03/2023, 9:11 PM
    Hello, I am using Functions, but my question is not Functions-specific. I'm trying to send 2 cookies at once in the
    Set-Cookie
    header, but the browser recognizes it as a single cookie (it should split them into 2). The created cookie has a name and a value from the first one and other attributes like
    Path
    and
    Max-Age
    from the second one. My code:
    Copy code
    ts
    // Next13, Edge Runtime
        const headers = new Headers({
          'Content-Type': 'application/json;charset=UTF-8',
          'Set-Cookie': this.responseCookies.join(', '),
        });
    
        return new Response(JSON.stringify({ message: 'Sent' }), {
          status: 200,
          headers,
        });
    I've also built one local worker that uses Hono to send the same cookies and it works perfectly. this.responseCookies looks like this:
    Copy code
    ts
    [
      'refresh-token=...; Max-Age=2592000; Path=/api/auth/refresh; HttpOnly; SameSite=Lax',
      'access-token=...; Max-Age=120; Path=/; HttpOnly; SameSite=Lax'
    ]
  • k

    kian

    01/03/2023, 9:41 PM
    > but the browser recognizes it as a single cookie (it should split them into 2) I don't think it should - Set-Cookie should not be folded.
  • k

    kian

    01/03/2023, 9:41 PM
    Use
    headers.append
    to add multiple
    Set-Cookie
    headers.
  • c

    coldhands

    01/03/2023, 10:08 PM
    It does get split when I do it in Hono (image below). Changed
    set
    to
    append
    , still does not work. This is my code now
    Copy code
    ts
        const headers = new Headers({ 'Content-Type': 'application/json' });
        this.responseCookies.forEach((cookie) => {
          headers.append('Set-Cookie', cookie);
        });
    
        console.log('all cookies', headers.get('Set-Cookie'));
    
        return new Response(JSON.stringify({ message: 'Sent' }), {
          status: 200,
          headers,
        });
    I even tried everything ChatGPT told me and eventually it ran out of ideas 😢
  • s

    Skye

    01/03/2023, 10:11 PM
    When you have more than one header with the same name, you must use
    headers.getAll('Set-Cookie')
    to see them
  • c

    coldhands

    01/03/2023, 10:13 PM
    I think that
    Headers
    are extended in CF Workers, but I don't think
    getAll()
    is generally available
  • s

    Skye

    01/03/2023, 10:14 PM
    It's a special method made for workers, which explains why it won't be in MDN
  • s

    Skye

    01/03/2023, 10:14 PM
    As for the type issue, I believe it's only in the v4 of workers types
  • s

    Skye

    01/03/2023, 10:23 PM
    See the first bullet point of https://developers.cloudflare.com/workers/runtime-apis/headers/#differences
  • f

    Firefly

    01/04/2023, 6:46 AM
    Hi guys Do
    pages
    functions run on node or something more like v8? Looking to see if I can get a better understanding of which API's I can use, I am assuming the same as functions (https://developers.cloudflare.com/workers/runtime-apis/) can anyone confirm this?
  • k

    kian

    01/04/2023, 8:02 AM
    Yes, Functions are just Workers
  • k

    kian

    01/04/2023, 8:02 AM
    It’s all V8
  • h

    HMM

    01/04/2023, 7:27 PM
    Im trying to add a service binding to pages functions and I am getting this error. Ive noticed I only get the error when trying with my workers that have underscores in the name. Is there a restriction put on the service names?
    Invalid Service name (name_of_worker). Check your Service name and try again. (Code: 8000022)
  • w

    Walshy | Pages

    01/04/2023, 7:29 PM
    lovely... the dash doesn't accept underscores but looks like the API does
  • w

    Walshy | Pages

    01/04/2023, 7:29 PM
    I went off the dash for validation
  • h

    HMM

    01/04/2023, 7:31 PM
    Ah ok, I had created my workers with wrangler. Thanks
  • w

    Walshy | Pages

    01/04/2023, 7:32 PM
    made a ticket but OOO so can't fix right now
  • f

    fflaten

    01/05/2023, 12:29 PM
    Hello 🙂 Can functions in a pages project depend on other npm-pacakges or will that require a standalone worker-project?
  • s

    Skye

    01/05/2023, 12:31 PM
    Yes, your functions are bundled with esbuild, so any npm packages that work in V8 (meaning they don't depend on node-specific things), should all work fine
  • f

    fflaten

    01/05/2023, 12:32 PM
    Any guides/samples on this? Have only seen standalone functions/helloworld.js samples for functions, nothing with package.json etc.
  • j

    James

    01/05/2023, 12:34 PM
    This is good feedback - it should probably be made more clear. It should ‘just work’ though - place the package.json in your root as per normal, and then a functions folder alongside. Anything you import within functions should be bundled.
  • f

    fflaten

    01/05/2023, 12:37 PM
    Oh, I can use the reuse the root-level package.json used for my web framework? 😮
  • j

    James

    01/05/2023, 12:41 PM
    Yeah, just treat functions like a normal folder in your project
  • f

    fflaten

    01/05/2023, 12:43 PM
    Awesome! And AFAIK functions have more or less the same limitations as workers right? So technically https://github.com/PaganMuffin/og-image-test should be achievable in functions also?
  • s

    Skye

    01/05/2023, 12:44 PM
    It's not yet possible to upload wasm to functions but I've seen a bit of talk about it recently so hopefully that's on the way
  • f

    fflaten

    01/05/2023, 12:46 PM
    Delay or worker it is 🙂 Thanks for the excellent help!
  • w

    Walshy | Pages

    01/05/2023, 1:58 PM
    Can you make a docs issue
  • j

    James

    01/05/2023, 1:58 PM
    !remind 6h functions docs issue package.json
1...324325326...392Latest