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

    Isaac McFadyen | YYZ01

    03/17/2022, 2:12 PM
    So what do you mean by path-based routing? Functions definitely does path-based routing, and so does Workers using something like Itty-router.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:12 PM
    Oh, you mean redirect the traffic. Yeah, Functions can as long as it's not on the same zone.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:12 PM
    And you can rewrite if you want the URL to stay the same.
  • t

    the-philociraptor

    03/17/2022, 2:15 PM
    Yeah, I’m using AWS ALB terminology. 😂 Basically I want to do the same thing as the resolveOverride: https://support.cloudflare.com/hc/en-us/articles/206190798-Using-Resolve-Override-in-Page-Rules To allow my pages
    /api
    to go to another domain I own where the api is hosted; but still have the result appear to be coming from
    /api
    for cookies and such to work properly
  • t

    the-philociraptor

    03/17/2022, 2:16 PM
    Sounds like it will work. Just need to figure it out.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:16 PM
    Yup, very possible.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:16 PM
    What you'll want to do is proxy the request using fetch. There's an example a while ago, let me find it. 🙂
  • t

    the-philociraptor

    03/17/2022, 2:19 PM
    If I do it that way, won’t I effectively be resolving the api inside the worker and then passing it on to the client?
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:19 PM
    I can't find an example, but the basics are: - Take the incoming request and clone it so that you can edit it using
    const clonedRequest = new Request(request.body, request)
    instead of just
    clone()
    - Change the URL of the request. - Run a
    fetch
    with the Request as input. - Return the results of the fetch. That way everything about the request stays the same, except you change the URL.
  • t

    the-philociraptor

    03/17/2022, 2:21 PM
    So essentially the worker would be acting as the client to the backend api in that case. Which does not work for how I do sessions
  • t

    the-philociraptor

    03/17/2022, 2:21 PM
    That’s why I was looking at resolveOverride
  • t

    the-philociraptor

    03/17/2022, 2:23 PM
    I guess unless I pass the session cookie. But that means I have to pass it both ways. Then it seems like I have to recreate each request type as well.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:23 PM
    Well no.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:24 PM
    Because all cookies are proxied.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:24 PM
    Only the URL of the request changes.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:24 PM
    That's why you need to clone and operate on the incoming request, so everything gets passed through.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:24 PM
    Rather than just making your own fetch request.
  • t

    the-philociraptor

    03/17/2022, 2:25 PM
    So if visitor comes in with no cookie, then there is a reply that comes back with a cookie; that cookie will be sent back?
  • t

    the-philociraptor

    03/17/2022, 2:26 PM
    Am I completely off my rocker in thinking that re-creating the cloudflare native page rule is easier and effectively does the same thing? Or would that not work.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:27 PM
    No. So if a user comes in with a cookie, it's forwarded to your origin. If there's a cookie in the origin response, sure, the user gets it. But that would happen anway.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:27 PM
    And if a user comes in with no cookie and your origin rejects it the user gets the rejection as usual.
  • t

    the-philociraptor

    03/17/2022, 2:29 PM
    Okay, that sounds like it could work then. I think just for kicks I’m going to try the resolveOverride first and see if I can get that to work
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:31 PM
    Sure.
  • i

    Isaac McFadyen | YYZ01

    03/17/2022, 2:31 PM
    Just keep in mind you can't do any processing in the Function if you go that way (for example, applying custom cache rules).
  • t

    the-philociraptor

    03/17/2022, 2:34 PM
    Fair, but I don’t have a desire to do that right now anyway. A lot of my API can’t really be cached. What caching I do-do is to prevent duplicate DB calls for non-critical data and I do it inside of the API with a local TTL cache
  • t

    the-philociraptor

    03/17/2022, 2:35 PM
    Hopefully at some point it will all be re-written to function natively in functions and this is just a bandaid
  • r

    Rui Saraiva

    03/17/2022, 5:07 PM
    Over the last couple of week, I started working with some client-only routes with Gatsby and created some functions to serve the same HTML file for those specific routes (for example
    /start/**
    ) when deployed to Cloudflare Pages. One thing that I'm seeing is that I'm getting a lot more functions requests than I expected being reported on the Cloudflare Pages dashboard top right corner. Example: https://d.pr/i/kRaiso currently 5k functions requests and I barely open any off those client-only URLs today since these routes are available only on preview deployments. This is what my function
    functions/start/[[path]].ts
    file looks like:
    Copy code
    ts
    export const onRequestGet: PagesFunction = async ({ env, request }) => {
        return env.ASSETS.fetch(new Request(new URL('/start/', request.url).toString(), request));
    };
    Am I missing anything regarding functions on Cloudflare Pages?
  • j

    JuliusSeeser

    03/17/2022, 5:13 PM
    So, silly and perhaps uninformed question here: Will Functions have a "free tier" (counting towards your workers limits) or when it leaves beta will we have to migrate our projects to workers free separately? The wording (https://developers.cloudflare.com/pages/platform/functions/#pricing-and-limits): "In the future, you can expect billing to reflect that of the Workers Bundled plan." seems to indicate this is the case?
  • w

    Walshy | Pages

    03/17/2022, 5:16 PM
    Hey, This is due to a current limitation of Functions. All requests to your site will count towards the Functions count, this will be solved for GA. If you need an increase please request one here:
  • w

    Walshy | Pages

    03/17/2022, 5:16 PM
    Hey, Yes we will definitely have a free plan 🙂
1...100101102...392Latest