From 61a072389ef43618668a5a0b798021a94156df04 Mon Sep 17 00:00:00 2001 From: Andrew Berrien <74077809+APB9785@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:03:22 -0600 Subject: [PATCH] Fix variant roll changing when fetching assets (#704) --- lib/beacon/plug.ex | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/beacon/plug.ex b/lib/beacon/plug.ex index f0c59c5a..ea57cd55 100644 --- a/lib/beacon/plug.ex +++ b/lib/beacon/plug.ex @@ -17,9 +17,32 @@ defmodule Beacon.Plug do """ @behaviour Plug + @private_routes [ + "__beacon_check__", + "__beacon_assets__", + "__beacon_media__" + ] + @impl Plug def init(_opts), do: [] @impl Plug - def call(conn, _opts), do: Plug.Conn.put_session(conn, :beacon_variant_roll, Enum.random(1..100)) + def call(conn, _opts) do + if Enum.any?(@private_routes, &(&1 in conn.path_info)) do + conn + else + put_roll(conn) + end + end + + defp put_roll(conn) do + path_list = conn.path_params["path"] + + with %{private: %{phoenix_live_view: {_, _, %{extra: %{session: %{"beacon_site" => site}}}}}} <- conn, + {_, _} <- Beacon.RouterServer.lookup_path(site, path_list, 1) do + Plug.Conn.put_session(conn, "beacon_variant_roll", Enum.random(1..100)) + else + _ -> conn + end + end end