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

    Isaac McFadyen | YYZ01

    02/23/2022, 7:52 PM
    And then use the Endpoints syntax I sent above.
  • m

    mibaatwork

    02/23/2022, 7:53 PM
    Yes and from there I can call dan e.g. a worker! but I thought with /function that would be more elegant
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:54 PM
    No, you cannot do that, because SvelteKit with Pages doesn't support /functions since it already uses it behind-the-scenes.
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 7:54 PM
    Endpoints can serve the same purpose though, everything that can be done in a Worker can also be done in an Endpoint.
  • m

    mibaatwork

    02/23/2022, 7:56 PM
    Top, thanks for the quick reply
  • v

    vivekpatt_whyd

    02/23/2022, 8:08 PM
    @User Thank you for replying. Basically I want to generate dynamic sitemap so I am trying to implement functions in pages(angular framework)
    website/sitemap.xml
    (probably this one) or
    website/en/sitemap.xml
    So how should i structure the functions folder ? Since you said it shouldn't be in dist, do you mean that i should be at project's root and I just have to git push it and pages will take care of the functions ?
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 8:09 PM
    Yes, that's exactly it (project root). And every Function has to end with
    .js
    , so with your sitemap example it would be
    sitemap.xml.js
    and then Pages will strip the JS off and allow you to access it at
    sitemap.xml
    .
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 8:10 PM
    As for structuring the functions folder: each folder is a path. For example:
    /functions/test/sitemap.xml.js
    would be at
    example.com/test/sitemap.xml
    ,
    /functions/test2/hi.js
    would be at
    example.com/test2/hi
    , etc.
  • v

    vivekpatt_whyd

    02/23/2022, 8:13 PM
    Thank you @User All this while I misunderstood it and I was trying to fit in the dist folder via build or something. It is very clear to me now, Again, Thanks.
  • i

    Isaac McFadyen | YYZ01

    02/23/2022, 8:13 PM
    No problem.
  • l

    Larry

    02/24/2022, 4:14 AM
    I came to the same spot you are in about a week and a half ago. I considered 3 alternatives: 1. Stick with SvelteKit and give up on using Cloudflare's /functions mechanism. I went down this path for a while but once I started trying to test locally with durable objects, I got stuck. I've learned a lot since then, and I probably could get over that hump now, but I'm still glad I went with alternative 3 below mostly because it's one less abstraction layer I have to worry about. 2. Use SvelteKit's
    adapter-static
    for the UI and Cloudflare's /functions for my API. I didn't try this but thought about it. 3. Drop back to plain old Svelte (not SvelteKit) for just the UI and use Cloudflare for everything else. I gave up a few things going this way: a) SveltKit's filesystem router but I am using svelte-spa-router on another project that started pre-SvelteKit so I'm already comfortable with that approach. b) SSR. I consider SSR an unnecessary complexity for what I'm doing. I'm not building a content app where search engine optimization is critical and the data round trip time is a bigger determinant of the user experience than the UI render time for my context. I think you give this up with alternative 2 also. c) SvelteKit's elegant hierarchical _layout approach. In that other project I had already built a poor man's equivalent to _layout using the
    <svelte:component this={$activeComponent} />
    tag so I'm not losing much. d) The ability to deploy to someplace other than Cloudflare. Alternative 2 also gives this up. I'm building with durable objects so I need Cloudflare no matter what so this was no loss for me, but could be for you. Of course, you may have a different context and decide to go a different route but wanted to share what I learned in the hopes it could help you.
  • v

    vivekpatt_whyd

    02/24/2022, 11:31 AM
    Does anyone know about a good package which can be used for generating dynamic sitemap?
  • m

    mibaatwork

    02/24/2022, 5:40 PM
    Speaking of experience: I am an Enterprise Architect and integrating routes into a CDN are not particularly difficult. We use Path Based Services in several projects within a domain. (also with Svelte!) e.g. As a feature extension for /functions it would be possible to use the BasePath in the configuration to use the rewrite rules of the CDN to resolve the routes of the functions before the execution of the Svelte app. Example config: { "routes": { "POST /login": { "module": [ "/app/auth/login.js:onRequestPost" ] }, "/userdata": { "module": [ "/app/auth/usadata.ts:onRequest" ] } }, "baseURL":"/app/auth" } so I call mypage.com/app/auth/login as a post, I end up in the worker/function if there is no match in the routes the svelte app is called and the svelte router can resolve all other routes. mypage.com/ or mypage.com/about ... In my opinion this could be solved today with url rewrites (https://developers.cloudflare.com/rules/transform/url-rewrite/examples). But for this I have to create another pages page, which contains only the functions. It would be nicer if the automatic deployment from a project would be possible.
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 5:44 PM
    The main issue around this is SvelteKit (or are you using vanilla Svelte?) uses a Worker internally so /functions can't currently be used.
  • m

    mibaatwork

    02/24/2022, 6:08 PM
    The solution has nothing to do with SvelteKit/Svelte for now! The Cloudflare deployment runs sveltekit build (+ @sveltejs/adapter-cloudflare) and the artifact ends up under .sveltekit/cloudflare/_worker.js Currently it seems that if a directory /function is in the root of the project, only that is used and the application will be ignored . I think that the combination JS App + /function does not work anywhere. ( failure by design? ) Or does anyone use a framework preset (React, Nuxt, Next, hudo... ) where this works? I have read some issues that other developers have the same problem (not only with Svelte).
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 6:58 PM
    Yes, it's an infrastructure contraint. When a _worker.js is specified it overrides the /functions, this is because it's actually bundling everything into 1 worker in the background.
  • m

    mibaatwork

    02/24/2022, 7:01 PM
    not with me! is a /functions folder in the project the _worker.js does not work anymore
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 7:01 PM
    I know, that's by design.
  • m

    mibaatwork

    02/24/2022, 7:03 PM
    can this be modified? with the methods I have described above?
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 7:04 PM
    Right now this is not possible to modify, no. If you are using SvelteKit you'll need to use Endpoints.
  • m

    mibaatwork

    02/24/2022, 7:05 PM
    and how does that work with next.js or nuxt.js? does that not work there either?
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 7:06 PM
    Next.JS is not supported, no, since it uses Node.JS utilities not available in Functions/Workers.
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 7:06 PM
    You would have to build statically, which means no SSR.
  • m

    mibaatwork

    02/24/2022, 7:14 PM
    Well, yes! is not that bad . In my microservice architecture I have more than one functionality/api anyhow. I love how straightforward Cloudflar is! The Cloudflare colleagues do really good job👍
  • s

    Skye

    02/24/2022, 7:19 PM
    Nuxt 3 can work on cloudflare workers however!
  • l

    Lostballoon

    02/24/2022, 9:50 PM
    In workers, it would be in the TOML file, but where can we configure this for pages functions:
    Copy code
    The package "path" wasn't found on the file system but is built into node. Are you trying to
      bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
  • i

    Isaac McFadyen | YYZ01

    02/24/2022, 9:53 PM
    Pages Functions don't support bundling unfortunately.
  • l

    Lostballoon

    02/24/2022, 9:56 PM
    hmm, i see, thanks
  • l

    Lostballoon

    02/25/2022, 10:00 PM
    so I managed to bypass that restriction by using vanilla js and an API instead of their library.
  • l

    Lostballoon

    02/25/2022, 10:01 PM
    though, my next question is how can I setup my local environment simulate the use of environment variables in my function?
1...818283...392Latest