Zach’s ugly mug (his face) Zach Leatherman

I added Lighthouse Scores to my Site’s Footer and You Can Too

July 12, 2020 #14 Popular

Speedlify started with a very simple goal: make it easy to continuously check a site’s performance. As my friend Scott Jehl has said, “it can be easier to make a site fast than to keep it that way.” And as a performance advocate, I feel that it’s important to keep myself honest and to have some transparency behind the sites I build.

In that vein, I decided to experiment with publishing Lighthouse scores of some of the pages on my personal web site onto the actual pages themselves. This does have a small performance cost associated with it, but eventually I hope to optimize it away when my personal site has a bit better Jamstack architecture behind it.

Fetching the Lighthouse Score

If you don’t care how this works and just want to set it up—skip to the next section!

Obviously the costliest and most resource intensive piece of adding this feature to my site would be running the Lighthouse tests. I certainly wouldn’t want to run these on every page visit. Luckily Speedlify handles this for me—it’s already testing a bunch of pages on my site and it’s set up to only run once a day. All I needed to do was to add a static API, which is just a fancy way of saying that I used Eleventy to generate a bunch of static JSON files in an /api/ directory.

There are two API pieces here:

Speedlify tests once a day. I do not deploy my personal site every day (don’t tell my employer but my personal site is not yet on Netlify 😭—I wish it were but haven’t migrated it yet February 2022 update: zachleat.com is now on Netlify). This is a problem (for multiple reasons), right? If I consume the API data when I deploy my personal web site the results would be stale when Speedlify runs the next day! To workaround this temporary limitation I decided to use the Fetch API to grab the data client-side using JavaScript.

This is marginally okay as the widget is hidden at the very bottom of the page in the footer. But when I migrate my personal site to Netlify, this won’t be necessary. Ideally I would add a webhook in Speedlify to re-build my personal site every time the results change and delete that client-side JavaScript altogether. Yet another shining example of how a Jamstack architecture can improve front-end performance by making a chunk of clientside JavaScript no longer necessary.

Adding this to your Eleventy site!

