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

    James

    02/04/2022, 4:33 PM
    Interesting. It looks like
    ENOBUFS
    is thrown when the system is out of (or low on) resources like memory. Are you capping your system RAM? Or open files? https://stackoverflow.com/a/38019342 might be a good reference to see what other things you could be capping on your system.
  • c

    Cоlе

    02/06/2022, 6:12 AM
    is anyone have any issues with the site builder tonight?
  • c

    Cоlе

    02/06/2022, 6:12 AM
    I did a "retry deployment" tonight, and all of a sudden, all of my POST endpoints redirect to GET
  • c

    Cоlе

    02/06/2022, 6:13 AM
    only POST tho
  • x

    Xan

    02/06/2022, 9:01 AM
    Hey! I recently deployed my personal site using cloudflare pages, and so far I'm very happy. I haven't done any fullstack development in the past, but I'd love to experiment with functions. However, I'm having trouble finding documentation on how to connect your functions to the frontend properly. I'm mostly working with React. I tried following https://blog.cloudflare.com/building-full-stack-with-pages/ but got stuck at "Integrating as Jamstack". My frontend does not receive the expected response. Could anyone point me in the right direction? (haven't worked with workers before)
  • w

    Walshy | Pages

    02/06/2022, 10:25 AM
    Hey, I'd recommend checking out our documentation on Functions - https://developers.cloudflare.com/pages/platform/functions If you have anymore questions feel free to ask 🙂
  • s

    simplenotezy

    02/07/2022, 8:43 AM
    When using Cloudflare Pages functions, can one grab environment variables from `process.env.SOME_ENVIRONMENT`or do we have to pass the environment variables into the underlying functions, using the `env`from
    async ({ request, next, env, waitUntil })
    ?
  • h

    HardAtWork

    02/07/2022, 8:58 AM
    You have to pass in env from the root function
  • x

    Xan

    02/07/2022, 4:53 PM
    what is the best way of handling secrets, like API keys? is it safe to put them in the environment variables of the function? should they be handled in another way?
  • x

    Xan

    02/07/2022, 5:26 PM
    I suppose I should use the KV key-value storage?
  • i

    Isaac McFadyen | YYZ01

    02/07/2022, 5:28 PM
    Depends on what they are for. Are they user's API keys, or ones that your function uses to call external services?
  • x

    Xan

    02/07/2022, 5:35 PM
    keys my function use for external services
  • i

    Isaac McFadyen | YYZ01

    02/07/2022, 8:31 PM
    KV could work then, or you could use environment variables. No secrets yet for Pages Functions (although they are planned), so depends on who has access to your account to view those.
  • i

    Isaac McFadyen | YYZ01

    02/07/2022, 8:32 PM
    I would go environment variables though, since KV can be viewed from the dashboard anyway so it's not any extra protection. 🙂
  • e

    Erisa | Support Engineer

    02/08/2022, 5:59 AM
    Env vars are used for building and thus passed around quite a bit more, if your secret was only for runtime I could see KV as being an okay place to store them
  • s

    simplenotezy

    02/08/2022, 7:04 AM
    thanks @User. It would be great if Functions supported some more simple ways, like
    process.env.SOME_VAR
    because I had to modify a lot of functions to accept an extra parameter (the secrets).
  • a

    Ap369

    02/08/2022, 5:52 PM
    When functions will be production ready?
  • h

    HardAtWork

    02/08/2022, 5:52 PM
    Best answer is
  • r

    Rubidot

    02/09/2022, 2:51 AM
    Hi, I'd like to have a catch-all function that would respond to any route that isn't caught by a more specific function or static file. For example, this file structure:
    Copy code
    hello.html
    functions/[[catchall]].js
    functions/[rootlevel].js
    I would expect the routs to match like this:
    Copy code
    /hello -> hello.html
    /dim   -> [rootlevel].js
    /dim/sum -> [[catchall]].js
    However, with this setup, all requests get routed to my
    [[catchall]].js
    function, and there are no routes that lead to my other files. Is this the intended behavior? So far, I've only tried this using
    wrangler pages dev
    .
  • j

    JustinNoel

    02/09/2022, 7:26 AM
    If you have a structure like:
    Copy code
    hello.html
    functions/api/
      [index].js
      [[nested]].js
    You should catch:
    /hello
    : by
    hello.html
    /api/abc
    : by
    index.js
    /api/123
    : by
    index.js
    /api/users/
    : by
    nested.js
    /api/users/123
    : by
    nested.js
    /api/comments/
    : by
    nested.js
    /api/comments/123
    : by
    nested.js
    []
    is for anything at the current directory
    [[]]
    is for nested stuff https://developers.cloudflare.com/pages/platform/functions#functions-routing P.S. You can't have an
    .html
    file in your
    functions
    directory.
  • r

    Rubidot

    02/09/2022, 7:30 AM
    Oh, yes, the html is outside the functions directory. Updating my example, and I will review my code again. Thanks, I appreciate you taking the time to look at this.
  • r

    Rubidot

    02/09/2022, 11:44 PM
    @User I setup another test and explored some more. Precedence between functions works as expected. What was throwing me off is that static files always fall behind functions. In your example, this doesn't come up because the functions are all under the /api/ route and the html file is at root level. Extending the example to show the conflict I'm talking about:
    Copy code
    hello.html
    api/invisible.html
    functions/api/
      [index].js
      [[nested]].js
    In this example,
    api/invisible.html
    is inaccessible, as all routes starting
    api
    get served by either
    api/[index].js
    or
    api/[[nested]].js
    This seems like a reasonable limitation; just want to understand it correctly. I was trying to make a 404 function instead of having to use a static 404.html file, but it doesn't seem like that's possible while keeping static files available.
  • e

    Erwin

    02/10/2022, 1:06 AM
    You can make them work. In your environment is a binding for the static assets. So in your function you can request the static file for that request. And if there is something, you can return it and if not return a 404 page
  • r

    richardcooke

    02/10/2022, 11:52 AM
    I've been trying out pages functions to deploy our various dashboards at the startup I work for and thought it might be useful to share my experience/pain-points/workarounds. Firstly workers are amazing and be able to use them with the git workflow is so good! For context we’re deploying SSR React apps using the
    _worker.js
    format. * When deploying the CF build automatically runs
    npm install
    , this is an issue because we’re using PNPM and
    npm install
    fails with an error. There seems to be no way to prevent
    npm install
    from running. On Netlify it would be possible to pass
    NPM_FLAGS
    where we could set
    —version
    to avert the install, in all honestly automatically running
    npm install
    feels a bit presumptuous. We’ve added a script on the preinstall hook that renames the package.json and replaces it with an empty one, then on postinstall puts the original back. We have to set
    NPM_VERSION
    to 6 to because NPM 7+ runs the preinstall hook after the install 🤦‍♂️ * I think this has been raised before but it took me a while to figure out
    env.ASSETS.fetch
    requires some part of the original request, passing the URL or constructing a new request won’t work like it does in wrangler. * We're using streams to stream our app to clients, but workers have the readable stream constructor hidden behind the
    streams_enable_constructors
    flag, there's no way to set
    compatibility_flags
    as far as I’m aware in pages functions. This is our biggest blocker as we have to disable the streaming response. * As far as I can see preview deploys on custom domains are not possible (correct me if I'm wrong). It would be great if we could set a custom default domain which had all of the preview deploys and was also what was published as the preview link in PR's. We’re going to look at using another worker to route requests to achieve this. I might be missing something, if there is a better way to do any of this it would be great to hear 🙂
  • g

    geelen

    02/10/2022, 12:29 PM
    Wow, you're pretty much pushing everything to its limit @User !
  • g

    Greg Brimble | Cloudflare Pages

    02/10/2022, 12:38 PM
    > I think this has been raised before but it took me a while to figure out env.ASSETS.fetch requires some part of the original request, passing the URL or constructing a new request won’t work like it does in wrangler. https://github.com/cloudflare/wrangler2/issues/165 > As far as I can see preview deploys on custom domains are not possible (correct me if I'm wrong). It would be great if we could set a custom default domain which had all of the preview deploys and was also what was published as the preview link in PR's. We’re going to look at using another worker to route requests to achieve this. With preview branch aliases, you can hack something together, but it's a bit of a faff.
  • g

    Greg Brimble | Cloudflare Pages

    02/10/2022, 12:38 PM
    @User tag custom-branch-domain
  • d

    Dyno

    02/10/2022, 12:38 PM
    To use a custom domain on a specific branch of your Pages project, first follow the instructions to add the custom domain to your Pages project. If your custom domain is not whitelisted on Pages, this will not work. Next, go to your DNS provider, and find the CNAME record for your custom domain pointing to your Pages.dev domain. Add the branch name to the front of your Pages.dev domain, so a CNAME for
    dev.mywebsite.com
    pointing to
    excelsior.pages.dev
    would be changed to point toward
    dev.excelsior.pages.dev
    .
  • e

    Erwin

    02/10/2022, 12:43 PM
    A few minutes later in the internal pages chat channel @User:
    Copy code
    Jira Create, Bot, 8 min
    NEW: PAGES-XXX : Backlog : Allow specification of Compatibility Date   (glen)
  • r

    richardcooke

    02/10/2022, 12:50 PM
    Regarding preview branch aliases would this also work for feature branches created for PR's where the pages sub domain is created with the PR? The reason this is an issue for us currently is how our auth cookies currently work (we can't authenticate on a pages.dev domain).
1...747576...392Latest