Skip to content

fix: proxy Snapchat bundled config loads#823

Open
harlan-zw wants to merge 3 commits into
mainfrom
fix/snapchat-bundle-config-proxy
Open

fix: proxy Snapchat bundled config loads#823
harlan-zw wants to merge 3 commits into
mainfrom
fix/snapchat-bundle-config-proxy

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

Resolves #822

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

Bundled scevent.min.js was deriving its config host from the local asset URL, then loading /config/... from the app origin. This adds scoped SDK patches for new URL(...).host and script-loader URL arguments so Snapchat config loads go through the tr.snapchat.com proxy without depending on current minified names.

The Snapchat first-party e2e now forces a fresh upstream bundle and fails if /config/... is requested from the app origin. The bundled SDK regression test also derives Snapchat proxy settings from the registry so domains and patches cannot drift silently.

✅ Verification

  • pnpm lint passes with the existing docs passive-voice warning
  • pnpm typecheck
  • pnpm build
  • pnpm vitest run test/unit/transform.test.ts test/unit/rewrite-ast.test.ts test/unit/bundle-sdk-patches.test.ts test/unit/proxy-configs.test.ts
  • pnpm vitest run --project e2e-dev test/e2e-dev/first-party.test.ts -t snapchatPixel

@vercel

vercel Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
scripts-playground Ready Ready Preview, Comment Jul 4, 2026 3:24am

@pkg-pr-new

pkg-pr-new Bot commented Jul 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/scripts@823

commit: b756a68

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

This PR adds two new SdkPatch variants and implements AST rewrite logic for host pinning and script-loader URL rewriting. The snapchatPixel registry now applies these patches in bundle and proxy modes. Tests were expanded with new unit coverage for the rewrite logic, an integration test for bundled proxy patching, a transform test for forced bundle cache bypass, and e2e updates for the renamed bundle-patched cache and same-origin request tracking. Related runtime typing, a Google Maps model type, and fixture files were also updated.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant NuxtScriptBundleTransformer
  participant rewriteScriptUrlsAST
  participant Snapchat SDK
  participant Nuxt proxy (/_scripts/p)

  NuxtScriptBundleTransformer->>rewriteScriptUrlsAST: bundled script + sdkPatches
  rewriteScriptUrlsAST->>rewriteScriptUrlsAST: collect scriptLoaderFunctionKeys
  rewriteScriptUrlsAST->>rewriteScriptUrlsAST: apply replace-new-url-host
  rewriteScriptUrlsAST->>rewriteScriptUrlsAST: apply replace-script-loader-url
  rewriteScriptUrlsAST-->>NuxtScriptBundleTransformer: patched script source
  NuxtScriptBundleTransformer-->>Snapchat SDK: serve patched script
  Snapchat SDK->>Nuxt proxy (/_scripts/p): request config via /config path
  Nuxt proxy (/_scripts/p)-->>Snapchat SDK: proxied response
Loading

Related issues: #822

Suggested labels: enhancement, bug

Suggested reviewers:

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR also changes unrelated Google Maps and useScript type code that is outside the Snapchat fix scope. Split the Google Maps and useScript type adjustments into a separate PR, or remove them if they are not required for #822.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR adds Snapchat SDK patches to pin host detection and rewrite config-loader URLs, which addresses #822.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title is concise and accurately summarizes the main Snapchat bundle config-loading fix.
Description check ✅ Passed The description matches the changeset and explains the bundled Snapchat config proxy fix and tests.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/snapchat-bundle-config-proxy

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
test/unit/bundle-sdk-patches.test.ts (1)

151-182: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Hardcoded snapchatPixel config duplicates registry.ts; risks drift.

This inline proxyConfigs.snapchatPixel mirrors the actual registry entry (packages/script/src/registry.ts) but isn't sourced from it, unlike the analogous suite in test/unit/rewrite-ast.test.ts which calls getProxyConfigs(). If the registry's sdkPatches/domains for snapchatPixel change, this test will keep passing against a stale snapshot instead of failing.

