Skip to content

Commit

Permalink
Pull custom RUM code into its own module
Browse files Browse the repository at this point in the history
It's a bit surprising to have a bunch of our code tacked on to the end
of a vendored file. Our docs say the lux-measurer file "shouldn't ever
change", but that feels like a bit of a shaky assumption.

In any case, it feels much cleaner to have our code in its own file,
instead of muddling it with vendored code.

I've use script type=module for two reasons:

1) Like the design system, we can use it as a check that we're in a
   modern browser, which means we can use const / arrow functions and
   functions like `.some()` which might not be present in old browsers.
   For the same reason, we can simplify a lot of the checks around whether
   the performance API will be there, and whether it will work correctly.
2) It defers the execution of the script, so we'll automatically wait
   for the HTML to be fully parsed before our code runs. Although we're
   not interacting with the HTML, I think this should more or less
   guarantee that (a) the nextHopProtocol timing property is available
   and (b) the LUX global will already have been defined by lux-measurer.js
   Just in case (b) isn't always true, I've checked that LUX is defined
   before attempting to use it.

I'm also removing the try / catch code - it doesn't seem like it's doing
anything useful even if there is an error, and running as a module the
code shouldn't have any reference errors / surprisingly undefined APIs.
  • Loading branch information
richardTowers committed Jan 13, 2025
1 parent 2f86e97 commit d51d4ab
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function () {
/* global LUX, performance */
if (typeof LUX === 'undefined') { return }

const navigationPerformance = performance.getEntriesByType('navigation')[0]
if (!navigationPerformance) { return }

// As per RFC 147[1], this adds in monitoring of the type of HTTP protocol that
// is used when a browser loads a page.
// [1]: https://github.com/alphagov/govuk-rfcs/pull/148
LUX.addData('http-protocol', navigationPerformance.nextHopProtocol)
}())
Original file line number Diff line number Diff line change
Expand Up @@ -157,40 +157,3 @@ if (
LongTaskObserver.observe({ type: ["longtask"] });
} catch (e) {}
}

// As per RFC 147[1], this adds in monitoring of the type of HTTP protocol that
// is used when a browser loads a page.
//
// The User Timing API (aka window.performance) is used to record the data - to
// avoid the use of this from breaking the JavaScript for the small number of
// browsers that don't support it, it's been wrapped in a try/catch block plus a
// couple of checks to prevent "is not defined" errors.
//
// Because the `nextHopProtocol` isn't immediately available - it seems to need
// a request to be made before it's populated - we need to wait for the
// `DOMContentReady` event before we can see what the HTTP version is.
//
// [1]: https://github.com/alphagov/govuk-rfcs/pull/148

var measureHTTPProtocol = function () {
var getEntriesByType = performance.getEntriesByType('navigation')

if (typeof getEntriesByType !== 'undefined' && getEntriesByType.length > 0) {
var httpProtocol = getEntriesByType[0].nextHopProtocol
LUX.addData("http-protocol", httpProtocol)
}
}

try {
if (typeof performance !== 'undefined' && typeof performance.getEntriesByType !== 'undefined') {
if (document.readyState === 'complete') {
measureHTTPProtocol()
} else {
window.addEventListener('load', function() {
measureHTTPProtocol()
})
}
}
} catch (e) {
console.error('Error in LUX reporting the HTTP protocol (%s):', window.location, e)
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<title><%= title %></title>

<%= javascript_include_tag "govuk_publishing_components/vendor/lux/lux-measurer", { async: true } %>
<%= javascript_include_tag "govuk_publishing_components/rum-custom-data", type: "module" %>
<%= javascript_include_tag "govuk_publishing_components/rum-loader",
{
async: true,
Expand Down
1 change: 1 addition & 0 deletions spec/support/jasmine-browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"govuk_publishing_components/analytics-*.js",
"govuk_publishing_components/load-analytics-*.js",
"govuk_publishing_components/vendor/lux/lux-measurer-*.js",
"govuk_publishing_components/rum-custom-data-*.js",
"component_guide/accessibility-test-*.js",
"component_guide/filter-components-*.js",
"component_guide/audit-filter-*.js"
Expand Down

0 comments on commit d51d4ab

Please sign in to comment.