: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(auth): send no preview image from self-hosted instances (#122367)
Paul D'Ambraab169536242a
diff --git a/posthog/frontend_views.py b/posthog/frontend_views.pyindex c532a2c7e85e..ca92419169ed 100644--- a/posthog/frontend_views.py+++ b/posthog/frontend_views.py@@ -7,6 +7,7 @@ from django.http import HttpRequest, HttpResponse, HttpResponseRedirectfrom django.utils.http import url_has_allowed_host_and_schemefrom django.views.decorators.csrf import ensure_csrf_cookie+from posthog.cloud_utils import is_cloudfrom posthog.dataclasses import frozenfrom posthog.models.instance_setting import get_instance_settingfrom posthog.utils import render_template@@ -22,6 +23,7 @@ _REGION_HOSTS = {"us.posthog.com", "eu.posthog.com"}class _PageMetadata:title: strdescription: str+ self_hosted_description: str# Same share image as posthog.com, so links to the app and the website preview alike.@@ -32,6 +34,7 @@ _PUBLIC_PAGE_METADATA: dict[str, _PageMetadata] = {"/login": _PageMetadata(title="Log in • PostHog",description="Log in to PostHog, the single platform for engineers to analyze, test, observe, and deploy new features.",+ self_hosted_description="Log in to your self-hosted PostHog instance.",),"/signup": _PageMetadata(title="Sign up • PostHog",@@ -39,6 +42,7 @@ _PUBLIC_PAGE_METADATA: dict[str, _PageMetadata] = {"Create a free PostHog account to analyze, test, observe, and deploy new features. ""Every product has a monthly free tier, and no credit card is required."),+ self_hosted_description="Create an account on your self-hosted PostHog instance.",),}@@ -85,11 +89,18 @@ def public_page_metadata_context(request: HttpRequest) -> dict[str, str]:if metadata is None:return {}canonical_url = f"{settings.SITE_URL}{path}"+ if not is_cloud():+ return {+ "page_title": metadata.title,+ "page_description": metadata.self_hosted_description,+ "canonical_url": canonical_url,+ }return {"page_title": metadata.title,"page_description": metadata.description,"canonical_url": canonical_url,"preview_image_url": _PREVIEW_IMAGE_URL,+ "twitter_site": "@PostHog","structured_data_json": _structured_data_json(metadata, canonical_url),}diff --git a/posthog/templates/head.html b/posthog/templates/head.htmlindex e3f5810917b9..70777aa5e1c9 100644--- a/posthog/templates/head.html+++ b/posthog/templates/head.html@@ -35,11 +35,17 @@<meta property="og:title" content="{{ page_title }}"><meta property="og:description" content="{{ page_description }}"><meta property="og:url" content="{{ canonical_url }}">- <meta property="og:image" content="{{ preview_image_url }}">- <meta name="twitter:card" content="summary_large_image">- <meta name="twitter:site" content="@PostHog">- {# nosemgrep: python.flask.security.xss.audit.template-unescaped-with-safe.template-unescaped-with-safe, generic.html-templates.security.var-in-script-tag.var-in-script-tag (_structured_data_json escapes "<", ">" and "&", so the JSON cannot end the script element) #}- <script type="application/ld+json">{{ structured_data_json|safe }}</script>+ {% if preview_image_url %}+ <meta property="og:image" content="{{ preview_image_url }}">+ <meta name="twitter:card" content="summary_large_image">+ {% else %}+ <meta name="twitter:card" content="summary">+ {% endif %}+ {% if twitter_site %}<meta name="twitter:site" content="{{ twitter_site }}">{% endif %}+ {% if structured_data_json %}+ {# nosemgrep: python.flask.security.xss.audit.template-unescaped-with-safe.template-unescaped-with-safe, generic.html-templates.security.var-in-script-tag.var-in-script-tag (_structured_data_json escapes "<", ">" and "&", so the JSON cannot end the script element) #}+ <script type="application/ld+json">{{ structured_data_json|safe }}</script>+ {% endif %}{% endif %}{% if not opt_out_capture and js_posthog_api_key %}{{ posthog_bootstrap|page_json_script:"posthog-user-identity-with-flags" }}diff --git a/posthog/test/test_urls.py b/posthog/test/test_urls.pyindex 015e6e0f81a7..f584d0908861 100644--- a/posthog/test/test_urls.py+++ b/posthog/test/test_urls.py@@ -228,7 +228,7 @@ class TestPublicPageMetadata(SimpleTestCase):("invite_signup", "/signup/0b8e1f2a-1111-4c2d-9e3f-123456789abc", "PostHog", None),])- @override_settings(SITE_URL="https://us.example.com")+ @override_settings(SITE_URL="https://us.example.com", CLOUD_DEPLOYMENT="US")def test_head_renders_search_metadata(self, _name, request_path, expected_title, expected_canonical_path):request = RequestFactory().get(request_path)html = render_to_string("head.html", public_page_metadata_context(request))@@ -243,11 +243,26 @@ class TestPublicPageMetadata(SimpleTestCase):self.assertIn(f'<link rel="canonical" href="{canonical_url}">', html)self.assertIn(f'<meta property="og:url" content="{canonical_url}">', html)self.assertIn('<meta name="description" content="', html)+ self.assertIn('<meta property="og:image" content="https://posthog.com/images/og/default.png">', html)+ self.assertIn('<meta name="twitter:site" content="@PostHog">', html)structured_data = json.loads(html.split('<script type="application/ld+json">')[1].split("</script>")[0])self.assertEqual(structured_data["@type"], "WebPage")self.assertEqual(structured_data["url"], canonical_url)self.assertEqual(structured_data["name"], expected_title)+ @parameterized.expand([("login", "/login?next=/insights"), ("signup", "/signup/")])+ @override_settings(SITE_URL="https://hobby.example.com", CLOUD_DEPLOYMENT=None)+ def test_head_on_self_hosted_has_no_preview_image_or_posthog_marketing_metadata(self, _name, request_path):+ request = RequestFactory().get(request_path)+ html = render_to_string("head.html", public_page_metadata_context(request))++ self.assertNotIn("og:image", html)+ self.assertIn('<meta name="twitter:card" content="summary">', html)+ self.assertNotIn("posthog.com", html)+ self.assertNotIn("twitter:site", html)+ self.assertNotIn("application/ld+json", html)+ self.assertNotIn("free tier", html)+class TestLegacyDuckgresAdminUrls(SimpleTestCase):@parameterized.expand(