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

    João Castro

    02/23/2023, 2:43 PM
    I'm putting the _middleware file at the root of the functions folder, and it runs for every route
  • j

    João Castro

    02/23/2023, 3:48 PM
    The problem is, i only want to rewrite some HTML, and its consuming extra function invocations for nothing when it doesn't hit a page route.
  • j

    James

    02/23/2023, 3:52 PM
    You could use
    _routes
    to exclude an
    assets
    dir or similar from running Functions: https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes
  • j

    João Castro

    02/23/2023, 3:55 PM
    Nice! That seems to be exactly what i was looking for. Thanks!
  • n

    Noodles

    02/23/2023, 4:28 PM
    Just wanted to follow up about this, still can’t seem to get it to work.
  • j

    James

    02/23/2023, 4:48 PM
    How are you publishing this?
    wrangler pages publish .
    ?
  • j

    James

    02/23/2023, 4:49 PM
    Oh your Function's format isn't correct either
  • j

    James

    02/23/2023, 4:50 PM
    Functions use Workers under the hood, but they don't run Workers via
    addEventListener
    and such. You need to use specific Functions syntax like
    onRequestGet
    , etc: https://developers.cloudflare.com/pages/platform/functions/get-started/
  • n

    Noodles

    02/23/2023, 4:51 PM
    Through GitHub, I will try to use wrangler though and adjust the syntax thanks!
  • j

    jgarr

    02/23/2023, 5:40 PM
    Is there a way to get the source URL in a function? I want to redirect the person back to the page they came from but haven't found a source url in the context
  • h

    HardAtWork

    02/23/2023, 5:41 PM
    How did they originally get to your Function? Via a redirect?
  • j

    jgarr

    02/23/2023, 5:41 PM
    form submit click (POST request)
  • j

    jgarr

    02/23/2023, 5:46 PM
    I'm sending my email sign up form to a function that pulls out the email and stores it in KV here https://justingarrison.com/ it just redirects back to the home page with
    ?email=submit
    to disable the subscribe button. But I also have a signup form at the bottom of the posts eg https://justingarrison.com/blog/2023-02-23-ec2-names-explained/ and I'd like to send them back to the post they were on. My only thought was to create a hidden field part of the form that contains the path and use that in the function
  • j

    jgarr

    02/23/2023, 5:46 PM
    I wasn't sure if that data already existed in the context
  • h

    HardAtWork

    02/23/2023, 5:49 PM
    ```js req.headers.get("referer") ```maybe?
  • j

    jgarr

    02/23/2023, 5:50 PM
    Thanks, I'll give it a shot
  • z

    zszszsz

    02/23/2023, 9:34 PM
    Just a quick question for dynamic routing, I want to match sth like
    /
    ,
    /foo/bar/.../
    , or
    /foo/bar/baz
    , or
    /**/*.html
    or
    /**/index.html
    , but not
    /**/*.js
    or
    /**/*.css
    , (or .jpg, .svg, .txt, .webmanifest, .json or so), is it doable currently with dynamic routing ?
  • h

    hanpolo

    02/23/2023, 11:23 PM
    Anyone have an idea how to insert values into a local KV for local development with pages functions? Starting command line is
    npx wrangler pages dev --kv=USER -- npm start
  • h

    hanpolo

    02/23/2023, 11:24 PM
    I'm trying to seed something in the USER namespace
  • h

    hanpolo

    02/23/2023, 11:24 PM
    before my code is called
  • z

    zszszsz

    02/23/2023, 11:54 PM
    do it yourself
  • z

    zszszsz

    02/23/2023, 11:55 PM
    just create a file at
    .wranlger/state/kv/<bind-name>/<key>
    with the value as content
  • h

    hanpolo

    02/24/2023, 12:32 AM
    Great suggestion .. but it does not work. Here's what I did: 1. ran my test instance
    npx wrangler pages dev --kv=TEST -- npm start
    2. created a file:
    ~/.wrangler/state/kv/TEST/BUG_TEST
    3. put the value 'TEST1' inside BUG_TEST 4. Read the value from file:
    Copy code
    const test = await context.env.TEST.get('BUG_TEST');
      console.log(`test=${test}`);
    5. Got the value 'null' 6. To confirm the namespace works I did another test right after:
    Copy code
    await context.env.TEST.put('BUG_TEST','THIS IS A TEST');
      const after = await context.env.TEST.get('BUG_TEST');
      console.log(`after=${after}`);
    7. This one yielded "THIS IS A TEST"
  • z

    zszszsz

    02/24/2023, 12:34 AM
    why
    ~/.wrangler
    , it should be
    ./.wrangler
  • z

    zszszsz

    02/24/2023, 12:36 AM
    Since you did
    await context.env.TEST.put('BUG_TEST','THIS IS A TEST')
    , you should have already have that file right
  • h

    hanpolo

    02/24/2023, 12:42 AM
    According to the docs, it puts it in memory
  • h

    hanpolo

    02/24/2023, 12:42 AM
    Also, I realized that wrangler is installed in my node_modules, since I'm invoking it with npx
  • h

    hanpolo

    02/24/2023, 12:43 AM
    tried a file there with same results
  • z

    zszszsz

    02/24/2023, 12:45 AM
    No it should be just at its working dir, or let's say "project root" if it is its working dir.
  • z

    zszszsz

    02/24/2023, 12:53 AM
    Ahh ok you need
    --persist
1...350351352...392Latest