Skip to content

feat: integrate Partytown with first-party proxy mode#654

Merged
harlan-zw merged 1 commit intomainfrom
feat/partytown-first-party
Mar 19, 2026
Merged

feat: integrate Partytown with first-party proxy mode#654
harlan-zw merged 1 commit intomainfrom
feat/partytown-first-party

Conversation

@harlan-zw
Copy link
Collaborator

❓ Type of change

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

📚 Description

Partytown and first-party proxy mode were completely separate code paths — Partytown scripts bypassed bundling/URL rewriting, and the intercept plugin (main-thread monkey-patches) couldn't reach Partytown's web worker. This meant scripts loaded from third-party domains and all tracking requests went unproxied.

This PR integrates the two via three points:

  1. resolveUrl generation — generates a Partytown resolveUrl function from the same intercept rules used by the main-thread intercept plugin. This is the web-worker equivalent — Partytown calls it for every network request (fetch, XHR, sendBeacon, Image) made by worker-executed scripts.

  2. skipApiRewrites for partytown scripts — when bundling scripts for Partytown, URL string literals are still rewritten but API call rewrites (fetch__nuxtScripts.fetch, etc.) are skipped since __nuxtScripts doesn't exist in the worker context and resolveUrl handles interception instead.

  3. Auto-configuration — after finalizeFirstParty, auto-configures @nuxtjs/partytown's resolveUrl from the computed intercept rules. Warns if user already has a custom resolveUrl.

Flow for a partytown + first-party script

  1. Build: Script downloaded, URLs rewritten via AST (API calls left alone), served as bundled asset
  2. SSR: useScript partytown quick-path renders <script src="/_scripts/assets/bundled.js" type="text/partytown"> (src already rewritten by transform)
  3. Worker: Partytown loads bundled script in worker. Static URLs already point to proxy. Dynamic URLs caught by resolveUrl → rewritten to /_scripts/p/*
  4. Server: Nitro proxy handler forwards to third-party with privacy transforms

Changed files

File Change
src/first-party/partytown-resolve.ts New — generatePartytownResolveUrl()
src/first-party/setup.ts finalizeFirstParty returns interceptRules alongside devtools data
src/first-party/index.ts Re-exports new function and type
src/module.ts Auto-configures partytown resolveUrl, passes partytownScripts to transform
src/plugins/rewrite-ast.ts skipApiRewrites option on rewriteScriptUrlsAST
src/plugins/transform.ts Accepts partytownScripts set, passes skipApiRewrites for partytown scripts
test/unit/rewrite-ast.test.ts Tests for skipApiRewrites + generatePartytownResolveUrl

Partytown scripts now work with first-party proxying. Previously, these
were completely separate code paths — Partytown bypassed bundling/rewriting
and the intercept plugin couldn't reach the web worker.

Three integration points:
- Generate Partytown `resolveUrl` from intercept rules (web-worker equivalent of the intercept plugin)
- Skip `__nuxtScripts.*` API rewrites for partytown scripts (avoids undefined reference in worker)
- Auto-configure `@nuxtjs/partytown` resolveUrl when both features are enabled
@vercel
Copy link
Contributor

vercel bot commented Mar 19, 2026

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

Project Deployment Actions Updated (UTC)
scripts-playground Building Building Preview, Comment Mar 19, 2026 11:13am

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 19, 2026

Open in StackBlitz

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

commit: 0cb02d3

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 86f4f42e-f22c-4f0b-989e-b255d511a95c

📥 Commits

Reviewing files that changed from the base of the PR and between d25917e and 0cb02d3.

📒 Files selected for processing (7)
  • src/first-party/index.ts
  • src/first-party/partytown-resolve.ts
  • src/first-party/setup.ts
  • src/module.ts
  • src/plugins/rewrite-ast.ts
  • src/plugins/transform.ts
  • test/unit/rewrite-ast.test.ts

📝 Walkthrough

Walkthrough

This pull request introduces Partytown integration support by adding a URL resolver generation mechanism and restructuring first-party configuration return values. A new generatePartytownResolveUrl function creates JavaScript code that resolves intercepted requests to first-party proxy paths based on configured rules. The finalizeFirstParty function now returns a structured FinalizeFirstPartyResult containing intercept rules and optional devtools data. The module automatically configures Partytown's resolveUrl when Partytown is enabled and intercept rules are available. AST rewriting logic is extended to conditionally skip API call rewrites for Partytown-managed scripts. Tests validate both the new resolve URL generator and conditional AST rewriting behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main feature: integrating Partytown with first-party proxy mode, which aligns with all changes in the PR.
Description check ✅ Passed The description includes the type of change (New feature), detailed explanation of the problem and solution, and comprehensive documentation of the integration flow and changed files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 feat/partytown-first-party
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

@harlan-zw harlan-zw merged commit 805182a into main Mar 19, 2026
10 checks passed
@harlan-zw harlan-zw deleted the feat/partytown-first-party branch March 19, 2026 11:21
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.

1 participant