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

    Erwin

    11/30/2021, 10:53 AM
    You obviously know a hell of a lot more about how that works than I do. But I thought the compiler was run with npx? Or am I now confusing local dev and regular builds?
  • g

    geelen

    11/30/2021, 10:59 AM
    Yeah nah npx is just for local. Build images come with the compiler preinstalled, don't want non-determinism within the builds, there's enough in the whole system :)
  • e

    Erwin

    11/30/2021, 12:14 PM
    Makes perfect sense!
  • j

    Jeremiahlee

    11/30/2021, 12:20 PM
    If I have env vars defined in the Cloudflare UI, what is the best way for those env vars to be available when running
    wrangler pages dev
    ?
  • g

    Greg Brimble | Cloudflare Pages

    11/30/2021, 12:28 PM
    You currently have to specify them with
    wrangler pages dev --binding BINDING_NAME=value
    . Would you expect them to be automatically populated?
  • j

    Jeremiahlee

    11/30/2021, 12:31 PM
    I would not expect them to be populated automatically and think that's probably a bad idea. I tried
    VAR_NAME=foo wrangler pages dev
    and was surprised that did not work.
  • c

    chrisjmccreadie

    11/30/2021, 1:54 PM
    I get this when I try to use that when binding to a KV ReferenceError: BINDING_NAME is not defined. Attempted to access binding using global in modules. You must use the 2nd
    env
    parameter passed to exported handlers or Durable Object constructors.
  • g

    Greg Brimble | Cloudflare Pages

    11/30/2021, 2:08 PM
    Yeah, just got to grab it from the context:
    Copy code
    ts
    export const onRequest = (context) => {
      return new Response(context.env.BINDING_NAME)
    }
  • m

    MrBBot

    11/30/2021, 2:18 PM
    I'll update this error message to include pages functions context 👍
  • c

    chrisjmccreadie

    11/30/2021, 2:51 PM
    Thanks a lot will try in a bit
  • j

    Jeremiahlee

    11/30/2021, 3:00 PM
    Confirmed working locally. As soon as it's in prod, I think I am ready to bring my site over from Vercel 🤞
  • m

    Marcelino Franchini

    11/30/2021, 5:52 PM
    The _headers file is compatible with Pages Functions? It seems that only works with static files and not with the functions paths.
  • s

    Skye

    11/30/2021, 5:53 PM
    functions run before headers and redirects, so if you return a response there, it won't include the headers that would have been modified
  • m

    Marcelino Franchini

    11/30/2021, 5:54 PM
    Understood, thank you.
  • h

    HardAtWork

    11/30/2021, 5:56 PM
    Now I'm wondering if underneath the hood, Pages Functions are run with the same Worker as Pages Static, or if each function is given its own service...
  • t

    tdep

    11/30/2021, 6:43 PM
    Hi, I've just started trying out this feature, when pushing and trying the functions on the *.pages.dev works fine, but I think I'm having issues testing locally. I've set up the beta for wrangler and running the following npx wrangler pages dev --proxy 3000 -- npx yarn start. I get no errors from the command and the react app loads fine. However the function endpoints are not working. Any clue? building while having to wait for pages to build the app each time is really time consuming
  • e

    Erwin

    11/30/2021, 8:38 PM
    Great to hear!
  • t

    tdep

    12/01/2021, 7:29 AM
    someone already had this issue, how did you fix it?
  • j

    johanndev

    12/01/2021, 8:19 AM
    Hi tdep - a couple of thoughts: 1. Did you pass the
    --local
    flag? 2. Are you using the correct port to access you app? The default is port
    8788
    3. Are you developing on Windows? The current version of wrangler2 has a problem with path handling, see: https://github.com/cloudflare/wrangler2/issues/51 (The workaround proposed in the issue works)
  • t

    tdep

    12/01/2021, 9:15 AM
    Hi @User thanks for the help, I develop on both macOS and Linux(ubuntu-based) so I guess point 3 doesn't apply, I have tried passing the --local flag and default port but still nothing (
    npx wrangler pages dev --local --proxy 8788 -- yarn start
    ). This is the output from the start of the proxy (nothing seems wrong here):
    Copy code
    Running yarn start...
    [proxy]: yarn run v1.22.17
    $ react-scripts start
    
    [proxy]: warning ../../package.json: No license field
    
    Compiling worker to "/var/folders/vf/w_c7xzcn451bxvlwnpv10_400000gn/T/functionsWorker.js"
    Finished in 311ms.
    [HPM] Proxy created: /  -> http://127.0.0.1:65487
    [HPM] Proxy created: /  -> http://127.0.0.1:8788
    Serving at http://127.0.0.1:8788/
    [proxy]: (node:9724) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at /Users/tdep/projects/marketplace-devenv/node_modules/postcss-safe-parser/node_modules/postcss/package.json.
    Update this package.json to use a subpath pattern like "./*".
    (Use `node --trace-deprecation ...` to show where the warning was created)
    
    [proxy]: ℹ 「wds」: Project is running at http://x.x.x.x/
    
    [proxy]: ℹ 「wds」: webpack output is served from 
    
    [proxy]: ℹ 「wds」: Content not from webpack is served from /Users/tdep/projects/marketplace-devenv/public
    
    [proxy]: ℹ 「wds」: 404s will fallback to /
  • p

    pinko64

    12/01/2021, 10:43 AM
    Can we use node related stuff in functions? For now if i for example import nodemailer it gives me errors that http, stream, child_process etc could not be resolved. example: error: Could not resolve "child_process" (use "platform: 'node'" when building for node) Is there a way to use the "platform: 'node'" or something? Where should it be put?
  • j

    Jeremiahlee

    12/01/2021, 11:08 AM
    No, Cloudflare Functions are a different runtime. It uses the APIs of browsers and is more like Deno. Node does many things in a non-browser-like way because Node added the functionality before browsers did. I think it's smart for Cloudflare to align itself with the better APIs, but it also means it's not compatible with everything in the Node ecosystem. https://developers.cloudflare.com/workers/learning/how-workers-works
  • w

    Walshy | Pages

    12/01/2021, 11:09 AM
    ^ this. What package are you attempting to use?
  • p

    pinko64

    12/01/2021, 11:11 AM
    nodemailer and googleapis
  • j

    johanndev

    12/01/2021, 2:58 PM
    Hi @User! Sorry, my wording regarding the port selection was imprecise - your initial arguments
    npx wrangler pages dev --proxy 3000 -- npx yarn start
    were correct. The
    --proxy
    argument specifies the port where your frontend-dev server is running (i.e. the your react app). Wrangler itself runs under a another port (default is
    8788
    ), which is printed in the line:
    Serving at http://127.0.0.1:8788/
    . This is the port where wrangler dev-server will route your requests to the respective function, and therefore the port which should be used to access you pages app.
  • t

    tdep

    12/01/2021, 3:49 PM
    thanks, it works!
  • j

    jschlesser

    12/02/2021, 1:13 AM
    Are there any docs on the import search path for pages-functions? Where should I stash library code that doesn't export a handler? Is there a recommended way?
  • p

    pinko64

    12/02/2021, 7:36 AM
    Is the max 30 workers script limit also in the pages/functions system? Could i have more than 30 functions inside the functions folder? What about if i have more sites and they all sum more than 30 functions?
  • w

    Walshy | Pages

    12/02/2021, 7:39 AM
    yep you can have as many functions in the functions folder. They're rolled into one Worker anyway 🙂
  • w

    Walshy | Pages

    12/02/2021, 7:39 AM
    You should also be able to have functions in all your sites without hitting the Worker limit. They shouldn't count towards that
1...252627...392Latest