@rsc-kit/mcp
Version:
An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.
153 lines (114 loc) • 4.55 kB
Markdown
# Third-party scripts
> Analytics, tag managers and widgets — and why there is no Script component.
Write a `<script>` tag. React 19 puts it in the `<head>` and sends it once, so
there is no `Script` component to import:
```tsx title="src/app/layout.tsx"
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<script async src="https://plausible.io/js/script.js" data-domain="example.com" />
</body>
</html>
)
}
```
## Adding an external script
Render the tag with `async` from a server component:
```tsx
<script async src="https://www.clarity.ms/tag/abc123" />
```
React **hoists** it into `<head>` and **deduplicates** it. Render the same
`src` in three components and one tag goes out. `async` means it never blocks
the page.
Put site-wide scripts in the root layout. The root layout renders once and is
kept across navigations, so the script runs once. In a page, a client-side
navigation renders the tag again, and whether the browser runs a script
inserted that way isn't something to rely on.
## Adding an inline snippet
Most analytics ship a few lines that stub a global and load the real script.
Google Analytics:
```tsx title="src/app/layout.tsx"
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX" />
<script
id="gtag-init"
dangerouslySetInnerHTML={{
__html: `window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXX');`,
}}
/>
```
Microsoft Clarity:
```tsx title="src/app/layout.tsx"
<script
id="ms-clarity"
dangerouslySetInnerHTML={{
__html: `(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "abc123");`,
}}
/>
```
An inline snippet renders where you wrote it and runs during parse,
**before hydration**. For a snippet whose job is to start recording as early
as possible, that is what its authors intended. Next's `Script` with
`strategy="afterInteractive"` would have run it later.
## Running a script after hydration
Some scripts touch DOM that React rendered, and find it missing if they run
during parse. Load those from a small client component of your own:
```tsx title="src/components/AfterHydration.tsx"
'use client'
import { useEffect } from 'react'
export function AfterHydration({ src, onLoad }: { src: string; onLoad?: () => void }) {
useEffect(() => {
const tag = document.createElement('script')
tag.src = src
tag.async = true
if (onLoad) tag.onload = onLoad
document.head.append(tag)
return () => tag.remove()
}, [src, onLoad])
return null
}
```
```tsx title="src/app/support/page.tsx"
<AfterHydration src="https://widget.example.com/chat.js" />
```
This is also where `onLoad` and `onReady` go: they run in the browser, so they
need a component.
## Loading a script when the page is idle
Wrap the effect's body in `requestIdleCallback`:
```tsx
useEffect(() => {
const id = requestIdleCallback(() => {
const tag = document.createElement('script')
tag.src = src
tag.async = true
document.head.append(tag)
})
return () => cancelIdleCallback(id)
}, [src])
```
That is Next's `lazyOnload`.
## Under a Content Security Policy
A script loaded by `src` needs its origin in `script-src`. An inline snippet in
a page the build stores is hashed and runs. On a page rendered per request,
render it from a client component and give it `useNonce()`'s nonce. See
[Content Security Policy](/guides/content-security-policy#your-own-inline-code).
## Porting from Next
| Next's `Script strategy` | Here |
| --- | --- |
| `beforeInteractive` | `<script src>` without `async`. The browser blocks on it, which is what that strategy meant. |
| `afterInteractive` | `<script async src>`, or an inline snippet |
| `lazyOnload` | [The idle effect](#loading-a-script-when-the-page-is-idle) |
| `worker` | Not supported. That was Partytown, a separate project. |
The `id` prop carries across unchanged.
## Why nothing ships for this
A `Script` component would wrap a `script` tag whose two useful behaviours,
hoisting and deduplication, React already provides. The package's own tests
pin that React does, so the day it stops, this page is what changes rather
than your app.