rspress-plugin-third-parties
High-performance third-party embeds, Google Tag Manager, analytics, and non-blocking script strategies for Rspress sites.
π Read the Documentation Β· π View Interactive Demo Β· π¦ NPM Package
β‘ Overview
rspress-plugin-third-parties brings Next.js-style third-party optimizations (@next/third-parties) directly to Rspress.
External embeds (like YouTube, Google Maps, and Twitter/X posts) and script tags (like GA4 and GTM) are notorious for blocking the main thread, lowering Lighthouse performance scores, and causing layout shifts. This plugin solves that by providing:
- β‘ React Compiler Pre-Optimized: Ships fully compiled with automated auto-memoization at build-time. Zero unnecessary re-renders.
- π Non-Blocking Execution: Lazy loading scripts using
requestIdleCallbackand fine-grained loading strategies. - β‘ React 19 Resource Pre-initialization: Leverages native
ReactDOM.preinitandReactDOM.preloadduring SSG/SSR. - π§ Smart Memory Caching: Built-in script deduplication and stylesheet cache registry.
- π οΈ Zero-Config Global GA: Inject Google Analytics across all documentation pages automatically via your
rspress.config.ts. - π¦ Zero-Weight Embeds: Powered by
third-party-capitalfor lightweight HTML generation.
π Table of Contents
- Installation
- Plugin Registration
- Component Reference
- Event Tracking Utility
- Script Loading Strategies
- Under the Hood Mechanics
- License
π¦ Installation
Install via your preferred package manager:
π Plugin Registration
To auto-inject Google Analytics globally across your Rspress documentation, register the plugin in your rspress.config.ts:
π§© Component Reference
<Script />
The foundation component powering all external integrations. It manages script mounting, deduplication, inline scripts, stylesheet preloading, and lifecycle events (onLoad, onReady, onError).
<Script /> Props
<YouTubeEmbed />
Powered by lite-youtube-embed under the hood. Renders an ultra-fast visual facade that defers the heavy YouTube player until play is clickedβkeeping your main thread pristine and Lighthouse scores flawless.
<YouTubeEmbed /> Props
<GoogleMapsEmbed />
Provides performance-optimized embeds for Google Maps without blocking the browser during initial navigation.
<GoogleMapsEmbed /> Props
<TweetEmbed />
Zero-overhead Twitter/X post embed powered by react-tweet. Fetches raw post data and renders lightweight, native React DOM components styled identically to X/Twitter UI without downloading heavy widgets.js scripts or layout-shifting IFrames.
<TweetEmbed /> Props
<GoogleAnalytics />
Explicitly embed Google Analytics 4 (GA4) inside MDX pages or custom layout components.
<GoogleAnalytics /> Props
<GoogleTagManager />
Integrate Google Tag Manager (GTM) with support for custom domains, authentication, preview environments, and custom initial dataLayer states.
<GoogleTagManager /> Props
You must provide either gtmId or a custom gtmScriptUrl containing the ID parameter.
π Event Tracking Utility
Send custom Google Analytics events dynamically from anywhere in your client-side React code:
π― Script Loading Strategies
π¬ Under the Hood Mechanics
- React 19 Resource Pre-init: Uses
ReactDOM.preinitandReactDOM.preloadto declare external resource hints before browser parsing. - Script Caching: Maintains global
ScriptCacheandLoadCacheMap/Set singletons, ensuring identical script tags are never loaded twice across page transitions. - Graceful React 18 Fallback: Fallbacks to dynamic
document.createElement('link')stylesheet injection when React 19 resource pre-init functions are unavailable.