fix analytis script URL to avoid one more request on our CDN#1520
fix analytis script URL to avoid one more request on our CDN#1520
Conversation
Tagging OptionsShould a new tag be published when this PR is merged?
|
📝 WalkthroughWalkthroughThis PR adds conditional routing for analytics scripts through a new environment-controlled flag. A boolean constant Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
analytics/loaders/OneDollarScript.ts (1)
79-88:⚠️ Potential issue | 🟠 MajorEncode
staticScriptUrlbefore embedding it in the query string.
Unencoded URLs can break the loader query (e.g.,&splits params), causing script load failures.✅ Suggested fix
- const trackerScript = `<script + const trackerSrc = USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT + ? `/live/invoke/website/loaders/analyticsScript.ts?url=${encodeURIComponent(staticScriptUrl)}` + : staticScriptUrl; + const trackerScript = `<script id="tracker" data-autocollect="false" data-hash-routing="true" data-url="${collector}" - src="${ - USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT - ? `/live/invoke/website/loaders/analyticsScript.ts?url=${staticScriptUrl}` - : staticScriptUrl - }" + src="${trackerSrc}" ></script>`;
🤖 Fix all issues with AI agents
In `@website/components/OneDollarStats.tsx`:
- Around line 107-109: When building the proxied analytics URL in the
OneDollarStats component, the query value uses raw staticScript which can break
if it contains characters like & or #; update the src construction that checks
USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT to pass encodeURIComponent(staticScript)
(referencing the OneDollarStats component, the staticScript variable and the
USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT flag) so the script URL is properly
URL-encoded when proxied through /live/invoke.
🧹 Nitpick comments (1)
analytics/loaders/OneDollarScript.ts (1)
3-5: Consider moving the env-derived flag to a shared config module.
Importing a TSX UI module from a loader couples server logic to UI code and forces evaluation of the component module. Extract the flag (and related constants, if useful) to a dedicated config file consumed by both sides.
| src={USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT | ||
| ? `/live/invoke/website/loaders/analyticsScript.ts?url=${staticScript}` | ||
| : staticScript} |
There was a problem hiding this comment.
URL-encode staticScript when proxying through /live/invoke.
Unencoded query values can break when the URL includes & or #, leading to failed script loads.
✅ Suggested fix
- src={USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT
- ? `/live/invoke/website/loaders/analyticsScript.ts?url=${staticScript}`
- : staticScript}
+ src={USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT
+ ? `/live/invoke/website/loaders/analyticsScript.ts?url=${encodeURIComponent(staticScript)}`
+ : staticScript}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| src={USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT | |
| ? `/live/invoke/website/loaders/analyticsScript.ts?url=${staticScript}` | |
| : staticScript} | |
| src={USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT | |
| ? `/live/invoke/website/loaders/analyticsScript.ts?url=${encodeURIComponent(staticScript)}` | |
| : staticScript} |
🤖 Prompt for AI Agents
In `@website/components/OneDollarStats.tsx` around lines 107 - 109, When building
the proxied analytics URL in the OneDollarStats component, the query value uses
raw staticScript which can break if it contains characters like & or #; update
the src construction that checks USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT to pass
encodeURIComponent(staticScript) (referencing the OneDollarStats component, the
staticScript variable and the USE_SITE_DOMAIN_FOR_ANALYTICS_SCRIPT flag) so the
script URL is properly URL-encoded when proxied through /live/invoke.
Summary by cubic
Switched the analytics script to load directly from the static URL by default, removing an extra CDN request and improving load time. Added an env flag to optionally keep using the site-domain proxy.
Written for commit b75fe60. Summary will update on new commits.
Summary by CodeRabbit