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

    Metabeast

    04/02/2022, 3:51 PM
    How can I forward to the Google login page and then fetch the data?
  • w

    Walshy | Pages

    04/02/2022, 3:56 PM
    https://developers.google.com/identity/protocols/oauth2
  • h

    hanpolo

    04/03/2022, 3:25 AM
    Uninstalled everything, reinstalled new wrangler@beta, now things are working. Thanks.
  • m

    Metabeast

    04/03/2022, 12:36 PM
    Is it possible to fetch something in the backend and then use it in the frontend like here for example
    Copy code
    js
    router.get('/callback', () => {
      const res = fetch('url').then(res => res.json());
    
      new Response(index, {
        myVariable: res
      })
    })
    Copy code
    html
    <div>
    <h2>${myVariable}</h2>
    </div>
  • i

    Isaac McFadyen | YYZ01

    04/03/2022, 2:02 PM
    Take a look at HTMLRewriter.
  • i

    Isaac McFadyen | YYZ01

    04/03/2022, 2:02 PM
    https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/
  • c

    cvejic092

    04/04/2022, 10:10 AM
    I'm having an issue with HTMLRewriter. I have 2 divs with different attribute names, and i want to remove one of them from DOM.But when i grab the div i want to remove with selector, and i call element.remove() or any other method that changes its structure, simply nothing happens and div stays in DOM?
  • p

    patrickheneise

    04/04/2022, 2:36 PM
    @User any idea about the cors issues? Don't know how to resolve that. I'd use the sendgrid api, but there's an error with
    require('fs')
  • h

    hanpolo

    04/05/2022, 4:13 AM
    Is there no way to pass http headers or body params to functions? I've tried using axios.get(url,{headers:{'someheader':'test'}}) and I get nothing on the function. Same with bodyParams.
  • s

    Shahzaib Sheikh

    04/05/2022, 6:57 AM
    Getting "Failed: error occurred while updating repo submodules" while building. I've checked submodule configuration. Anything that I might missing ?
  • a

    alinador

    04/05/2022, 12:07 PM
    Middleware: passing a URL to the next function works locally, but not after deployment. I am trying to rewrite URLs based on country and device (using functions, I want to mimic the page transform rule rewrite) Why do we have an option to pass a new url to the next function if it ignores and we get the original response?
  • s

    simpson

    04/05/2022, 4:04 PM
    are there any sample project on Pages (w/Functions) for Cloudflare Images? The direct creator upload is confusing
  • d

    dis01

    04/06/2022, 12:30 AM
    If useful, here are a couple of (hacky) examples of KV working with wrangler2 Pages Functions directly and also via SvelteKit. The example prepopulates the local KV stores with the Cloudflare KV data to help in local development: https://github.com/NaNorNull/Cloudflare-Pages-Functions-With-KV-and-SvelteKit
  • i

    Isaac McFadyen | YYZ01

    04/06/2022, 1:31 AM
    The SvelteKit one isn't required any more, KV bindings are available (if using Pages) on Platform which is an input to all Shadow Endpoints.
  • b

    Bytesource

    04/06/2022, 3:10 AM
    For proper page-not-found handling, Cloudflare Pages requires a file named
    404.html
    to be present in the root folder. However, my error file is located at
    /error/index.html
    .
  • b

    Bytesource

    04/06/2022, 3:13 AM
    I wrote the following middleware function to fetch this error page in case of a 404 status code:
    Copy code
    js
    // _middleware.ts
    const pageNotFoundHandler = async ({ request, next }) => {
        const response = await next();
    
        if (response.status === 404) {
            // Fetch 404 error page from where the Kirby static site generator puts it.
            const host = new URL(request.url).host 
            const errorPage = host + "/error/";
    
            const notFoundResponse = await fetch(errorPage);
    
            return notFoundResponse;
        }
        else { 
            // Page found, just return response.
            return response;
        }
    }
    
    export const onRequestGet = [ pageNotFoundHandler ];
    The problem is that this function is never called because whatever URL I type in, I always get a 200 status code back:
    GET /contactggggggggggggg 200 OK (9.58ms)
    Does anyone know why that is?
  • k

    kian

    04/06/2022, 8:11 AM
    Wouldn’t that be because your fetch for notFoundResponse was successful with a 200 and that’s what you’re returning to the user?
  • a

    alinador

    04/06/2022, 9:15 AM
    anyone from Cloudflare support? Please
  • b

    Bytesource

    04/06/2022, 10:47 AM
    If the page is not found, the 404 page at
    /error
    should be returned. Instead, the home page is returned (which Cloudflare Pages does by default if
    404.html
    is not present in the root folder). So, it seems, the middleware isn't invoked at all.
  • j

    JustinNoel

    04/06/2022, 11:28 AM
    I've got a problem with some integration between Supabase and Cloudflare Pages Functions. I think the problem is with Supabase. However, I'm wondering if it's simply possible that CF is rate-limiting requests from Supabase. Since CF Pages Functions have no logs, I can only see my own logging that I send to Logflare. If CF is rate-limiting the requests, my Function would never see them. I suspect there is no rate-limiting occurring because it doesn't happen when I use Apache Bench. However, I can't be certain without some insight from CF. Anyone have ideas on this? https://github.com/supabase/supabase/discussions/6327 ^ **UPDATE**: I've confirmed this is not a problem with Supabase. See the answer in the discussion board. The problem is with Cloudflare. It's seems my requests from Supabase are getting throttled.
  • c

    Chewey

    04/07/2022, 2:31 AM
    Hoping someone could clear up has the build env been changed to es5 now? as builds are now failing with
    Transforming async functions to the configured target environment ("es5") is not supported yet
    on the final stage However ive done no config changes (Havn't deployed in a couple weeks where it worked fine) The docs examples seem to use await which imply its fine and also mention you're unable to return a promise So what's the correct way to use fetch within a pages worker now?
  • c

    Chewey

    04/07/2022, 2:33 AM
    Copy code
    14:18:17.383      The target environment was set to "es5" here:
    14:18:17.383    
    14:18:17.383        ../../../buildhome/repo/tsconfig.json:3:14:
    14:18:17.383          3 │     "target": "es5",
    14:18:17.383            ╵               ~~~~~
    Which seems to be outside of my codebase
  • c

    Chewey

    04/07/2022, 2:33 AM
    (Fast builds is enabled)
  • c

    Chewey

    04/07/2022, 2:38 AM
    *edit the tsconfig part seems to be from mine which bumping to es6 fixes the workers Although not sure why it suddenly broke as previous builds have worked fine with that config and the relevant files that errored
  • b

    Bytesource

    04/07/2022, 12:04 PM
    In case of a 404, if no
    404.html
    is present in the root folder, does Cloudflare Pages directly fetch the home page? I'm asking because it seems that in case of a 404,
    _middleware
    is not called at all.
  • x

    xHyroM

    04/08/2022, 4:36 AM
    Can I use nodejs packages (nodemailer) in functions? I want to make a form that when you fill out the results will be sent to your email.
  • e

    Evаn

    04/08/2022, 7:30 AM
    Most node packages won't be compatible with the V8 runtime that Functions/Workers uses. For sending email in my app, I make a post request to Mailgun with
    fetch
    .
  • m

    maximillian

    04/08/2022, 8:55 AM
    Hey y'all, I'm noticing that Safari prefetches URLs before I hit enter. This creates a problem in my function because I get duplicate calls for users that type the URL in manually. Any way to deal with this? I'm looking at the headers to see if Safari sends something to distinguish a real visit from a prefetch but can't find anything...
  • e

    Evаn

    04/08/2022, 8:58 AM
    Can you move the functionality to a POST request or similar? Ideally, GETs should be idempotent such that there aren't issues from duplicate calls.
  • m

    maximillian

    04/08/2022, 9:02 AM
    Ah that's clever
1...112113114...392Latest