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

    Erisa | Support Engineer

    12/10/2021, 5:25 PM
    Yeah, the
    _worker.js
    will run on any request, with the idea being that you add your own routing or custom logic instead of relying on the built-in one
  • s

    saibotsivad

    12/10/2021, 8:16 PM
    does
    _worker.js
    override all requests? aka how does one serve static assets from Pages?
  • s

    saibotsivad

    12/10/2021, 8:16 PM
    I saw the
    return env.ASSETS.fetch(request)
    snippet on the docs page, is
    ASSETS
    a special
    env
    pass-through to the static assets?
  • s

    saibotsivad

    12/10/2021, 8:17 PM
    > Then call env.ASSETS when you want to serve static assets. from another line, so I guess that's true 🤔
  • w

    Walshy | Pages

    12/10/2021, 8:17 PM
    yep
    env.ASSETS.fetch
    is the way to go
  • s

    saibotsivad

    12/10/2021, 8:21 PM
    I see in the docs, from the
    context
    object https://developers.cloudflare.com/pages/platform/functions#writing-your-first-function
    Copy code
    env, // same as existing Worker API
    but I'm having a hard time finding where
    env
    is documented in the Worker API
  • s

    saibotsivad

    12/10/2021, 8:22 PM
    a cross-link in the Pages Functions docs would be helpful, searching for "env" is a bit too wide to be helpful 😅
  • s

    saibotsivad

    12/10/2021, 8:23 PM
    I guess I found it in the new module syntax https://developers.cloudflare.com/workers/runtime-apis/fetch-event#syntax-module-worker
  • s

    saibotsivad

    12/10/2021, 8:24 PM
    but where is
    ASSETS
    coming from? something special to Pages?
  • w

    Walshy | Pages

    12/10/2021, 8:25 PM
    Yep, it's a thing set by Pages for fetching your assets
  • w

    Walshy | Pages

    12/10/2021, 8:26 PM
    The Pages docs are the best reference for how to use things like
    env
  • w

    Walshy | Pages

    12/10/2021, 8:26 PM
    The syntax differs slightly from normal Workers
  • w

    Walshy | Pages

    12/10/2021, 8:27 PM
  • s

    saibotsivad

    12/10/2021, 9:08 PM
    right, I was pointing out that the only place
    env
    is "documented" is in the "Writing your first function" code snippet, where the comment says it's "same as existing Worker API"
  • s

    saibotsivad

    12/10/2021, 9:12 PM
    (also, there's no cross-link to "existing Worker API" for
    env
    , and I can't find where it's mentioned, so effectively
    env
    is entirely undocumented at this point)
  • h

    HardAtWork

    12/10/2021, 9:25 PM
    In most cases in Workers,
    env
    basically has
    type: any
    , as the structure of
    env
    is defined by the User. The only time(that I can think of off the top of my head), where
    env
    has a predefined child is in Pages, as you mentioned, with
    env.ASSETS
    .
  • m

    maxbeyer

    12/11/2021, 6:21 AM
    Hello 👋 I'm trying to use twilio inside of a cf function. Unfortunately, their node sdk uses common js. When building, Im getting errors like
    Could not resolve "fs" (use "platform: 'node'" when building for node)
    . I tried playing with the default webpack config, but couldnt get it to work. Do you have any hints, on how to solve this issue?
  • h

    HardAtWork

    12/11/2021, 6:26 AM
    The issue there is that the Twilio SDK relies on access to the file system, which Workers does not have
  • e

    Erwin

    12/11/2021, 8:24 AM
    When that came up in the internal chat today, @threepointone said “.. and we can’t polyfill that until R2 is available..” and everyone else in chat went: 👀
  • t

    threepointone

    12/11/2021, 8:25 AM
    In this specific case, we could probably polyfill it with stubs, and just not call functions that require a file system
  • t

    threepointone

    12/11/2021, 8:26 AM
    Do you have a sample snippet of how you’re using it?
  • m

    maxbeyer

    12/11/2021, 11:40 AM
    yes, thank you very much for your help 🙏. Im just trying to generate access tokens for a user to join a video chat. https://gist.github.com/mabey01/fba5fa900b838a6e1d094fafa407d909
  • m

    maxbeyer

    12/11/2021, 11:42 AM
    the filesystem access is also a problem, but i couldnt even get it to build. It was complaining about all the require statements not being able to resolve 🤔
  • s

    saibotsivad

    12/11/2021, 9:55 PM
    I'm in the middle of trying to do something similar, but with the Stripe SDK if the thing you're doing doesn't actually use the fs/etc, you might be able to use a webpack config like they're doing in this Stripe+Cloudflare demo https://github.com/stripe-samples/stripe-node-cloudflare-worker-template with the resolve.fallback properties unfortunately that means you need a custom webpack config, which Pages doesn't support, so you have to have a build step to build the route you want
  • s

    saibotsivad

    12/11/2021, 9:57 PM
    aka if you want
    /functions/api/messages/send.js
    than you'd need something like
    /src/send-message.js
    and have Webpack build the output to
    /functions/api/messages/send.js
    as part of the build, then Pages will deploy your built script
  • s

    saibotsivad

    12/11/2021, 9:57 PM
    it's not a great DX at all, but in theory it should work (I'm still in the process of getting Stripe to work, so...)
  • k

    Keenwau

    12/11/2021, 10:08 PM
    Did you ever check this out? I am in the process of working on something on Pages and Stripe is going to be a huge part of that soon. So Im gonna be headed down the same road as you shortly. I saw this before your posts and now I am wondering what this announcement means if you are having so many issues. https://blog.cloudflare.com/announcing-stripe-support-in-workers/
  • s

    saibotsivad

    12/11/2021, 10:11 PM
    I didn't see that earlier, it would have started me in a much better place
  • s

    saibotsivad

    12/11/2021, 10:13 PM
    that blog post feels very disingenuous to me: the Stripe SDK isn't "native" in Cloudflare at all, you have to use polyfills to make it work
  • s

    saibotsivad

    12/11/2021, 10:14 PM
    if you use Webpack you have to polyfill all this junk for it to even build https://github.com/stripe-samples/stripe-node-cloudflare-worker-template/blob/master/webpack.config.js
1...394041...392Latest