♻️ Suggested refactor: derive from registry
-        proxyConfigs: {
-          snapchatPixel: {
-            domains: ['sc-static.net', 'tr.snapchat.com', 'pixel.tapad.com'],
-            sdkPatches: [
-              { type: 'replace-new-url-host', host: 'sc-static.net' },
-              { type: 'replace-script-loader-url', fromDomain: 'tr.snapchat.com', pathPrefix: '/config' },
-            ],
-          } as any,
-        },
+        proxyConfigs: {
+          snapchatPixel: (await buildProxyConfigsFromRegistry(await registry())).snapchatPixel as any,
+        },
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/bundle-sdk-patches.test.ts` around lines 151 - 182, The Snapchat
proxy test is hardcoding a stale `proxyConfigs.snapchatPixel` snapshot instead
of using the shared registry data. Update the `applies snapchat config host
patches to bundled proxy scripts` test to derive `proxyConfigs` from the
registry source used elsewhere (for example via the same helper pattern as
`rewrite-ast.test.ts` and `getProxyConfigs()`), so `sdkPatches` and `domains`
stay aligned with `registry.ts` and future registry changes are caught.
test/unit/transform.test.ts (1)

645-687: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Drop the hash mock or assert the cache key. vi.mocked(hash).mockImplementationOnce(() => 'scevent.min') only affects the intermediate source filename/cache key; the emitted asset URL still comes from the fetched content hash. If this test isn’t checking cache naming, the mock is unnecessary.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/transform.test.ts` around lines 645 - 687, The `transform` test for
`useScriptSnapchatPixel` is over-mocking `hash` in a way that only affects the
intermediate cache key, not the emitted asset URL. Remove the
`vi.mocked(hash).mockImplementationOnce(...)` stub unless the test explicitly
asserts cache-key behavior, or update the expectations to verify the cache key
produced by the `bundle` path and its interaction with `mockBundleStorage`. Keep
the assertions focused on the actual `transform` output and the fetch bypass
behavior.
test/fixtures/first-party/nuxt.config.ts (1)

4-5: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Grammatically broken comment.

"Pages that need the real SDK load provide an explicit trigger" doesn't parse. Likely meant "Pages that need the real SDK should provide an explicit trigger...".

✏️ Suggested wording fix
 // trigger: 'manual' prevents the auto-generated plugin from loading all 18
-// scripts globally on every page. Pages that need the real SDK load provide an
-// explicit trigger in their composable call, keeping cross-provider noise low.
+// scripts globally on every page. Pages that need the real SDK should provide
+// an explicit trigger in their composable call, keeping cross-provider noise low.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/fixtures/first-party/nuxt.config.ts` around lines 4 - 5, The inline
comment in the Nuxt config is grammatically broken and should be rewritten for
clarity. Update the descriptive text near the existing explanatory comment so
that the sentence about pages needing the real SDK reads naturally, using the
same surrounding context in the configuration block. Keep the meaning intact
while fixing the wording in the comment text itself.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@test/fixtures/first-party/nuxt.config.ts`:
- Around line 4-5: The inline comment in the Nuxt config is grammatically broken
and should be rewritten for clarity. Update the descriptive text near the
existing explanatory comment so that the sentence about pages needing the real
SDK reads naturally, using the same surrounding context in the configuration
block. Keep the meaning intact while fixing the wording in the comment text
itself.

In `@test/unit/bundle-sdk-patches.test.ts`:
- Around line 151-182: The Snapchat proxy test is hardcoding a stale
`proxyConfigs.snapchatPixel` snapshot instead of using the shared registry data.
Update the `applies snapchat config host patches to bundled proxy scripts` test
to derive `proxyConfigs` from the registry source used elsewhere (for example
via the same helper pattern as `rewrite-ast.test.ts` and `getProxyConfigs()`),
so `sdkPatches` and `domains` stay aligned with `registry.ts` and future
registry changes are caught.

In `@test/unit/transform.test.ts`:
- Around line 645-687: The `transform` test for `useScriptSnapchatPixel` is
over-mocking `hash` in a way that only affects the intermediate cache key, not
the emitted asset URL. Remove the `vi.mocked(hash).mockImplementationOnce(...)`
stub unless the test explicitly asserts cache-key behavior, or update the
expectations to verify the cache key produced by the `bundle` path and its
interaction with `mockBundleStorage`. Keep the assertions focused on the actual
`transform` output and the fetch bypass behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bd56888e-76f0-4664-a05b-f77b2325e04d

📥 Commits

Reviewing files that changed from the base of the PR and between 385b058 and dfd7a13.

📒 Files selected for processing (9)
  • packages/script/src/plugins/rewrite-ast.ts
  • packages/script/src/registry.ts
  • packages/script/src/runtime/types.ts
  • test/e2e-dev/first-party.test.ts
  • test/fixtures/first-party/nuxt.config.ts
  • test/fixtures/first-party/pages/snap.vue
  • test/unit/bundle-sdk-patches.test.ts
  • test/unit/rewrite-ast.test.ts
  • test/unit/transform.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bundling the Snapchat pixel breaks it: scevent.min.js resolves its config URL against the page origin

1 participant