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

    thebatt

    03/31/2022, 1:34 PM
    In my Function (_worker.js), env.ASSETS.fetch() will only show me my index.html with a '/' appended to the request URL, no actual files which are definitely in my output folder. Someone know why it works locally in dev to serve my files but not when deployed?
  • t

    thebatt

    03/31/2022, 2:59 PM
    And yes they are pushed to git along with the _worker.js
  • t

    thebatt

    03/31/2022, 3:01 PM
    This does have some benefits, like being able to edit KV data in your editor locally, as it saves individual files, for development and testing purposes. Saves time not having to push changes and inspect on preview or production bindings
  • h

    hanpolo

    03/31/2022, 5:59 PM
    Folks, I have a Pages SPA with a hello world function. I've got it working on Cloudflare. the '/' route works great. '/api/hello' my function works great and also '/about' which is resolved by my SPA router works fine. However, when I proxy the tooling to run it locally "npx wrangler pages dev -- npm start" and try '/about' I get a 404 error. Is this a bug in Wrangler. It seems it does not recognize this is an SPA and is either trying to find a function or a page. Any settings I'm missing?
  • h

    hanpolo

    03/31/2022, 6:00 PM
    btw .. newish to Discord .. not really sure how to use this forum
  • g

    Greg Brimble | Cloudflare Pages

    03/31/2022, 6:26 PM
    If you build the SPA and then serve a directory instead of running the proxy command, does it work?
  • g

    Greg Brimble | Cloudflare Pages

    03/31/2022, 6:26 PM
    e.g.
    npx wrangler pages dev public
  • h

    hanpolo

    04/01/2022, 2:41 AM
    Running directory results in a blank screen.
  • g

    Greg Brimble | Cloudflare Pages

    04/01/2022, 7:20 AM
    Can you make a set of reproduction steps and post as an issue on GitHub? Thanks!
  • m

    MAXOUXAX

    04/01/2022, 7:08 PM
    Hello there! đź‘‹ Currently trying to get the headers of the incoming request, but that object is undefined no matter if actual headers are set or not. When trying to use methods on the headers object, I get that error:
    Copy code
    POST /api/x/y 500 Internal Server Error (30.08ms)
    [pages:err] POST /api/x/y: TypeError: Cannot read properties of undefined (reading 'get')
    coming from that line of code
    if (!context.headers.get('content-type').includes('form'))
    Sending the test request through Hoppscotch locally, here's the curl command equivalent of the request I'm sending, as you can see there is an header set:
    Copy code
    bash
    curl --request POST \
      --url http://localhost:8788/api/x/y \
      --header 'Content-Type: JSON' \
      --data 'some json object'
    Also, here's the wrangler command I used to serve my website locally:
    wrangler pages dev --proxy 8080 -- npm run serve
  • w

    Walshy | Pages

    04/01/2022, 7:34 PM
    Hey, the
    headers
    come from Request however you're currently using it on the context. You should instead do
    if (!context.request.headers.get('content-type').includes('form'))
  • p

    patrickheneise

    04/01/2022, 9:00 PM
    Why do I get 'cors' issues when using
    fetch
    on the Functions?
  • g

    Greg Brimble | Cloudflare Pages

    04/01/2022, 10:54 PM
    Can you give an example?
  • i

    Isaac McFadyen | YYZ01

    04/02/2022, 1:31 AM
    CORS issues are usually related to not having Access-Control-Allow headers on the origin you're trying to fetch from.
  • i

    Isaac McFadyen | YYZ01

    04/02/2022, 1:31 AM
    It's a browser measure designed to prevent people from randomly fetching content from other sites without permission.
  • i

    Isaac McFadyen | YYZ01

    04/02/2022, 1:31 AM
  • t

    TrickMatt

    04/02/2022, 2:00 AM
    Shouldnt that be irrelevant when calling from CF Pages Functions (they run in a v8 isolate and not a browser respecting CORS) ?
  • p

    patrickheneise

    04/02/2022, 7:44 AM
    That’s my point. It’s a browser measure. I’m trying to post to sendgrid API and get a cord error from Functions
  • p

    patrickheneise

    04/02/2022, 7:48 AM
    That’s what I expected
  • p

    patrickheneise

    04/02/2022, 7:50 AM
    https://docs.sendgrid.com/for-developers/sending-email/curl-examples just a regular mail send I get a 400 from sendgrid. Their node SDK doesn’t work because they require fs
  • p

    patrickheneise

    04/02/2022, 7:54 AM
    Copy code
    const req = await fetch('https://api.sendgrid.com/v3/mail/send', {
          method: 'POST',
          body: JSON.stringify(email),
          headers: {
            Authorization: `Bearer ${env.SENDGRID_API_KEY}`,
            'Content-Type': 'application/json'
          }
        })
  • g

    Greg Brimble | Cloudflare Pages

    04/02/2022, 8:50 AM
    What’s the CORS error that you’re seeing? Do you see it just when using
    wrangler pages dev
    , or when you deploy to prod as well?
  • g

    Greg Brimble | Cloudflare Pages

    04/02/2022, 8:51 AM
    Are you using a meta framework such as Remix?
  • p

    patrickheneise

    04/02/2022, 12:46 PM
    I’m using a static site rendered with Next.js, the Function is triggered by a . Response is just a 400 with a link to their CORS Page, saying you should use a server-side solution to talk to their API.
  • p

    patrickheneise

    04/02/2022, 12:46 PM
    Local dev with wrangler I get the same message
  • p

    patrickheneise

    04/02/2022, 12:47 PM
    (I think; can’t verify right now)
  • i

    Isaac McFadyen | YYZ01

    04/02/2022, 1:59 PM
    They do respect CORS IIRC, since it's the
    fetch
    api.
  • i

    Isaac McFadyen | YYZ01

    04/02/2022, 1:59 PM
    Hmm.... actually, I don't know for sure.
  • w

    Walshy | Pages

    04/02/2022, 2:25 PM
    No, only browsers respect CORS. It's why you can use the Cloudflare API in a Worker
  • i

    Isaac McFadyen | YYZ01

    04/02/2022, 2:31 PM
    Ah, Ok
1...111112113...392Latest