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

    Jeremiahlee

    11/28/2021, 9:27 PM
    Specifically, I'm trying to adapt this to a Pages Function. https://plausible.io/docs/proxy/guides/cloudflare
  • j

    Jeremiahlee

    11/28/2021, 9:29 PM
    But maybe I don't need this listener?
  • j

    Jeremiahlee

    11/28/2021, 9:31 PM
    Just now reading the docs for what passThroughOnException does, I don't think I do need it. It's fine for this function to fail closed.
  • w

    Walshy | Pages

    11/28/2021, 9:32 PM
    the addEventListener('fetch') is for running code when the Worker is called. So yeah, you'd need to change this a little in order to make it work for Functions
  • j

    Jeremiahlee

    11/28/2021, 9:32 PM
    I'll try without. Thanks, @User 🙂
  • w

    Walshy | Pages

    11/28/2021, 9:34 PM
    something like this should work (untested):
    Copy code
    js
    const ScriptName = '/js/script.js';
    const Endpoint = '/api/event';
    
    const ScriptWithoutExtension = ScriptName.replace('.js', '');
    
    export async function onRequest({ request, waitUntil }) {
      const pathname = new URL(request.url).pathname
      const [baseUri, ...extensions] = pathname.split('.')
    
      if (baseUri.endsWith(ScriptWithoutExtension)) {
          return getScript(request, waitUntil, extensions)
      } else if (pathname.endsWith(Endpoint)) {
          return postData(request)
      }
      return new Response(null, { status: 404 })
    }
    
    async function getScript(request, waitUntil, extensions) {
        let response = await caches.default.match(request);
        if (!response) {
            response = await fetch("https://plausible.io/js/plausible." + extensions.join("."));
            waitUntil(caches.default.put(request, response.clone()));
        }
        return response;
    }
    
    async function postData(req) {
        const request = new Request(req);
        request.headers.delete('cookie');
        return await fetch("https://plausible.io/api/event", request);
    }
  • j

    Jeremiahlee

    11/28/2021, 10:44 PM
    What is the proper way to invoke waitUntil in a Pages Function? I'm getting a
    TypeError: Illegal invocation  at Object.waitUntil
    error. It must be called from an
    instanceof ExecutionContext
    . These docs suggest that
    context.waitUntil
    is passed to the Pages Function. https://developers.cloudflare.com/pages/platform/functions#writing-your-first-function My code:
    Copy code
    export async function onRequest(context) {
        let forwardedRequest = await caches.default.match(context.request);
    
        if (!forwardedRequest) {
            forwardedRequest = await fetch("https://plausible.io/js/plausible.outbound-links.js");
            context.waitUntil(caches.default.put(context.request, forwardedRequest.clone()));
        }
    
        return forwardedRequest;
    }
  • j

    Jeremiahlee

    11/28/2021, 10:46 PM
    I should note that I am getting this error when running locally,
    npx wrangler pages dev
  • e

    Erwin

    11/28/2021, 11:40 PM
    Hmm.. that sounds like the
    this
    inside
    context
    isn't bound properly.. but I remember seeing the code that does the binding while investigating something else. This is something the Pages team should have a look at. It is very possible a bug in the
    pages dev
    part of Wrangler
  • e

    Erwin

    11/28/2021, 11:46 PM
    Hmm.. just had a quick look. Pretty sure I found the issue. But I don't have permissions to release a new version of the bundler/runtime. That will have to wait until one of the UK based Pages Team members wakes up 🙂
  • g

    Greg Brimble | Cloudflare Pages

    11/28/2021, 11:54 PM
    Have you got the troublesome line or diff I can take a look at?
  • e

    Erwin

    11/28/2021, 11:55 PM
    Should be a PR up right about now 🙂
  • w

    Walshy | Pages

    11/28/2021, 11:56 PM
    Can I get the credit for this bug?
  • w

    Walshy | Pages

    11/28/2021, 11:57 PM
    I technically caused it, just... didn't find it first hand
  • e

    Erwin

    11/29/2021, 12:00 AM
    How did you cause it??
  • w

    Walshy | Pages

    11/29/2021, 12:00 AM
    I gave the code :p
  • w

    Walshy | Pages

    11/29/2021, 12:01 AM
    Oh wait they didn't use my code
  • w

    Walshy | Pages

    11/29/2021, 12:01 AM
    I wanted a +1 for my resume under "breaking Cloudflare"
  • j

    Josh

    11/29/2021, 12:03 AM
    I'll make sure this is brought up in the process
  • e

    Erwin

    11/29/2021, 1:01 AM
    Funnily enough Cloudflare would never considered you to have caused that even if they did use your code..
  • e

    Erwin

    11/29/2021, 1:01 AM
    The person who accepted your code and the person who reviewed your code are at least equally involved..
  • w

    William | Chaos Management

    11/29/2021, 1:03 AM
    lol
  • r

    rkusa

    11/29/2021, 8:17 AM
    I am getting the same error for
    waitUntil
    when deploying, not just locally via
    pages dev
    (see https://discord.com/channels/595317990191398933/910978223968518144/914538090826182767). Just in case the upcoming fix only applies to
    pages dev
    .
  • d

    Deleted User

    11/29/2021, 9:21 AM
    are pages functions subject to the cap of 100 workers per account?
  • d

    Deleted User

    11/29/2021, 9:21 AM
    also if they're going to be sharing a bunch of code, are there any best practices for that?
  • e

    Erwin

    11/29/2021, 10:28 AM
    No, it will fix it for both.. And it shouldn't take too long..
  • a

    Adriaan

    11/29/2021, 12:23 PM
    Does Wrangler dev remove "set-cookie" headers? I am setting it in functions middleware with
    Copy code
    response.headers.append("Set-Cookie", "test=true")
    and it works on the pages platform, just not in Wrangler dev. Other headers are coming through in Wrangler dev.
  • g

    Greg Brimble | Cloudflare Pages

    11/29/2021, 12:47 PM
    Not intentionally. If you could quickly write up an issue on the wrangler2 repo (https://github.com/cloudflare/wrangler2/issues/new), that would be fab. Thanks!
  • k

    Kev

    11/29/2021, 1:01 PM
    What's the correct way to reference environment variables in a pages function?
  • k

    Kev

    11/29/2021, 1:01 PM
    process.env["SOME_API_KEY"]
    does not seem to do the trick
1...232425...392Latest