Skip to content

docs: add custom script integration patterns guide#637

Open
KlementMultiverse wants to merge 2 commits intonuxt:mainfrom
KlementMultiverse:docs/custom-script-patterns
Open

docs: add custom script integration patterns guide#637
KlementMultiverse wants to merge 2 commits intonuxt:mainfrom
KlementMultiverse:docs/custom-script-patterns

Conversation

@KlementMultiverse
Copy link

Summary

Adds a new documentation guide at docs/content/docs/1.guides/2.custom-script-patterns.md covering 5 common patterns for building custom script integrations with useScript():

  1. Basic integration — Wrapping a simple IIFE with useScript(), handling the promise chain and proxied functions
  2. API wrapping — Exposing strongly-typed methods when the third-party script is loaded (modeled after useScriptSegment)
  3. Consent management — Coordinating script load/unload with consent state using useScriptTriggerConsent()
  4. Error handling — Graceful degradation when scripts fail to load or are blocked by ad blockers
  5. Registry contribution checklist — Required files, composable structure, and step-by-step contribution guide

Also adds a See Also section to docs/content/docs/3.api/1.use-script.md linking to the new patterns guide.

Motivation

Addresses #636. Developers building custom integrations or contributing to the registry currently have to reverse-engineer patterns from existing implementations. This guide bridges the gap between the useScript() API reference and production integration patterns.

Changes

  • New file: docs/content/docs/1.guides/2.custom-script-patterns.md (267 lines)
  • Modified: docs/content/docs/3.api/1.use-script.md — added See Also section at the bottom

Thanks @harlan-zw for the invitation to contribute this!

Adds a new guide covering 5 common patterns for building custom script
integrations with useScript():

1. Basic integration - wrapping IIFE scripts
2. API wrapping - strongly-typed method exposure
3. Consent management - coordinating with useScriptTriggerConsent()
4. Error handling - graceful degradation for blocked/failed scripts
5. Registry contribution checklist - file structure and steps

Also adds a "See Also" section to the useScript() API docs linking
to the new guide.

Closes nuxt#636
@vercel
Copy link
Contributor

vercel bot commented Mar 6, 2026

Someone is attempting to deploy a commit to the Nuxt Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 6, 2026

Open in StackBlitz

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

commit: ee21425

@coderabbitai
Copy link

coderabbitai bot commented Mar 6, 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: 75c0b6bc-c387-4b4e-a167-8f8b7f9a328a

📥 Commits

Reviewing files that changed from the base of the PR and between 5a4eea6 and a52360e.

📒 Files selected for processing (1)
  • docs/content/docs/1.guides/2.custom-script-patterns.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/content/docs/1.guides/2.custom-script-patterns.md

📝 Walkthrough

Walkthrough

A new documentation page "Custom Script Patterns" was added at docs/content/docs/1.guides/2.custom-script-patterns.md describing patterns for integrating and wrapping third‑party scripts with useScript() (Basic Integration, API Wrapping, Consent Management, Error Handling, Registry Contribution), including TypeScript and Vue examples, proxy behavior, SSR notes, and a Registry Contribution Checklist. The useScript.md document (docs/content/docs/3.api/1.use-script.md) received a "See Also" section linking to Custom Script Patterns, Key Concepts, and Registry Scripts. No code or public API declarations were changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: adding a new documentation guide for custom script integration patterns with useScript().
Description check ✅ Passed The PR description comprehensively covers all required template sections: it specifies the documentation type of change, provides detailed motivation, explains the five patterns added, and lists the files modified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
docs/content/docs/1.guides/2.custom-script-patterns.md (1)

91-109: Simplify the example to avoid redundant destructuring.

The example destructures onMessage on line 93 but never uses it, then calls useMyChat() again on line 96 to get onLoaded. The onMessage used inside the callback (line 98) comes from the callback parameter, not line 93.

♻️ Proposed simplification
 <script setup lang="ts">
-const { open, onMessage } = useMyChat('your-token')
+const { open, onLoaded } = useMyChat('your-token')
 
 // Subscribe to messages after the script loads
-const { onLoaded } = useMyChat('your-token')
 onLoaded(({ onMessage }) => {
   onMessage((msg) => {
     console.log(`New message from ${msg.from}: ${msg.text}`)
   })
 })
 </script>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/content/docs/1.guides/2.custom-script-patterns.md` around lines 91 -
109, The example redundantly calls useMyChat twice and destructures onMessage
which is never used; simplify by calling useMyChat('your-token') once (capturing
open and onLoaded), remove the unused onMessage destructuring, and move the
subscription to messages inside the onLoaded callback via the provided onMessage
parameter so the code only uses open and onLoaded from useMyChat.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@docs/content/docs/1.guides/2.custom-script-patterns.md`:
- Around line 91-109: The example redundantly calls useMyChat twice and
destructures onMessage which is never used; simplify by calling
useMyChat('your-token') once (capturing open and onLoaded), remove the unused
onMessage destructuring, and move the subscription to messages inside the
onLoaded callback via the provided onMessage parameter so the code only uses
open and onLoaded from useMyChat.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: aeaa42af-59ce-4733-a2aa-575cff7cbb82

📥 Commits

Reviewing files that changed from the base of the PR and between 5a7278b and 5a4eea6.

📒 Files selected for processing (2)
  • docs/content/docs/1.guides/2.custom-script-patterns.md
  • docs/content/docs/3.api/1.use-script.md

Consolidate two useMyChat() calls into one and remove the unused
onMessage destructure. The onMessage parameter comes from the
onLoaded callback, not the outer scope.

Addresses CodeRabbit nitpick: redundant destructuring on line 93.
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