Skip to content

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

Fix cache bust verification + function clause service error (#5808)

RobertJoonascef947e02cf9
diff --git a/extra/lib/plausible/installation_support/browserless_config.ex b/extra/lib/plausible/installation_support/browserless_config.ex
index a34807d336..25d457cf12 100644
--- a/extra/lib/plausible/installation_support/browserless_config.ex
+++ b/extra/lib/plausible/installation_support/browserless_config.ex
@@ -6,21 +6,21 @@ defmodule Plausible.InstallationSupport.BrowserlessConfig do
@retry_policy_by_status %{
# rate limit
- 429 => {:delay, :timer.seconds(1)},
+ 429 => {:delay, 1000},
# timeout
- 408 => {:delay, :timer.seconds(0.5)},
+ 408 => {:delay, 500},
# even 400 are verified manually to sometimes succeed on retry
- 400 => {:delay, :timer.seconds(0.5)}
+ 400 => {:delay, 500}
}
@doc """
Examples:
iex> retry_browserless_request([429, 400]).(nil, %{status: 429})
- {:delay, :timer.seconds(1)}
+ {:delay, 1000}
iex> retry_browserless_request([429, 400]).(nil, %{status: 400})
- {:delay, :timer.seconds(0.5)}
+ {:delay, 500}
iex> retry_browserless_request([429, 400]).(nil, %{status: 408})
nil
diff --git a/extra/lib/plausible/installation_support/checks/installation_v2.ex b/extra/lib/plausible/installation_support/checks/installation_v2.ex
index 3aa0667280..cae296f37f 100644
--- a/extra/lib/plausible/installation_support/checks/installation_v2.ex
+++ b/extra/lib/plausible/installation_support/checks/installation_v2.ex
@@ -102,7 +102,7 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do
timeoutMs: @plausible_window_check_timeout_ms,
timeoutBetweenAttemptsMs: @timeout_between_attempts_ms,
cspHostToCheck: PlausibleWeb.Endpoint.host(),
- url: Plausible.InstallationSupport.URL.bust_url(url),
+ url: url,
userAgent: Plausible.InstallationSupport.user_agent(),
debug: Application.get_env(:plausible, :environment) == "dev"
}
diff --git a/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex b/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex
index 81e9245e21..9101754d72 100644
--- a/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex
+++ b/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex
@@ -31,16 +31,15 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2CacheBust do
state
_known_installation_failure ->
- url_that_maybe_busts_cache =
- Plausible.InstallationSupport.URL.bust_url(url)
+ reset_diagnostics = %InstallationSupport.Verification.Diagnostics{
+ selected_installation_type: state.diagnostics.selected_installation_type
+ }
- state_after_cache_bust =
- Plausible.InstallationSupport.Checks.InstallationV2.perform(%{
- state
- | url: url_that_maybe_busts_cache
- })
-
- put_diagnostics(state_after_cache_bust, diagnostics_are_from_cache_bust: true)
+ state
+ |> struct!(diagnostics: reset_diagnostics)
+ |> struct!(url: InstallationSupport.URL.bust_url(url))
+ |> InstallationSupport.Checks.InstallationV2.perform()
+ |> put_diagnostics(diagnostics_are_from_cache_bust: true)
end
end
end
diff --git a/test/plausible/installation_support/verification/checks_test.exs b/test/plausible/installation_support/verification/checks_test.exs
index b0eb988898..4baa8ea60a 100644
--- a/test/plausible/installation_support/verification/checks_test.exs
+++ b/test/plausible/installation_support/verification/checks_test.exs
@@ -6,7 +6,7 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do
on_ee do
import Plausible.AssertMatches
- alias Plausible.InstallationSupport.Verification.Checks
+ alias Plausible.InstallationSupport.Verification.{Checks, Diagnostics}
alias Plausible.InstallationSupport.Result
use Plausible.Test.Support.DNS
import Plug.Conn
@@ -54,32 +54,46 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do
stub_lookup_a_records(expected_domain)
- verification_counter =
- stub_verification_result_i(fn i ->
- case i do
- 1 ->
- %{
- "completed" => true,
- "trackerIsInHtml" => false,
- "plausibleIsOnWindow" => false,
- "plausibleIsInitialized" => false
- }
+ counter = :atomics.new(1, [])
+
+ get_context_url_from_body = fn conn ->
+ {:ok, body, _conn} = Plug.Conn.read_body(conn)
+ JSON.decode!(body)["context"]["url"]
+ end
- 2 ->
- %{
- "completed" => true,
- "trackerIsInHtml" => true,
- "plausibleIsOnWindow" => true,
- "plausibleIsInitialized" => true,
- "testEvent" => %{
- "normalizedBody" => %{
- "domain" => "example.com"
- },
- "responseStatus" => 200
- }
+ stub_verification_result(fn conn ->
+ js_data =
+ if :atomics.add_get(counter, 1, 1) == 1 do
+ assert get_context_url_from_body.(conn) == url_to_verify
+
+ %{
+ "completed" => true,
+ "trackerIsInHtml" => false,
+ "plausibleIsOnWindow" => false,
+ "plausibleIsInitialized" => false
+ }
+ else
+ assert [_, "plausible_verification" <> _] =
+ String.split(get_context_url_from_body.(conn), "?")
+
+ %{
+ "completed" => true,
+ "trackerIsInHtml" => true,
+ "plausibleIsOnWindow" => true,
+ "plausibleIsInitialized" => true,
+ "testEvent" => %{
+ "normalizedBody" => %{
+ "domain" => "example.com"
+ },
+ "responseStatus" => 200
}
+ }
end
- end)
+
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(200, Jason.encode!(%{"data" => js_data}))
+ end)
assert_matches %Result{
ok?: false,
@@ -99,7 +113,56 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do
)
|> Checks.interpret_diagnostics()
- assert 2 == :atomics.get(verification_counter, 1)
+ assert 2 == :atomics.get(counter, 1)
+ end
+
+ test "cache bust diagnostics fully replace the initial installation check diagnostics" do
+ expected_domain = "example.com"
+ url_to_verify = "https://#{expected_domain}"
+
+ stub_lookup_a_records(expected_domain)
+
+ counter = :atomics.new(1, [])
+
+ stub_verification_result(fn conn ->
+ if :atomics.add_get(counter, 1, 1) == 1 do
+ js_data =
+ %{
+ "completed" => true,
+ "trackerIsInHtml" => false,
+ "plausibleIsOnWindow" => false,
+ "plausibleIsInitialized" => false
+ }
+
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(200, Jason.encode!(%{"data" => js_data}))
+ else
+ conn
+ |> put_resp_content_type("text/html")
+ |> send_resp(408, "Request has timed out")
+ end
+ end)
+
+ state =
+ Checks.run(url_to_verify, expected_domain, "manual",
+ report_to: nil,
+ async?: false,
+ slowdown: 0
+ )
+
+ assert_matches %Diagnostics{
+ tracker_is_in_html: nil,
+ plausible_is_on_window: nil,
+ plausible_is_initialized: nil,
+ service_error: ^any(:string, ~r/.*408.*/)
+ } = state.diagnostics
+
+ # Browserless gets called 3 times:
+ # 1) initial/regular installation check
+ # 2) cache bust installation check
+ # 3) retry due to #2 responding with 408
+ assert 3 == :atomics.get(counter, 1)
end
for {installation_type, expected_recommendation} <- [
@@ -524,10 +587,10 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do
end
end
- defp stub_verification_result(js_data) do
+ defp stub_verification_result(js_data) when is_map(js_data) do
counter = :atomics.new(1, [])
- Req.Test.stub(Plausible.InstallationSupport.Checks.InstallationV2, fn conn ->
+ stub_verification_result(fn conn ->
:atomics.add_get(counter, 1, 1)
conn
@@ -538,19 +601,8 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do
counter
end
- defp stub_verification_result_i(get_js_data) do
- counter = :atomics.new(1, [])
-
- Req.Test.stub(Plausible.InstallationSupport.Checks.InstallationV2, fn conn ->
- iteration = :atomics.add_get(counter, 1, 1)
- js_data = get_js_data.(iteration)
-
- conn
- |> put_resp_content_type("application/json")
- |> send_resp(200, Jason.encode!(%{"data" => js_data}))
- end)
-
- counter
+ defp stub_verification_result(f) do
+ Req.Test.stub(Plausible.InstallationSupport.Checks.InstallationV2, f)
end
end
end