-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Append a referrer param to the "Built with Reflex" badge link #6951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add the `REFLEX_REFERRER_PARAM` environment variable, read at compile time to append a `ref` query parameter to the "Built with Reflex" badge link. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The "Built with Reflex" badge appends a urlencoded `ref` query parameter to its reflex.dev link when the `REFLEX_REFERRER_PARAM` environment variable is set at compile time. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| """Components for displaying the Reflex sticky logo.""" | ||
|
|
||
| import urllib.parse | ||
|
|
||
| from reflex_base.components.component import ComponentNamespace | ||
| from reflex_base.environment import environment | ||
| from reflex_base.style import Style | ||
|
|
||
| from reflex_components_core.core.colors import color | ||
|
|
@@ -69,6 +72,18 @@ def add_style(self): | |
| }) | ||
|
|
||
|
|
||
| def _badge_href() -> str: | ||
| """Compute the badge link, appending the referrer param when set. | ||
|
|
||
| Returns: | ||
| The badge destination URL. | ||
| """ | ||
| referrer = environment.REFLEX_REFERRER_PARAM.get() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When AGENTS.md reference: AGENTS.md:L22-L23 Useful? React with 👍 / 👎. |
||
| if referrer: | ||
| return f"https://reflex.dev/?ref={urllib.parse.quote(referrer, safe='')}" | ||
| return "https://reflex.dev" | ||
|
|
||
|
|
||
| class StickyBadge(A): | ||
| """A badge that displays the Reflex sticky logo.""" | ||
|
|
||
|
|
@@ -82,7 +97,7 @@ def create(cls): | |
| return super().create( | ||
| StickyLogo.create(), | ||
| desktop_only(StickyLabel.create()), | ||
| href="https://reflex.dev", | ||
| href=_badge_href(), | ||
| target="_blank", | ||
| width="auto", | ||
| padding="0.375rem", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| """Tests for the "Built with Reflex" sticky badge.""" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This test covers AGENTS.md reference: AGENTS.md:L55-L58 Useful? React with 👍 / 👎. |
||
|
|
||
| import pytest | ||
| from reflex_components_core.core.sticky import StickyBadge | ||
|
|
||
|
|
||
| def _badge_href() -> str: | ||
| """Render a fresh badge and extract its href prop. | ||
|
|
||
| Returns: | ||
| The rendered href value, without surrounding quotes. | ||
| """ | ||
| props = StickyBadge.create().render()["props"] | ||
| href_prop = next(p for p in props if p.startswith("href:")) | ||
| return href_prop.removeprefix("href:").strip('"') | ||
|
|
||
|
|
||
| def test_badge_href_default(monkeypatch: pytest.MonkeyPatch): | ||
| """Without a referrer param, the badge links to the plain reflex.dev URL.""" | ||
| monkeypatch.delenv("REFLEX_REFERRER_PARAM", raising=False) | ||
| assert _badge_href() == "https://reflex.dev" | ||
|
|
||
|
|
||
| def test_badge_href_with_referrer(monkeypatch: pytest.MonkeyPatch): | ||
| """A referrer param is appended as a ref query parameter.""" | ||
| monkeypatch.setenv("REFLEX_REFERRER_PARAM", "owner-123") | ||
| assert _badge_href() == "https://reflex.dev/?ref=owner-123" | ||
|
|
||
|
|
||
| def test_badge_href_urlencodes_referrer(monkeypatch: pytest.MonkeyPatch): | ||
| """Special characters in the referrer param are urlencoded.""" | ||
| monkeypatch.setenv("REFLEX_REFERRER_PARAM", "a b&c/d?e=f") | ||
| assert _badge_href() == "https://reflex.dev/?ref=a%20b%26c%2Fd%3Fe%3Df" | ||
|
|
||
|
|
||
| def test_badge_href_empty_referrer(monkeypatch: pytest.MonkeyPatch): | ||
| """An empty referrer param falls back to the default URL.""" | ||
| monkeypatch.setenv("REFLEX_REFERRER_PARAM", "") | ||
| assert _badge_href() == "https://reflex.dev" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When production compilation uses the supported minimum dependency set,
reflex-base0.9.7 lacksREFLEX_REFERRER_PARAM, so_badge_href()raisesAttributeErrorwhile creating the sticky badge and aborts compilation. Please raise thereflex-baseminimum to a release containing this new attribute.Knowledge Base Used: Component package ecosystem