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

    James

    01/02/2022, 5:00 PM
    I would be very shocked if you need to manually cache DNS lookups. I'd definitely recommend you benchmark that to see if there's any measurable change before worrying about it.
  • w

    William | Chaos Management

    01/02/2022, 5:04 PM
    I doubt I need to cache it, I’d like to benchmark this, but I’m not sure how to do that
  • s

    steranevdy

    01/03/2022, 3:23 AM
    how do people implement timeout with worker
    fetch
    ?
  • e

    Erwin

    01/03/2022, 3:43 AM
    I haven't tried it personally, but Workers should support
    AbortController
    now.. so https://developer.mozilla.org/en-US/docs/Web/API/AbortController should work
  • h

    HardAtWork

    01/03/2022, 3:44 AM
    Damn. Started building a thing with
    Promise.race()
    , but that is much cleaner.
  • e

    Erwin

    01/03/2022, 3:46 AM
    So much that indeed.. especially since this one actually terminates the fetch, which makes a difference if you are running into the concurrent connection limit 🙂
  • h

    HardAtWork

    01/03/2022, 4:00 AM
    Something like this? Can't find a website that loads really slow to test it, off the top of my head.
    Copy code
    js
    const betterFetch = async(e,t)=>{const r=new AbortController,o=r.signal,i=await Promise.race([new Promise(((e,t)=>setTimeout(e,s,"request timed out"))),fetch(e,{signal:o,...t})]);var s;if("request timed out"===i)throw r.abort(),i;return i};
  • c

    Cloudflare Workers Bot

    01/03/2022, 4:00 AM
    Copy code
    javascript
    addEventListener('fetch', event => {
      event.respondWith(handleRequest(event.request))
    })
    const sleep = delay => new Promise((resolve, reject) => setTimeout(resolve, delay, "request timed out"));
    
    
    async function handleRequest(req) {
      return betterFetch(req);
    }
    const betterFetch = async (o, t) => {
      const abortController = new AbortController(),
       signal = abortController.signal,
        res = await Promise.race([sleep(2000), fetch(o, {signal, ...t})]);
      if(res === "request timed out") {
        abortController.abort();
        throw res;
      }
      return res;
    }
  • e

    Erwin

    01/03/2022, 4:10 AM
    Fetch a worker.dev URL that just sleeps for 30 seconds before returning Hello World?
  • h

    HardAtWork

    01/03/2022, 4:11 AM
    Now I have to log in...
  • e

    Erwin

    01/03/2022, 4:15 AM
    I am so sorry 😦
  • s

    steranevdy

    01/03/2022, 5:49 AM
    i have some functions returning 500, how to log the server errors?
  • h

    HardAtWork

    01/03/2022, 6:52 AM
    Wrap your entire function in a try/catch, and send all errors to a provider like logflare.
  • s

    SirJosh

    01/03/2022, 7:21 AM
    how do i control how cloudflare pages functions builds? i notice that cloudflare pages seems to be running some extra commands when building my project locally, i only see:``` > npm run build > voxylstats@ build /tmp/voxylstats > cross-env NODE_ENV=production remix build Building Remix app in production mode... (node:17639) ExperimentalWarning: The ESM module loader is experimental. Built in 397ms ```yet when building for pages, i see the following additional output (see attachment) this is a problem for me, because i'm overriding how
    esbuild
    is normally called to introduce some plugins for node compat (see attached patchfile). is there any way to modify the behavior of cloudflare pages when building functions?
    cloudflarepages-functions-compiler0-3-8
  • h

    HardAtWork

    01/03/2022, 7:33 AM
    Maybe if you use a _workers.JS file?
  • s

    SirJosh

    01/03/2022, 7:34 AM
    do you have resources on what a
    _workers.JS
    file is?
  • s

    SirJosh

    01/03/2022, 9:18 AM
    update to this: i worked around the problem by stubbing the file system with node modules and applying patches to modules that were being weird
  • s

    SirJosh

    01/03/2022, 9:18 AM
    unfortunately, cloudflare seems to really not like my workaround
  • s

    SirJosh

    01/03/2022, 9:20 AM
    it'd be really nice if i could get this working :'(
  • s

    SirJosh

    01/03/2022, 9:23 AM
    i'm gonna go to sleep so i'm not sure i'll be online soon enough if anyone wants to take a stab and look into this dunno what to provide that would make y'all's (cloudflare devs) lives easier, so here's the repo and site
  • s

    SirJosh

    01/03/2022, 10:11 AM
    not sure if i should ping a @ workers community team or @ community champion to punt this up to the appropriate developers or not
  • s

    steranevdy

    01/03/2022, 10:47 AM
    i'm seeing different behavior when using local HTMLRewriter vs on preview env
  • s

    steranevdy

    01/03/2022, 10:51 AM
    i'm trying the get the page title using HTMLRewriter, for some reason, if the characters are in japanese, on preview env, it's not returning anything
  • s

    steranevdy

    01/03/2022, 10:51 AM
    on local, when using wrangler2, it's returning something
  • m

    maximillian

    01/03/2022, 2:25 PM
    I'm trying to use a node package in a function. Is this possible? I'm getting a bunch of errors along the following lines:
    Copy code
    `The package "XXX" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.`
  • m

    maximillian

    01/03/2022, 2:28 PM
    Can I set different platforms for functions, or is this only for workers?
  • m

    maximillian

    01/03/2022, 2:57 PM
    I'm trying to follow this tutorial: https://blog.cloudflare.com/announcing-stripe-support-in-workers/
  • m

    maximillian

    01/03/2022, 2:59 PM
    The articles refers to this example project with a
    functions
    folder, but it doesn't have one... https://github.com/cloudflare/stripe.pages.dev
  • m

    maximillian

    01/03/2022, 2:59 PM
    Is stripe compatible with
    functions
    or not?
  • w

    Walshy | Pages

    01/03/2022, 3:00 PM
    Yep it is
1...515253...392Latest