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

    Kieran Huggins

    02/21/2022, 2:11 AM
    I think the lightbulb moment for me was realizing that functions are not run in the context of a filesystem. Not sure why I expected otherwise... maybe because I'm used to having a web server of some sort? Is there documentation I missed about
    env.ASSETS
    ? This is what I ended up with btw
    Copy code
    export async function onRequest({env, request: {cf: {country, regionCode}, url}}) {
      let filename = `${country}_${regionCode}.json`.toLowerCase();
      return await env.ASSETS.fetch(`${url}/${filename}`);
    }
  • l

    Larry

    02/21/2022, 3:08 AM
    @User , I created pull request #506 against cloudflare/wrangler2 to add support for mounts which solved my problem and now allows me to rapidly iterate locally on durable object development. I'm going to be really embarrassed when someone tells me there was an easier way to have done what I was trying to do but I asked, waited a week, asked again and I had to do something so... .
  • k

    Kieran Huggins

    02/21/2022, 10:43 AM
    correction - the above did not work. Reverted to network fetch for now and will watch the GH issue for updates. Thanks all!
  • g

    Greg Brimble | Cloudflare Pages

    02/21/2022, 2:57 PM
    Annoyingly, for now you'll need:
    Copy code
    ts
    export async function onRequest({env, request, url}) {
      const {cf: {country, regionCode} = request
      let filename = `${country}_${regionCode}.json`.toLowerCase();
      return await env.ASSETS.fetch(`${url}/${filename}`, request);
    }
  • g

    Greg Brimble | Cloudflare Pages

    02/21/2022, 2:58 PM
    There's some header or something in the incoming request that's needed. We'll fix this for GA.
  • r

    RaifY

    02/21/2022, 2:59 PM
    How to change pages function build options?
  • r

    RaifY

    02/21/2022, 3:00 PM
    I need to implement something like this in pages function
  • z

    zsmooth

    02/22/2022, 4:15 AM
    You can build your own worker file for Pages instead of relying on wrangler to do it for you. See the Advanced section of the Pages with Functions docs. Basically you’ll output a file called
    _worker.js
    to your output dir instead of having `functions`: https://developers.cloudflare.com/pages/platform/functions#advanced-mode
  • b

    Broonix

    02/22/2022, 8:57 PM
    Is HTML rewriting possible when using Advanced workers /w Pages? When passing the request into the
    HTMLRewriter
    I'm getting errors that the content is not streamable. I'm loading the content via
    env.ASSETS.fetch(request)
  • h

    HardAtWork

    02/22/2022, 9:09 PM
    So are you doing something like this?
    Copy code
    js
    return new HTMLRewriter().on("div", new ElementHandler()).transform(await env.ASSETS.fetch(req));
  • b

    Broonix

    02/22/2022, 9:17 PM
    Correct.
  • h

    HardAtWork

    02/22/2022, 9:20 PM
    What does it look like if you log
    await env.ASSETS.fetch(req)
    ?
  • w

    Walshy | Pages

    02/22/2022, 9:22 PM
    https://pages-rewriter.pages.dev/
  • w

    Walshy | Pages

    02/22/2022, 9:22 PM
    I'd definitely suggest wrangler2 for testing locally too
  • b

    Broonix

    02/22/2022, 9:23 PM
    it's a string, contains the HTML content of the page.
  • b

    Broonix

    02/22/2022, 9:25 PM
    I'll throw together a demo, I ended up solving this with normal worker outside of my Pages project. That has sadly caused other issues.
  • e

    Erwin

    02/23/2022, 1:10 AM
    Ohh.. it could be that Pages is reading the entire content of the file before returning it. I am probably going to have to take a look at the Pages Worker anyway for something unrelated, so I will take a look at it then if no one has beaten me to it @User
  • d

    Deleted User

    02/23/2022, 4:43 PM
    @User That country/region code function seems like it will be a very popular use case — I came to ask about it as well but your example is explanatory. I just have a couple questions; where is the
    context
    argument documented and is
    env.ASSETS
    inherent to all pages projects? I assume this is baked-in equivalent of using the kv asset handler in Workers Sites.
  • d

    Deleted User

    02/23/2022, 4:50 PM
    nevermind, found https://developers.cloudflare.com/workers/runtime-apis/request#properties
  • g

    Greg Brimble | Cloudflare Pages

    02/23/2022, 5:22 PM
    https://github.com/cloudflare/workers-types/blob/master/index.d.ts#L1699-L1706 https://developers.cloudflare.com/pages/platform/functions#writing-your-first-function And yes,
    env.ASSETS.fetch()
    is available to all Pages projects using Functions 🙂
  • d

    Deleted User

    02/23/2022, 5:23 PM
    thanks
  • v

    vivekpatt_whyd

    02/23/2022, 7:45 PM
    Hello everyone, I need help with setting functions in angular framework. Basically I want to know how to set it up? My angular project runs on pages and it is a i18n build, which mean the out put folder looks like this ./dist/en/index.html ./dist/nl/index.html ./dist/fr/index.html The function folder should be ./dist/functions/hello.js or ./dist/en/functions/hello.js any guidance or pointers are appreciated 🙏 Thank you
  • m

    mibaatwork

    02/23/2022, 7:46 PM
    Hi guys. I've been looking for a solution to a /functions problem for days. I use Sveltekit in Pages. I deployed the app and it runs top and the svelte-router works (e.g. mypage.pages.dev/ or mypage.pages.dev/about ...)! Now I wanted to get data into the Svelte app via /functions. so I created the folder /functions in root as in the documentation. When I deploy, Cloudflare also gives me /functions/data/hello-world.js => mypage.pages.dev/data/hello-world my content, but now the Svelte app is not running anymore! The Functions Config: { "routes": { "POST /data/hello-world": { "module": [ "data/hello-world.js:onRequestPost" ] }, "/data/info": { "module": [ "data/info.ts:onRequest" ] } }, "baseURL":"/" } Is this because of the baseURL and can I change it? or do /functions not run with a frontend application? Translated with www.DeepL.com/Translator (free version)
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:46 PM
    The functions folder shouldn't have a hello.js at all based on what you've said. The filename matters, so your functions folder should be:
    /functions/en/index.js
    for English,
    /functions/fr/index.js
    for French, etc.
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:47 PM
    Also, it shouldn't be in
    dist
    , it needs to be in the root of your project.
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:47 PM
    Yeah, SvelteKit doesn't support Functions because the whole project is compiled into one Function, assuming you are using
    adapter-auto
    or
    adapter-cloudflare
    .
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:47 PM
    Instead, use endpoints: https://kit.svelte.dev/docs/routing#endpoints
  • m

    mibaatwork

    02/23/2022, 7:48 PM
    ok, thx. i try this!!
  • m

    mibaatwork

    02/23/2022, 7:51 PM
    But couldn't this be solved on CDN level? that with existing baseurl like for example /api/ befor the svelte app is resolved?
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:52 PM
    Yes, but not with Functions. You will have to use Endpoints for that, like having a file in
    /src/routes/api/index.js
1...808182...392Latest