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

    William.

    03/11/2023, 7:56 PM
    Copy code
    js
    // Cloudflare middleware to append meta tags to HTML responses based on path
    export async function onRequestGet(context) {
        // Used to break the loop
        if (context.request.url.indexOf('metatags') > -1) {
            return await context.next();
        }
        // We're not altering any assets, so we can skip this middleware
        if (context.request.url.match(/\.[a-z]/gm)) {
            return await context.next();
        }
    
        // Meta tags to append to the HTML response
        const metaTags = {
            // Remove for brevity
        };
        let metas;
        // Get the path from the request URL
        const path = context.request.url;
        if (path.indexOf('/assessment') > -1) {
            metas = metaTags.assessment;
        } else {
            metas = metaTags.global;
        }
    
    
    
        const data = await fetch(context.request.url + '/metatags').then(function (response) {
            return response.text();
        })
        // Append meta tags to the HTML response
        let alteredHTML = data.replace('<head>', `<head>
            <!--{{WORKER_META_TAGS}}-->
            <title>${metas.title}</title>
            <meta name="description" content="${metas.description}">
            <meta name="image" content="${metas.image}">
            <meta name="robots" content="${metas.noIndex ? 'noindex' : 'index'}">
            <!--{{/WORKER_META_TAGS}}-->
            `);
    
        return new Response(alteredHTML, {
            headers: {
                'content-type': 'text/html;charset=UTF-8',
            },
        }, { status: 200 });
    }
  • w

    William.

    03/11/2023, 7:56 PM
    This is the result i get in Wrangler
  • w

    William.

    03/11/2023, 7:58 PM
    But nothing happens in pages. Even tho I include the
    functions
    folder at the root of my angular project AND in the dist after build (tried with only root, tried with only in dist, tried with both)
  • n

    nightshade427

    03/12/2023, 7:09 PM
    Is there a way to specify a queue consumer in a pages app that uses functions? I want to reuse my pages code in the queue consumer. If not is something like that played for future?
  • j

    James

    03/12/2023, 7:15 PM
    Unfortunately not right now. I'm sure it'll come in future, but you'll need to use Workers for a queue consumer today
  • n

    nightshade427

    03/12/2023, 7:48 PM
    Is there a way to reuse the code easily between the queue consumer worker and pages site?
  • j

    James

    03/12/2023, 7:48 PM
    You could store them in a monorepo or something and import code between them
  • j

    James

    03/12/2023, 7:49 PM
    Or even just a
    src/worker
    folder, with a
    wrangler.toml
    and then run different commands for publishing to Pages/Workers
  • j

    James

    03/12/2023, 7:50 PM
    It's not the most ergonomic, but that's the best way (outside of something like terraform) imo to handle that today
  • n

    nightshade427

    03/12/2023, 7:50 PM
    Gotcha, ya was thinking the same, maybe use GitHub actions to deploy vs pages auto GitHub stuff
  • n

    nightshade427

    03/12/2023, 7:50 PM
    Thanks for help, much appreciated
  • j

    James

    03/12/2023, 7:51 PM
    Happy to help 🙂
  • p

    Petter

    03/13/2023, 11:27 AM
    Are service bindings supported in page functions yet?
  • s

    Skye

    03/13/2023, 11:37 AM
    Yes
  • s

    Skye

    03/13/2023, 11:37 AM
    Though not for
    wrangler pages dev
    just yet
  • c

    c becker

    03/15/2023, 4:49 AM
    Milestone Celebration: lazy initialization of identity in D1 tables, based on the JWT assertions form Access. Now working for my app! This stuff used to take me weeks on new projects. only a couple days for this one https://github.com/readysetawesome/timely-tasker/blob/lazy-init-identities/functions/greet.ts
  • g

    Greg Brimble | Cloudflare Pages

    03/15/2023, 9:36 AM
    Woohoo! Glad the Access Plugin works for you! 🥳
  • l

    Larry

    03/15/2023, 2:23 PM
    Possibly an off-topic question... The Access web page makes me think of it like Okta and other SSO solutions, meant for internal enterprise identity. However, would it also work well for me as the author of a multi-tenant application? I want each company to have its own set of identities in my system rather than one big lump. Maybe there is a clean way to partition the identities in Access? Maybe I can spin up a separate Access for each company client of mine?
  • g

    Greg Brimble | Cloudflare Pages

    03/15/2023, 2:26 PM
    Access is definitely geared towards protecting internal resources. Like you say, enterprise use-cases. But, you could probably get something set up if you have only a few clients that you’re willing to do a manual step or two for. I don’t think you can literally separate the list of users today, but you could provision independent applications which accept groups of users.
  • c

    c becker

    03/15/2023, 3:12 PM
    My understanding is If you only have one application, then all logins will come through the same access instance. But i think it’s possible to map identities to different tenants within your application based on something in the JWT body. Email domain or hopefully something stronger, IDK, really depends on how users become registered and affiliated with a particular tenant. Maybe a single use code to “claim” a spot in an organization for a particular identity. Not an easy problem for sure
  • u

    1ockwood

    03/16/2023, 5:37 PM
    How should environment variables be set for testing a Pages function locally? I feel like I'm probably missing something obvious, but have searched the docs and experimented with code and am not able to figure it out.
  • s

    Skye

    03/16/2023, 5:42 PM
    If you mean secrets, a
    .dev.vars
    file (same as a .env file), if you just mean regular env vars, you use
    --binding VAR_NAME=VAR_VALUE
    in your
    pages dev
    command 🙂
  • u

    1ockwood

    03/16/2023, 5:46 PM
    A-ha! The
    .dev.vars
    file is what I was looking for and worked perfectly. Thank you @Skye!
  • s

    sdev

    03/18/2023, 2:06 AM
    Can WebAuthn be implemented in the Functions runtime?
  • h

    HardAtWork

    03/18/2023, 2:27 AM
    There is @simplewebauthn/server, though it may be a little while, since the ESM update is still being worked on
  • m

    My Account

    03/18/2023, 7:12 AM
    is there any way I can make a particular function accessible from my site only?
  • m

    My Account

    03/18/2023, 7:13 AM
    origin header = NO as it can be spoofed
  • m

    My Account

    03/18/2023, 7:13 AM
    also it's static site so if u make post request with token then it's revealed
  • m

    My Account

    03/18/2023, 7:14 AM
    I would be highly grateful if anyone have something for this
  • m

    My Account

    03/18/2023, 7:14 AM
    I am struggling from long time now
1...357358359...392Latest