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

    Greg Brimble | Cloudflare Pages

    12/06/2021, 9:12 AM
    https://discord.com/channels/595317990191398933/910978223968518144/912483697540866118
  • r

    robinio

    12/06/2021, 10:09 AM
    Thanks, I have seen the option to request a higher limit. For me this would at least for now make the pricing a bit unpredictable and also I fear that if there is an issue with my functions, this might break my site, so for now, I would hold back an see how things evolve 🙂
  • f

    fragje

    12/06/2021, 2:24 PM
    I can't figure out how to access KV stores with wrangler v2 and pages functions in local environment. I start local dev with:
    Copy code
    npx wrangler pages dev ./build --kv=KV
    And in
    wrangler.toml
    I have added:
    Copy code
    kv_namespaces = [
      { binding = "KV", id = "<id to production kv>", preview_id = "<id to preview kv>" }
    ]
    In
    functions/test.js
    I await the value from KV and return it.
    Copy code
    export async function onRequest({env}) {
      const value = await env.KV.get('click');
      console.log(value);
      return new Response(value);
    }
    Works in production, but not locally. What am I missing?
  • w

    Walshy | Pages

    12/06/2021, 2:28 PM
    It's local so unless you've already set it somewhere, it won't be able to fetch it. You can make KV persist locally though if that would help
  • j

    jacobmarble

    12/06/2021, 2:55 PM
    Is
    --do
    known to work properly in wrangler beta.6?
  • j

    jacobmarble

    12/06/2021, 2:56 PM
    Or, what is a recommended way to test Durable Objects in Pages Functions?
  • r

    robinio

    12/06/2021, 2:58 PM
    Thanks, I figured I will give this a try and see if I can get the limit raised 🙂
  • c

    chrisjmccreadie

    12/06/2021, 3:17 PM
    I wrote this guide hope it helps https://gist.github.com/cryptoskillz/98b8e7090b7cc8d51531bb9dcfe7654a
  • r

    robinio

    12/06/2021, 3:22 PM
    I am working on a SPA. I'd like to replace a few meta tags based on the request of my index.html and figured pages-functions should allow this. I was guessing that it might work in combination with HTMLRewriter. Is something like this possible? Is there maybe a sample that shows how I can access my index.html? Note: I don't want to create index.html from ground up, just manipulating.
  • g

    Greg Brimble | Cloudflare Pages

    12/06/2021, 3:37 PM
    Absolutely possible.
    Copy code
    ts
    // ./functions/my/page.js
    // https://my.domain/my/page
    
    export const onRequest = async ({ next }) => {
      const response = await next()
      
      return new HTMLRewriter().on('head', {
        element(element) {
          element.append('<meta property="og:title" content="A cool new title for OpenGraph" />', { html: true })
        }
      }).transform(response)
    }
    Not tested, but that should do it!
  • r

    robinio

    12/06/2021, 3:42 PM
    Thanks this looks great. Could I run this as a middleware as well? Ideally only executed when returning an actual HTML?
  • g

    Greg Brimble | Cloudflare Pages

    12/06/2021, 3:47 PM
    Sure!
    if (response.headers.get('Content-Type').match(/text\/html/)
  • f

    fragje

    12/06/2021, 3:54 PM
    Thanks, should I get access to production KV data as long as I use the same variable name as setup in Pages > Settings > Functions?
  • r

    robinio

    12/06/2021, 4:00 PM
    Works perfect, thanks a lot!
    Copy code
    export const onRequest = async ({ next }) => {
        const response = await next();
    
        if (response.headers.get('Content-Type').match(/text\/html/)) {
            return new HTMLRewriter()
                .on('head', {
                    element(element) {
                        element.append('<meta property="og:title" content="A cool new title for OpenGraph" />', { html: true });
                    },
                })
                .transform(response);
        } else {
            return response;
        }
    };
  • o

    oliverjam

    12/06/2021, 4:14 PM
    No, Wrangler2 runs your Functions locally, so they cannot access your production KV at the moment
  • j

    jschlesser

    12/06/2021, 4:17 PM
    worked like a charm, thanks!
  • r

    robinio

    12/06/2021, 4:23 PM
    Just an additional question, is it possible to use something like
    event.passThroughOnException()
    to make the site does not break on accident? My current solution is a huge try and catch block, but that's not really ideal IMHO.
  • h

    HardAtWork

    12/06/2021, 4:29 PM
    Haven't verified if this works, but maybe try
    ctx.passThroughOnException()
    ?
  • c

    chrisjmccreadie

    12/06/2021, 4:37 PM
    Yeah
  • c

    chrisjmccreadie

    12/06/2021, 4:39 PM
    But if you use same env it will work when you deploy it to production it just creates a kv locally with the same name. This stops you having to rename things before you push to master
  • h

    HardAtWork

    12/06/2021, 4:52 PM
    Just verified,
    ctx.passThroughOnException()
    is not currently supported in Functions, but that might be a future feature, so stick around.
  • o

    oliverjam

    12/06/2021, 4:53 PM
    Yeah you do want to keep the name the same, but their problem was running
    await env.KV.get('click')
    locally and not seeing any data. You won't see KV data set in prod while you are running locally with Wrangler, even if the namespace is the same.
  • j

    jschlesser

    12/06/2021, 5:15 PM
    Is there a place I should look for more in depth docs or even the code for the functions routing syntax? Im trying to construct a path that looks like /api/thing/[param1]/[param2] and having a filesystem folder named '[param1]' and a file in it called '[param2].js' isn't working as expected. Is the only option for multiple params to use the double bracket syntax and parse the path fragment?
  • o

    oliverjam

    12/06/2021, 5:19 PM
    Yeah unfortunately that doesn't work atm, you'll have to use the
    [[path]]
    syntax. Confirmed above: https://discord.com/channels/595317990191398933/910978223968518144/917171795155636294
  • j

    jschlesser

    12/06/2021, 5:20 PM
    thanks for the speedy reply
  • j

    jacobmarble

    12/06/2021, 8:58 PM
    @User this just helped me out a lot. Thanks!
  • b

    BenQoder

    12/07/2021, 6:09 AM
    Good Morning All. I have installed supabase package on my codebase and I am trying to use it in pages function, It works on local but breaks on deployment. I don’t know if I can get help here or if anyone has experienced this
  • g

    grant

    12/07/2021, 3:08 PM
    Before I waste a ton of time, does anyone know how would I get the a KV namespace (or any environment variable for that matter) from within a sveltekit endpoint?
  • b

    BenQoder

    12/07/2021, 10:30 PM
    I figured it out… I have to specify my node version.
  • e

    Erwin

    12/08/2021, 12:51 AM
    I am not entirely sure I understand what you are trying to do? Could you elaborate?
1...323334...392Latest