Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(messaging): load the preference page stylesheet from its unhashed path (#117973)

Harley Alexander442f06ec1299
diff --git a/frontend/src/styles/message-preferences.css b/frontend/src/styles/message-preferences.css
index 4aefb500a88e..a4422724ab91 100644
--- a/frontend/src/styles/message-preferences.css
+++ b/frontend/src/styles/message-preferences.css
@@ -22,7 +22,7 @@
* this file into frontend/src, which made the output four times larger and, worse, let it
* satisfy classes these templates never reference. */
@import 'tailwindcss' source(none);
-@source '../../../posthog/templates/message_preferences';
+@source '../../../products/messaging/backend/templates/message_preferences';
/* The toggle knobs in preferences.html set `border-4` with no border color. Tailwind 4 draws an
* uncolored border in currentColor, which is black on these pages, so this sets gray-200 as the
diff --git a/products/messaging/backend/templates/message_preferences/_head.html b/products/messaging/backend/templates/message_preferences/_head.html
new file mode 100644
index 000000000000..c2ecb34d4ee6
--- /dev/null
+++ b/products/messaging/backend/templates/message_preferences/_head.html
@@ -0,0 +1,5 @@
+<meta charset="utf-8" />
+<meta name="viewport" content="width=device-width, initial-scale=1.0" />
+<title>Message preferences</title>
+{# Not the static tag: production hashes its URLs, and the CDN only serves unhashed names. #}
+<link rel="stylesheet" href="/static/message-preferences.css" />
diff --git a/products/messaging/backend/templates/message_preferences/error.html b/products/messaging/backend/templates/message_preferences/error.html
index df96ae819b07..498033baf036 100644
--- a/products/messaging/backend/templates/message_preferences/error.html
+++ b/products/messaging/backend/templates/message_preferences/error.html
@@ -1,13 +1,19 @@
-{% block content %}
- <div class="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
- <div class="max-w-md w-full space-y-8">
- <div>
- <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">Oops!</h2>
- <p class="mt-2 text-center text-sm text-gray-600">{{ error }}</p>
+<!DOCTYPE html>
+<html>
+ <head>{% include "message_preferences/_head.html" %}</head>
+ <body>
+ {% block content %}
+ <div class="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
+ <div class="max-w-md w-full space-y-8">
+ <div>
+ <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">Oops!</h2>
+ <p class="mt-2 text-center text-sm text-gray-600">{{ error }}</p>
+ </div>
+ <div class="mt-8 text-center">
+ <a href="/" class="text-blue-600 hover:text-blue-500">Return to homepage</a>
+ </div>
+ </div>
</div>
- <div class="mt-8 text-center">
- <a href="/" class="text-blue-600 hover:text-blue-500">Return to homepage</a>
- </div>
- </div>
- </div>
-{% endblock %}
+ {% endblock %}
+ </body>
+</html>
diff --git a/products/messaging/backend/templates/message_preferences/one_click_unsubscribe_success.html b/products/messaging/backend/templates/message_preferences/one_click_unsubscribe_success.html
index 3bdb144e96ee..d7a9f002b928 100644
--- a/products/messaging/backend/templates/message_preferences/one_click_unsubscribe_success.html
+++ b/products/messaging/backend/templates/message_preferences/one_click_unsubscribe_success.html
@@ -1,14 +1,6 @@
-{% load static %}
<!DOCTYPE html>
<html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Message preferences</title>
- {# Compiled from the Tailwind classes in this directory. Run #}
- {# `pnpm --filter=@posthog/frontend build:message-preferences` after changing a class here. #}
- <link rel="stylesheet" href="{% static 'message-preferences.css' %}" />
- </head>
+ <head>{% include "message_preferences/_head.html" %}</head>
<body>
{% block content %}
<div class="min-h-screen bg-gray-50">
diff --git a/products/messaging/backend/templates/message_preferences/preferences.html b/products/messaging/backend/templates/message_preferences/preferences.html
index 41aadf834528..fbdd9d1747c6 100644
--- a/products/messaging/backend/templates/message_preferences/preferences.html
+++ b/products/messaging/backend/templates/message_preferences/preferences.html
@@ -1,14 +1,9 @@
-{% load static %}
<!DOCTYPE html>
<html>
<head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Message preferences</title>
- {# Compiled from the Tailwind classes in this directory, including the ones the inline #}
- {# script below builds as strings. Run `pnpm --filter=@posthog/frontend #}
- {# build:message-preferences` after changing a class in either place. #}
- <link rel="stylesheet" href="{% static 'message-preferences.css' %}" />
+ {% include "message_preferences/_head.html" %}
+ {# The inline script below builds Tailwind classes as strings. Rebuild the stylesheet #}
+ {# after changing one of those too. #}
</head>
<body>
{% block content %}
diff --git a/products/messaging/backend/tests/api/test_message_preference_styles.py b/products/messaging/backend/tests/api/test_message_preference_styles.py
index 0bf88c96689b..da8c83ab315f 100644
--- a/products/messaging/backend/tests/api/test_message_preference_styles.py
+++ b/products/messaging/backend/tests/api/test_message_preference_styles.py
@@ -4,7 +4,7 @@ from pathlib import Path
from django.conf import settings
from django.test import SimpleTestCase
-TEMPLATE_DIR = Path(settings.BASE_DIR) / "posthog" / "templates" / "message_preferences"
+TEMPLATE_DIR = Path(settings.BASE_DIR) / "products" / "messaging" / "backend" / "templates" / "message_preferences"
STYLESHEET = Path(settings.BASE_DIR) / "frontend" / "public" / "message-preferences.css"
REBUILD = "pnpm --filter=@posthog/frontend build:message-preferences"
@@ -58,9 +58,11 @@ def _selector_present(css: str, utility: str) -> bool:
class TestMessagePreferenceStyles(SimpleTestCase):
def test_every_class_in_the_templates_is_in_the_compiled_stylesheet(self) -> None:
css = STYLESHEET.read_text()
+ templates = sorted(TEMPLATE_DIR.glob("*.html"))
+ assert templates, f"No templates found in {TEMPLATE_DIR}, so this test checks nothing."
missing: dict[str, set[str]] = {}
- for template in sorted(TEMPLATE_DIR.glob("*.html")):
+ for template in templates:
for attribute in _CLASS_ATTRIBUTE.findall(template.read_text()):
for token in _TEMPLATE_TAG.sub(" ", attribute).split():
if token in _SCRIPT_HOOKS or _TEMPLATE_SYNTAX.search(token) or not _UTILITY.match(token):
diff --git a/products/messaging/backend/tests/api/test_message_preferences.py b/products/messaging/backend/tests/api/test_message_preferences.py
index 0e0bd5a7dc42..4983422401cb 100644
--- a/products/messaging/backend/tests/api/test_message_preferences.py
+++ b/products/messaging/backend/tests/api/test_message_preferences.py
@@ -3,7 +3,7 @@ import json
from posthog.test.base import APIBaseTest, BaseTest
from unittest.mock import patch
-from django.test import Client
+from django.test import Client, override_settings
from django.urls import reverse
from parameterized import parameterized
@@ -22,6 +22,8 @@ from products.messaging.backend.models.message_preferences import (
PreferenceStatus,
)
+STYLESHEET_LINK = '<link rel="stylesheet" href="/static/message-preferences.css" />'
+
def mock_response(status_code: int, response_json: dict):
response = Response()
@@ -30,6 +32,13 @@ def mock_response(status_code: int, response_json: dict):
return response
+# Production storage, so a `{% static %}` link to the stylesheet fails to render here as it 404s there.
+@override_settings(
+ STORAGES={
+ "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
+ "staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"},
+ }
+)
class TestMessagePreferencesViews(BaseTest):
def setUp(self):
super().setUp()
@@ -66,6 +75,7 @@ class TestMessagePreferencesViews(BaseTest):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "message_preferences/preferences.html")
+ self.assertContains(response, STYLESHEET_LINK)
# Check context
self.assertEqual(response.context["recipient"].identifier, self.recipient.identifier)
@@ -92,6 +102,7 @@ class TestMessagePreferencesViews(BaseTest):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "message_preferences/one_click_unsubscribe_success.html")
+ self.assertContains(response, STYLESHEET_LINK)
self.recipient.refresh_from_db()
prefs = self.recipient.get_all_preferences()
@@ -124,6 +135,7 @@ class TestMessagePreferencesViews(BaseTest):
response = self.client.get(reverse("message_preferences", kwargs={"token": "invalid-token"}))
self.assertEqual(response.status_code, 400)
self.assertTemplateUsed(response, "message_preferences/error.html")
+ self.assertContains(response, STYLESHEET_LINK, status_code=400)
@patch("products.messaging.backend.presentation.views.recipient_preferences.validate_messaging_preferences_token")
def test_update_preferences_valid(self, mock_validate_messaging_preferences_token):