Skip to content

Instantly share code, notes, and snippets.

@injms
Created July 28, 2022 10:23
Show Gist options
  • Select an option

  • Save injms/615f184e861430d777825722f31d2fc1 to your computer and use it in GitHub Desktop.

Select an option

Save injms/615f184e861430d777825722f31d2fc1 to your computer and use it in GitHub Desktop.
Quick script to get the the breakdown of assets by type for the top 100 pages on GOV.UK
{
"engines" : {
"node" : "18.4.0"
},
"dependencies": {
"puppeteer": "15.5.0"
}
}
{
"dateRange": {
"from": "30 June 2021",
"to": "30 June 2022"
},
"results": [
"/report-covid19-result",
"/",
"/sign-in-universal-credit",
"/order-coronavirus-rapid-lateral-flow-tests",
"/search/all",
"/provide-journey-contact-details-before-travel-uk",
"/sold-bought-vehicle",
"/get-coronavirus-test",
"/log-in-register-hmrc-online-services",
"/check-mot-history",
"/guidance/travel-to-england-from-another-country-during-coronavirus-covid-19",
"/vehicle-tax",
"/get-information-about-a-company",
"/guidance/red-amber-and-green-list-rules-for-entering-england",
"/check-vehicle-tax",
"/government/organisations/companies-house",
"/coronavirus",
"/contact-the-dvla",
"/foreign-travel-advice",
"/calculate-your-holiday-entitlement",
"/check-uk-visa",
"/find-travel-test-provider",
"/view-driving-licence",
"/state-pension-age",
"/foreign-travel-advice/spain/entry-requirements",
"/universal-credit",
"/government/organisations/hm-revenue-customs",
"/contact-ukvi-inside-outside-uk",
"/uk-border-control",
"/check-mot-status",
"/guidance/nhs-covid-pass",
"/view-prove-immigration-status",
"/sign-in-childcare-account",
"/guidance/travel-abroad-from-england-during-coronavirus-covid-19",
"/done/vehicle-tax",
"/claim-tax-refund",
"/student-finance-register-login",
"/guidance/coronavirus-covid-19-testing-for-people-travelling-to-england",
"/apply-renew-passport",
"/personal-tax-account",
"/foreign-travel-advice/spain",
"/get-vehicle-information-from-dvla",
"/personal-tax-account/sign-in/prove-identity",
"/check-if-you-need-tax-return",
"/guidance/covid-19-coronavirus-restrictions-what-you-can-and-cannot-do",
"/find-local-council",
"/book-driving-test",
"/government/publications/covid-19-stay-at-home-guidance/stay-at-home-guidance-for-households-with-possible-coronavirus-covid-19-infection",
"/contact/govuk/anonymous-feedback/thankyou",
"/pay-dartford-crossing-charge",
"/browse/visas-immigration",
"/browse/driving",
"/student-finance-calculator",
"/foreign-travel-advice/france/entry-requirements",
"/change-driving-test",
"/guidance/how-to-quarantine-when-you-arrive-in-england",
"/get-a-passport-urgently/1-week-fast-track-service",
"/book-theory-test",
"/renew-adult-passport/renew",
"/browse/driving/driving-licences",
"/bank-holidays",
"/get-a-passport-urgently",
"/log-in-file-self-assessment-tax-return",
"/renew-driving-licence",
"/government/organisations/driver-and-vehicle-licensing-agency",
"/national-minimum-wage-rates",
"/guidance/red-list-of-countries-and-territories",
"/calculate-your-redundancy-pay",
"/check-state-pension",
"/change-address-driving-licence",
"/self-assessment-tax-returns",
"/guidance/countries-with-approved-covid-19-vaccination-programmes-and-proof-of-vaccination",
"/get-tax-free-childcare",
"/benefits-calculators",
"/maternity-paternity-pay-leave",
"/council-tax-bands",
"/foreign-travel-advice/france",
"/pay-self-assessment-tax-bill",
"/register-to-vote",
"/foreign-travel-advice/italy/entry-requirements",
"/prove-right-to-work",
"/get-a-passport-urgently/online-premium-service",
"/government/organisations/hm-passport-office/contact/hm-passport-office-webchat",
"/foreign-travel-advice/portugal/entry-requirements",
"/foreign-travel-advice/usa/entry-requirements",
"/child-benefit-tax-calculator",
"/apply-first-provisional-driving-licence",
"/track-your-driving-licence-application",
"/log-in-file-self-assessment-tax-return/sign-in/prove-identity",
"/government/publications/guidance-for-contacts-of-people-with-possible-or-confirmed-coronavirus-covid-19-infection-who-do-not-live-with-the-person/guidance-for-contacts-of-people-with-possible-or-confirmed-coronavirus-covid-19-infection-who-do-not-live-with-the-person",
"/browse/driving/vehicle-tax-mot-insurance",
"/check-vehicle-e10-petrol",
"/calculate-statutory-sick-pay",
"/find-energy-certificate",
"/foreign-travel-advice/greece/entry-requirements",
"/browse/abroad/passports",
"/uk-border-control/before-you-leave-for-the-uk",
"/government/organisations/hm-passport-office/about-our-services",
"/find-a-job",
"/government/organisations/hm-revenue-customs/contact/income-tax-enquiries-for-individuals-pensioners-and-employees"
]
}
/*
Requires:
Node v18.4.0
Puppeteer v15.5.0
*/
import puppeteer from 'puppeteer'
import fs from 'node:fs'
import TOP_100_PAGES from './top100pages.json' assert { type: 'json' }
const getPageReport = async (url) => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(`https://www.gov.uk${url}`)
page.waitForNetworkIdle({
waitUntil: 'networkidle0',
})
const perf = await page.evaluate( () => {
return JSON.stringify([
performance.getEntriesByType("navigation"),
performance.getEntriesByType("resource"),
].flat())
})
const images = []
const javascript = []
const css = []
const font = []
const markup = []
const other = []
const pagePerformance = JSON.parse(perf)
pagePerformance.forEach( ({entryType, name, transferSize}) => {
name = name.toLowerCase()
// This ensures we capture both the markup size (which has an entryType of
// 'navigation') as well as all of the other assets loaded.
if (entryType === 'navigation') {
markup.push(transferSize)
} else {
switch (true) {
case name.endsWith('jpg'):
case name.endsWith('png'):
case name.endsWith('svg'):
case name.endsWith('gif'):
images.push(transferSize)
break
case name.endsWith('js'):
javascript.push(transferSize)
break
case name.endsWith('css') === true:
css.push(transferSize)
break
case name.endsWith('woff2'):
font.push(transferSize)
break
default:
other.push(transferSize)
}
}
})
const report = {
url,
images: images.length > 0 ? images.reduce((a,b) => a + b) : 0,
javascript: javascript.length > 0 ? javascript.reduce((a,b) => a + b) : 0,
css: css.length > 0 ? css.reduce((a,b) => a + b) : 0,
font: font.length > 0 ? font.reduce((a,b) => a + b) : 0,
markup: markup.length > 0 ? markup.reduce((a,b) => a + b) : 0,
other: other.length > 0 ? other.reduce((a,b) => a + b) : 0,
}
report.forCSV = report.url + ', ' +
report.images + ', ' +
report.javascript + ', ' +
report.css + ', ' +
report.font + ', ' +
report.markup + ', ' +
report.other
await browser.close()
return report
}
;(async () => {
const reports = TOP_100_PAGES['results'].map( async (url) => {
return await getPageReport(url)
})
const fullReports = await Promise.all(reports)
const header = 'URL, Images, JavaScript, CSS, Font, Markup, Other\n'
const body = fullReports.map( ({ forCSV }) => forCSV).join('\n')
fs.writeFileSync('csv__top100PagesSize.csv', header + body)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment