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

    Isaac McFadyen | YYZ01

    02/28/2022, 7:47 PM
    There's none; Pages Functions cannot be used when using SvelteKit endpoints with
    @sveltejs/adapter-cloudflare
    or
    @sveltejs/adapter-auto
    . 😄
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 7:47 PM
    Yeah, totally possible. It's a bit more difficult to do without a supporting framework like SvelteKit, Remix, or Flareact (Next.JS not supported currently, those are the main three) but it's possible.
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 7:47 PM
    I run all of my sites on SvelteKit via SSR.
  • b

    balage

    02/28/2022, 7:51 PM
    nice! I wanted to use react, and I tried flareact, but I had some webpack issues with it when I was deploying my app. is Flareact production-ready? I've been kinda interested in svelte actually, so I might try this SvelteKit
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 7:51 PM
    I'm not sure, I've not personally used Flareact but heard it's pretty good. I'd totally recommend SvelteKit though... once you switch to Svelte it's seems like React makes everything overly complex, it's just so simple.
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 7:55 PM
    For example:
    Copy code
    javascript
    export const Counter = () => {
        const [state, setState] = useState(0)
    
        return (
        <div>
            Counter: {state}
          <button onClick={() => setState(state + 1)}>Increment</button>
          <button onClick={() => setState(state - 1)}>Decrement</button>
        </div>
        )
    }
    becomes
    Copy code
    html
    <script>
      let counter;
    </script>
    <div>
      Counter: {counter}
      <button on:click={() => counter += 1}>Increment</button>
      <button on:click={() => counter -= 1}>Decrement</button>
    </div>
  • b

    balage

    02/28/2022, 7:57 PM
    cool, I like that! as I see sveltekit is still in beta. how often do you experience bugs?
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 7:59 PM
    Very infrequently (I've had like 2 maybe the entire time for all of my sites?), and since it's all open-source they were fixed within a day 🙂
  • b

    balage

    02/28/2022, 8:02 PM
    alright, I'll give it a try, thank you for your help!
  • d

    Deleted User

    02/28/2022, 8:42 PM
    so it looks like I've created a recursive problem. I have an
    onRequestGet
    function that uses an
    HTMLRewriter
    to set the inner content of a
    <span class="country">
    element to the country that the visitor is connecting from... but the issue is that the rewriter creates an endless loop calling itself
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:42 PM
    Hmm... how are you getting the static file?
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:42 PM
    Are you calling
    next()
    ?
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:42 PM
    There's a
    next
    method on
    context
    that forwards the request to the static files.
  • d

    Deleted User

    02/28/2022, 8:42 PM
    Copy code
    ts
    export const onRequestGet: PagesFunction = async ({ env, request }) => {
      const { cf, url } = request;
      const { country } = cf;
      const response = await fetch(request);
      return new HTMLRewriter().on('span.country', {
        element(element) { element.setInnerContent(country) }
      }).transform(response);
    };
    This is obviously wrong, but learning here 😛
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:43 PM
    Oh, it's right there.
  • d

    Deleted User

    02/28/2022, 8:43 PM
    where would I use
    next
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:43 PM
    You'll need to do
    await next()
    where
    next
    is an input to onRequestGet, instead of
    await fetch(request)
    .
    next()
    gets the static asset that the function does proxying for.
  • d

    Deleted User

    02/28/2022, 8:44 PM
    oooh
  • d

    Deleted User

    02/28/2022, 8:44 PM
    perfect!!
  • d

    Deleted User

    02/28/2022, 8:44 PM
    that's so cool
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:44 PM
    Yeah, Functions are awesome 🙂
  • d

    Deleted User

    02/28/2022, 8:45 PM
    I was originally going to do some funky text replacement thing, but I stumbled on the HTML rewriter.
  • d

    Deleted User

    02/28/2022, 8:45 PM
    so excited
  • d

    Deleted User

    02/28/2022, 8:46 PM
    I also have wrangler2 running so I can preview this stuff locally
  • d

    Deleted User

    02/28/2022, 8:46 PM
    https://tenor.com/view/amazing-exquisite-vanessa-williams-queen-of-the-universe-perfect-gif-24317032
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 8:47 PM
    Nice!
  • t

    tinylobsta

    02/28/2022, 9:11 PM
    serverless functions are really cool. why haven't they become the dominant way of doing programming? they make so much sense
  • i

    Isaac McFadyen | YYZ01

    02/28/2022, 9:11 PM
    Hmm... well most of the issues with other Cloud providers are the startup times. Cold-start (where a function hasn't been used or needs to scale) can take up to 2 seconds on Python.
  • t

    tinylobsta

    02/28/2022, 9:13 PM
    yeah cold start is an issue, but there's ways to mitigate that by pinging a function, and i think cold start times will continue to decrease. but it seems like functions are an easy and fast way to get started developing an app, it feels like it should be the de facto standard for app dev
  • t

    tinylobsta

    02/28/2022, 9:13 PM
    i always use them for my new projects
1...848586...392Latest