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

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:45 AM
    You’ll also want to read from the cache with the same value as you’re putting in.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:45 AM
    You’re `put`ting in a
    cacheKey
    but `match`ing on the Request.
  • c

    Cоlе

    01/17/2022, 7:38 AM
    what should I be calling
    waitUntil
    on? I know normally in workers you call it on event, but can I access the event from a function?
  • c

    Cоlе

    01/17/2022, 7:40 AM
    good catch. I'm making sure to
    put(cachekey
    now, but it doesn't seem to cache. the cache folder doesn't appear in
    .mf/
    anymore
  • c

    Cоlе

    01/17/2022, 7:46 AM
    *
    Copy code
    ts
    export const onRequestGet: PagesFunction<{}> = async ({ request, env }): Promise<Response> => {
        const doc = "getKits";
        const subject = "0042N";
        cache = cache || await caches.open('kits');
    
        const cacheKey = new Request(request.url, {
            headers: request.headers,
            method: 'GET'
        });
    
        const cached = await cache.match(cacheKey);
    
        console.log(cached);
        if (cached) {
            const cloned = cached.clone();
            cloned.headers.set('cf-cache-status', 'HIT');
            return cloned;
        }
    
        let real = GetMongoDocument(env, collectionName, { _id: query(subject!) }).then(async (res) => {
            const response = toJSON(res);
            cache.put(cacheKey, response.clone());
            response.headers.set('cf-cache-status', 'MISS');
            return response;
        }).catch((err) => {
            return toError(Errors.ERR_INTERNAL_ERROR, doc, err);
        });
    
        return real;
    };
  • z

    Zelderon

    01/17/2022, 8:39 AM
    I have a function like the following
    Copy code
    export async function onRequestGet() {
        let request = new Request(`https://mapi.storyblok.com/`);
        let res = await fetch(request)
        return new Response(JSON.stringify({res: res, content: await res.text()}))
    }
    it works fine at the following pages.dev URL (no custom domain attached, status 200 returned with content): https://cf-pages-fetch-error.pages.dev/basic-fetch-https but not at my pages.dev URL with a custom domain attached (status 525 returned). Any idea why? I can provide the other pages.dev URL privately.
  • h

    HardAtWork

    01/17/2022, 9:01 AM
    waitUntil is a property in context
  • o

    oscartbeaumont

    01/17/2022, 1:17 PM
    Hello, I was wondering if anyone knows how to serve a specific html file from a function handler. My goal is to serve a html file called
    test.html
    from a function called
    [path].ts
    . In trying to make this work I came across the GitHub issue cloudflare/wrangler2#232 which states
    return next("/test.html");
    is not currently working. I am wondering if a workaround exists until this feature is fixed (potentially using
    env.ASSETS.fetch
    )? I made a gist (https://gist.github.com/oscartbeaumont/56e7aaf2a6d510c8a5d48de47e2f2e51) which shows what I am trying to do and my current code. Any help is appreciated.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:19 PM
    Yes, #232 is annoying. Haven't had the time to dig into it yet, but we will fix it up soon.
    Copy code
    ts
    env.ASSETS.fetch(new Request("http://fakehost/test.html", request))
    should work in the meantime
  • o

    oscartbeaumont

    01/17/2022, 1:21 PM
    Thanks for your help but when I do that I receive the error
    Failed to parse URL from /test.html
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:25 PM
    Sorry, was misremembering. Just edited it.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:25 PM
    It's really nasty, sorry. We'll get to it as soon as we can.
  • o

    oscartbeaumont

    01/17/2022, 1:27 PM
    Even that does not work. The function responds with status 301 and the location header set to
    /test
    and just loops until my browser returns ERR_TOO_MANY_REDIRECTS.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:30 PM
    Oh. Could maybe try setting the
    redirect
    property to
    follow
    ? I haven't tried that myself, but that should collapse the redirect inside the Function and just return a normal non-redirecting response.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:31 PM
    Where are you running this Function? What's the filename?
    functions/test.js
    ?
  • o

    oscartbeaumont

    01/17/2022, 1:41 PM
    I am running on my local machine using wrangler2 beta CLI. The ERR_TOO_MANY_REDIRECTS must be because the function was named
    functions/[path].ts
    so it must be looping with itself. I changed the function to be called
    functions/test.js
    temporarily and the function returns a 301 redirect to
    /test
    instead of serving the file directly. If I set
    request.redirect = "follow";
    before using the asset fetch you provided above it makes no difference.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:46 PM
    Hmm, yeah, that doesn't seem to be working for me either.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:46 PM
    Only other thing I could suggest then would be to request the URL of the redirected resource.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 1:47 PM
    Ahead of time, you know it looks like
    /test
    , so you could just:
    Copy code
    ts
    env.ASSETS.fetch(new Request("http://fakehost/test", request))
    If you need it dynamically, you could get the redirection from the
    Location
    header of the response.
  • o

    oscartbeaumont

    01/17/2022, 1:58 PM
    That does prevent the redirect. I also added
    if (url.pathname === "/test") { return await next(); }
    and it now everything works when the function is called
    [path].ts
    . Thanks heaps for your help, I am looking forwards to these issues being fixed!
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 2:04 PM
    Subscribe to #232 and #165 for updates. Thanks for bearing with us!
  • z

    Zelderon

    01/17/2022, 2:37 PM
    I cloned the pages.dev site where the function wasn't working to a new git repo and created a new pages.dev site from that, for now it's working as expected. /shrug
  • e

    e0

    01/17/2022, 5:20 PM
    The docs only mention secrets for local setup using Wrangler. I assume they are not available for Pages atm? What is the best place to follow the changes for Functions beta? Sorry if these are FAQs, tried searching but didn't find anything relevant.
  • g

    Greg Brimble | Cloudflare Pages

    01/17/2022, 6:16 PM
    Correct. No secrets in Pages at the moment. But it's on the list, and we'll have it before we GA.
  • c

    Cоlе

    01/17/2022, 6:35 PM
    ah thanks, that's what I needed. await also seems to work too
  • c

    Cоlе

    01/17/2022, 6:35 PM
    unfortunately, still no
    cache.match()
    😦
  • c

    Cоlе

    01/17/2022, 8:14 PM
    spent more time with the boilerplate, and I figured it out;
    response.headers.append("Cache-Control", "s-maxage=xxx")
    is not optional when
    cache.put(response)
  • h

    HardAtWork

    01/17/2022, 8:28 PM
    Oh, right! Completely forgot about that. Also, if you want to only use the Cloudflare Cache, and not the local cache, you can use the
    Cloudflare-CDN-Cache-Control
    header.
  • c

    Cоlе

    01/17/2022, 8:31 PM
    Cool! That's super useful, thanks for lmk
  • s

    Stew

    01/17/2022, 10:44 PM
    Am I right in saying there's no way to send emails from a worker/function? I'll need to either setup with an email service that works via http, or setup a small server to handle that myself?
1...636465...392Latest