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

    Mark Hood

    11/20/2021, 10:19 PM
    Thank you!
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:19 PM
    Cool. You want that available on the homepage or on
    /weather
    ?
  • w

    wonkrattle

    11/20/2021, 10:19 PM
    I want that available on the homepage. I’ve been able to make it exist on/weather
  • w

    wonkrattle

    11/20/2021, 10:20 PM
    But I want it alongside all the other content that I have there
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:20 PM
    Alrighty, so you'll need a
    functions/index.ts
    or
    functions/index.js
    file, and yeah, we can use HTMLRewriter for it.
  • w

    wonkrattle

    11/20/2021, 10:21 PM
    Sounds good, I’ll follow along
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:21 PM
    In your static assets, I'd create a placeholder of where you want this content to appear.
    Copy code
    html
            <p>
                I also enjoy constructing 
                <a href="/crosswords">crossword puzzles</a>
                .
            </p>
            <p>
                I use 
                <a href="https://www.mynetdiary.com">MyNetDiary</a>
                 to track 
                <a href="/diet.html" style="color:#49D6EA !important;">what I eat</a>
                .
            </p>
            <p id="weather">Could not load the weather</p>
        </article>
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:21 PM
    And we'll target #weather with HTMLRewriter.
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:26 PM
    Copy code
    js
    // ./functions/index.js
    
    export const onRequestGet = ({ request, next }) => {
      // Get the static asset response
      const response = await next()
      
      const { latitude, longitude } = request.cf
    
      // Get weather
      const weatherResponse = await fetch(`https://myweather.service/${latitude},${longitude}`)
      const weatherData = await weatherResponse.json()
      const { isRaining } = weatherData
    
      // Find the placeholder in our static asset
      return new HTMLRewriter().on('#weather', {
        // And act on the element
        element(element) {
          // https://developers.cloudflare.com/workers/runtime-apis/html-rewriter#methods
          element.setInnerContent(isRaining ? `It is raining 🌧` : 'It is not raining ☀️')
        }
      }).transform(response) 
    }
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:27 PM
    Untested, and just written by hand in Discord, but that should pretty much be it.
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:27 PM
    More about the selectors available here: https://developers.cloudflare.com/workers/runtime-apis/html-rewriter#selectors
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:28 PM
    Could also do it with async handlers: https://developers.cloudflare.com/workers/runtime-apis/html-rewriter#async-handlers
  • w

    wonkrattle

    11/20/2021, 10:28 PM
    Now, would that index.JS file operate on any page that had the weather tag
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:28 PM
    No, just the homepage
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:29 PM
    If it was
    functions/[path].js
    it would act on all URLs like this: - / - /thing - /weather If it was
    functions/[[path]].js
    it would act on all URLs, including these: - / - /thing - /todos/thing
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:30 PM
    https://developers.cloudflare.com/pages/platform/functions#path-segments
  • w

    wonkrattle

    11/20/2021, 10:30 PM
    Yeah I saw that I just didn’t understand how it applied to URLs with extensions
  • w

    wonkrattle

    11/20/2021, 10:30 PM
    Like index.html vs /index
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:31 PM
    I'm not 100%, but I imagine by "path segment", we're referring to things between `/`'s.
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:31 PM
    Certainly that's what it is for _headers and _redirects.
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:31 PM
    /:product/:id
    sort of thing
  • w

    wonkrattle

    11/20/2021, 10:32 PM
    Ahhh
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:32 PM
    i.e. extensions are included.
    index.html = index
  • w

    William | Chaos Management

    11/20/2021, 10:32 PM
    It seems like something went wrong when running
    wrangler pages dev
    because I imported another file as an ES Module
    wranglerlogmyfunction
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:33 PM
    Known bug on Windows at the moment, I'm afraid.,
  • w

    William | Chaos Management

    11/20/2021, 10:33 PM
    when I deploy this it should work fine, right?
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:33 PM
    https://github.com/cloudflare/wrangler2/issues/32
  • g

    Greg Brimble | Cloudflare Pages

    11/20/2021, 10:33 PM
    Looks good to me, yeah!
  • w

    William | Chaos Management

    11/20/2021, 10:33 PM
    cool
  • w

    wonkrattle

    11/20/2021, 10:33 PM
    I’m definitely looking forward to Wrangler being working locally because for now I have to preview each change and that takes about 10 minutes per 😂
1...8910...392Latest