I’ve hopefully streamlined the setup here enough that you can use this up for your own site too (if you’re already using Eleventy). Three steps here:

  1. Run your own Speedlify instance. You can learn more at the GitHub repository or Deploy to Netlify
    • Modify the files in Speedlify’s _data/sites/*.js folder with your own URLs!
  2. Consume the Speedlify API data files—they are generated automatically with your Speedlify instance.
    • Use Eleventy’s Cache Assets plugin
      npm install @11ty/eleventy-cache-assets --save-dev
    • Copy and paste the small speedlify.js Eleventy global data file to your global data directory. This tells us which URLs are speed tested in Speedlify. Change the URL in speedlify.js to point to your /api/urls.json file. Mine is https://www.speedlify.dev/api/urls.json.
  3. Use the <speedlify-score> Web Component. (These instructions were updated July 17, 2020)
    • npm install speedlify-score
    • Add the speedlify-score.js JavaScript and the speedlify-score.css CSS to your page.
    • Add the <speedlify-score> markup to your page where you want the score to show up. I’d recommend putting it in the footer of your page.
    • Use the speedlify.js Eleventy global data file we added earlier to set the hash attribute. Have a look at my footer.html to see how I did it using Liquid templates (only lines 16 through 19).
    • Also set the speedlify-url attribute to point to your Speedlify instance. Mine is
      speedlify-url="https://www.speedlify.dev"

Go forth and speed test!

Hope you were able to stumble through those instructions okay—I would love to see examples of this in the wild if you do get it working. Please send them my way!


< Newer
The Lighthouse Scores Will Continue Until Morale Improves
Older >
Use Speedlify to Continuously Measure Site Performance

Zach Leatherman IndieWeb Avatar for https://zachleat.com/is a builder for the web at IndieWeb Avatar for https://fontawesome.com/Font Awesome and the creator/maintainer of IndieWeb Avatar for https://www.11ty.devEleventy (11ty), an award-winning open source site generator. At one point he became entirely too fixated on web fonts. He has given 84 talks in nine different countries at events like Beyond Tellerrand, Smashing Conference, Jamstack Conf, CSSConf, and The White House. Formerly part of CloudCannon, Netlify, Filament Group, NEJS CONF, and NebraskaJS. Learn more about Zach »

40 Reposts

Matthias OttAndy BellHazim Sami / حازم ساميWolfram ⪡JS-- HTML++ CSS++⪢ KriesingJosua ZurbrüggMatt HobbsEleventyDani de la CruzJess but 6 feet awayNanoYassAndre 🦄MarcelloPGeorge Salib®Baldur BjarnasonyokselKyle HallAvakumov Egorlaurence hammondPaul IrishGaz ShawHelen BaikJoe SnellPatrick GreyYagnesh ModhЗарема ХалиловаWellington Torrejais da Silva (em 🏡) 🏳️‍🌈🇧🇷Muhammad Rehan SaeedDion AlmaerGeorge MerloccoScott DavisJaydn Edwards 🏳️‍🌈Rafael MorenoFernando MontoyaJosh #BlackLivesMatterIndieWeb Avatar for http://popurls.comPinboard PopularВеб-стандартыHenri Helvetica ✨👩🏾‍🚀✨ 🚀Tanner DolbyChristian Engel

201 Likes

Dylan VannMauricio DinarteCarey Evans 🇳🇿🧉rong-senAriel DeschapellKevin C. TofelJosh #BlackLivesMatterLouisBoldBeakNicholas AlmeidaNiko HalinkIago BarreiroAndrea MagnaboscoJacob WeimerMaximo CozettiJamie BeechWarren BuckleyBlack Lives MatterMikaelaRafael MorenoJohn MeyerhoferEric WallaceTom Hermans🤘Muhammad Rehan SaeedRus @ tetretalk.comShane ExterkampOle H SmylingsåsJeremy KatrasAndy | #BLMPhil KeysDavid TaborHans BurbanoDavid MerrileesAlejandro M. AlbertoWim HaubenMatt BuscheGeorge MerloccoWellington Torrejais da Silva (em 🏡) 🏳️‍🌈🇧🇷Gonzalo GuevaraSteve™Anand PrabhuCindyDion AlmaerLauraBfabian a. wohlgemuthmarkus staabBernhard HofmannkcShaun Bent 🇸🇪Nir AraziMGquadro Web AgencyJaydn Edwards 🏳️‍🌈Parthasarathi G KStephen CunliffeMarc Filleul 🇫🇷Bryan E.Alexei AccioLiliumJohn MichelJose GonzalezOleg KusovКирилл БелобеевArtur PaikinYagnesh ModhRaulGiridhar ShuklaAnthony McLinroyciferMartin Ager AdamsJiabinTony StenbergNi-ni-co-co-la-lásChirag JainPaul GrenierJohn F Croston IIIRalf BachЗарема ХалиловаPatrick HulceAl GrahamArmasherPatrick GreyErik EriksonRafael - International SEO Consultant 🔍 Hire me!Fitzy_LonghornJelmer de MaatJon C. JungerMarcoDanParkerBond usePrivilege(stopRacism)Casey RobinsonMark GohoHelen BaikYakimChenchelopHajiKhaled GarbayaKarel PersoonsJeff HobbsPerfReviews ⚡️eszterDan DenneySøren Birkemeyer 🦊Gaël 👨🏻‍💻Gaurang PatelTundeArthur AnstAnthony SappChuck GrimmettIván GonzálezAvakumov EgorZach AttackManaia Junior ⚡️AlexeyDavid Dal BuscoLeon BrocardAnushree Subramani 👑Milan MKyle HallAlex ClappertonThea 👩🏻‍💻🐱Nikhil PatilArtuto~Brandon McConnellGanapati V S 🇮🇳Toussaint JérémyyokselLauraMarcus RelacionRitik PatniRachele DiTullio, CPWA 🐱Travis MaynardZanderAppSeedMikie EtlingerDr. Adrian D. AlvarezAndre 🦄Bobby DuffNicolas RollierMohamed AzazyffadilaputraVishal & ViniArijit BhattacharyaDarpan KakadiaChrisMarcelloPAdrian ThomastanangularThamires HelaysaᴅᴇʀᴇᴋMichael GichiaBaldur BjarnasonPabloWBKJose Luis HernandoLaurieMatt FinucaneUli TroyoStephanie EcklesRob 👨‍🎨Robin RendleFie 🐼MarcoDMSara SoueidanErik Vorhesmubaris.eth 🚀StackroleAmit Kumar SinghDani de la CruzSailor SaturnBradley AllenErik Kroes 🏔Claude AyiteyDusanAndrea VaghiHoussein DjirdehJake DohmPrince WilsonMaxime Richardвкαя∂εℓℓVlad and stonesMatthias Ott§daire™Brantley HarrisLéa TortayJouni KantolaJoelSouhail KAOUSSIEleventyMatt StrömMax BöckJody DonettiRory JamesMatt Hobbs𝐆𝐨𝐤𝐮𝐥 | கோகுல்Hazim Sami / حازم ساميPeter AntoniusPelle WessmanJosua ZurbrüggDave RupertCarles MuiñosWolfram ⪡JS-- HTML++ CSS++⪢ Kriesing
26 Comments
  1. Mike Sherov - Black Lives Matter

    Now just add the css3 and html5 logos to your footer too.

  2. Zach Leatherman

    harsh but fair

  3. Mattia Astorino

    Also the current visitors counter is missing.

  4. Mattia Astorino

    😘

  5. Zach Leatherman

    y’all are killing me rn

  6. Mike Sherov - Black Lives Matter

    [valid xhtml 1.0]

  7. Mike Sherov - Black Lives Matter

    [verisign secure website ✅]

  8. Mike Sherov - Black Lives Matter

    [ 🔨 🚧 under construction 🔨 🚧 ]

  9. Zach Leatherman

    ahahaha I’m drunk with power

  10. Andy Bell

    this is v cool zach

  11. Mike Sherov - Black Lives Matter

    Please no. I'll stop

  12. Sara Soueidan

    Yessss thank you, Zach!

  13. Sara Soueidan

    One more todo for my Web site 😁 thanks for writing this up 😍

  14. Erik Vorhes

    Did adding Lighthouse Scores affect your Lighthouse Scores

  15. Chris

    lol 😆

  16. Zach Leatherman

    Yes I talk about this in the post 😅

  17. Mikie Etlinger

    @zachleat this is awesome. I’m curious to know if Speedlify can monitor sites outside of Netlify. Does it have to be hosted and monitor sites on Netlify?

  18. Zach Leatherman

    Yes it can monitor any URL! No it does not need to be hosted on Netlify! (Disclosure I work for Netify)

  19. Zach Leatherman

    Yay!

  20. Mikie Etlinger

    Cool. I’m going to give it a whirl. It would be cool if you could set thresholds for metrics and get a notification if the metric diffs fell outside of it.

  21. Zach Leatherman

    I probably won’t implement that but the code is open source!

  22. eszter

    what's the remaining 1%? :)

  23. Zach Leatherman

    probably that lighthouse widget I’m loading 😭

  24. Mark Goho

    A lesser person would have bumped that 99 to 100. Thanks for keeping it 99 with us Zach. 😂🤣

  25. Zach Leatherman

    this one is on the cusp and fluctuates back and forth 😅

  26. Markus Haack

    This was on my to-do list for a while. Just added Lighthouse scores to the footer of markus-haack.com and it looks great! My other sites will follow. Straightforward instructions from @zachleat provide everything you need to get started: zachleat.com/web/lighthouse…

Shamelessly plug your related post

These are webmentions via the IndieWeb and webmention.io.

Sharing on social media?

This is what will show up when you share this post on Social Media:

How did you do this? I automated my Open Graph images. (Peer behind the curtain at the test page)