Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Verifier v1: Retry verification on JS navigation (#5599)

RobertJoonasbc1794cac7e8
diff --git a/lib/plausible/installation_support/checks/installation.ex b/lib/plausible/installation_support/checks/installation.ex
index b89e5efa38..50208a52d5 100644
--- a/lib/plausible/installation_support/checks/installation.ex
+++ b/lib/plausible/installation_support/checks/installation.ex
@@ -10,13 +10,34 @@ defmodule Plausible.InstallationSupport.Checks.Installation do
{:error, _} -> ""
end)
- # Puppeteer wrapper function that executes the vanilla JS verifier code
+ # Puppeteer wrapper function that executes the vanilla JS verifier code.
+
+ # ### NO AUTOMATIC TEST COVERAGE
+
+ # Unfortunately, as things stand today, this Puppeteer wrapper logic
+ # cannot be tested without spinning up a real Browserless instance or
+ # bringing in a bunch of test deps for Puppeteer. Therefore, take extra
+ # care when changing this and make sure to run manual tests on local
+ # browserless (`make browserless`) before releasing an update.
+
+ # ### TRICKY: Handling client side JS navigation.
+
+ # We've seen numerous cases where client JS navigates or refreshes the
+ # page after load. Any such JS behaviour destroys the Puppeteer page
+ # context, meaning that our verifier execution gets interrupted and we
+ # end up in the `catch` clause.
+
+ # To make our best effort verifying these sites, we retry (up to twice)
+ # running the verifier again if we encounter this specific error.
+
+ # Important: On retries, we work with the client-modified page context
+ # instead of calling `page.goto(context.url)` again (which would most
+ # likely result in another interruptive navigation).
@puppeteer_wrapper_code """
export default async function({ page, context }) {
- try {
- await page.setUserAgent(context.userAgent);
- await page.goto(context.url);
+ const MAX_RETRIES = 2
+ async function attemptVerification() {
await page.evaluate(() => {
#{@verifier_code}
});
@@ -24,6 +45,27 @@ defmodule Plausible.InstallationSupport.Checks.Installation do
return await page.evaluate(async (expectedDataDomain, debug) => {
return await window.verifyPlausibleInstallation(expectedDataDomain, debug);
}, context.expectedDataDomain, context.debug);
+ }
+
+ try {
+ await page.setUserAgent(context.userAgent);
+ await page.goto(context.url);
+
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
+ try {
+ return await attemptVerification()
+ } catch (error) {
+ const shouldRetry = typeof error?.message === 'string' && error.message.toLowerCase().includes('execution context')
+
+ if (shouldRetry && attempt <= MAX_RETRIES) {
+ // Brief delay before retry
+ await new Promise(resolve => setTimeout(resolve, 500))
+ continue
+ }
+
+ throw error
+ }
+ }
} catch (error) {
const msg = error.message ? error.message : JSON.stringify(error)
return {data: {completed: false, error: msg}}