Files
supabase/apps/studio/compat/next/script.tsx
Alaister YoungandAlaister Young e8765574ef fix(studio): execute next/script scripts in the TanStack build (#52497)
In the TanStack build, `next/script` is a compat shim that rendered a
plain React `<script>` element. React never executes scripts it creates
on the client, so `<Script>` silently did nothing. The only consumer is
`TelemetryTagManager` (`packages/common/telemetry.tsx`), so Google Tag
Manager never loaded on the dashboard: the tag was in the DOM, but no
`gtm.js` event and no loader request.

**Changed:**
- `compat/next/script.tsx` is now a client-only port of Next's
`next/script`: it renders nothing and injects the script imperatively in
an effect (`document.createElement`), with Next's attribute handling,
`onLoad`/`onReady`/`onError` semantics and `lazyOnload` strategy
- Scripts are deduped by `id`/`src` at module level, so remounts and
duplicate instances never execute the same script twice (GTM can't
double-initialize)
- `beforeInteractive` and `worker` load after mount like
`afterInteractive`, since there's no Next document or Partytown in the
SPA

**Added:**
- `compat/next/script.test.tsx` covering inline execution, dedupe across
remounts, `src` scripts, load/error callbacks and `lazyOnload`

No CSP change needed: `script-src` already allows `'unsafe-inline'` and
the Stape GTM host, and `connect-src`/`img-src`/`frame-src` cover its
collect hits and service-worker iframe.

## To test

- On the preview, with consent accepted, open devtools on any dashboard
page:
- `window.dataLayer` contains a `gtm.js` event and
`window.google_tag_manager` is defined
- The Network tab shows the GTM loader request (`4icgbaujh.js`) plus
collect hits
- Exactly one `script[src*="4icgbaujh"]` element exists, even after
navigating around
- No CSP violations ("Refused to load…") in the console, including on
sign-in/sign-up
- Opt out of analytics in Account → Preferences, reload, and confirm the
loader isn't requested

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-10-29 21:25:49 +08:00

182 lines
5.4 KiB
TypeScript

import { useEffect, useRef, type ComponentPropsWithoutRef, type ReactNode } from 'react'
// Client-only port of next/script (next/dist/client/script.js) for the
// TanStack build. React never executes <script> elements it renders on the
// client, so, like Next, we inject the script imperatively in an effect and
// render nothing. Scripts are deduped by `id` (or `src`) at module level, so
// remounts and duplicate instances don't execute the same script twice.
type Strategy = 'beforeInteractive' | 'afterInteractive' | 'lazyOnload' | 'worker'
interface ScriptProps extends Omit<
ComponentPropsWithoutRef<'script'>,
'children' | 'onLoad' | 'onError'
> {
strategy?: Strategy
// Inline body, as an alternative to dangerouslySetInnerHTML.
children?: ReactNode
onLoad?: (e: Event) => void
onReady?: () => void
onError?: (e: Event | string) => void
}
// src -> promise settled when that remote script loads.
const ScriptCache = new Map<string, Promise<void>>()
// Keys (id || src) of scripts that have finished loading.
const LoadCache = new Set<string>()
const DOMAttributeNames: Record<string, string> = {
acceptCharset: 'accept-charset',
className: 'class',
htmlFor: 'for',
httpEquiv: 'http-equiv',
noModule: 'noModule',
}
const ignoreProps = [
'onLoad',
'onReady',
'dangerouslySetInnerHTML',
'children',
'onError',
'strategy',
'stylesheets',
]
const isBooleanScriptAttribute = (attr: string) => ['async', 'defer', 'noModule'].includes(attr)
function setAttributesFromProps(el: HTMLScriptElement, props: ScriptProps) {
for (const [p, value] of Object.entries(props)) {
if (ignoreProps.includes(p) || value === undefined) continue
const attr = DOMAttributeNames[p] || p.toLowerCase()
if (isBooleanScriptAttribute(attr)) {
Reflect.set(el, attr, !!value)
} else {
el.setAttribute(attr, String(value))
}
// Setting then removing clears falsy booleans, including the implicit
// force-async on dynamically inserted scripts.
if (value === false || (isBooleanScriptAttribute(attr) && (!value || value === 'false'))) {
el.setAttribute(attr, '')
el.removeAttribute(attr)
}
}
}
const requestIdleCallback = (cb: () => void) => {
if (typeof window.requestIdleCallback === 'function') window.requestIdleCallback(cb)
else window.setTimeout(cb, 1)
}
function loadScript(props: ScriptProps) {
const {
src,
id,
onLoad,
onReady,
dangerouslySetInnerHTML,
children,
strategy = 'afterInteractive',
onError,
} = props
const cacheKey = id || src
if (cacheKey && LoadCache.has(cacheKey)) return
// Same remote script already loading: don't inject again, but still run
// this instance's callbacks once it settles.
const pending = src ? ScriptCache.get(src) : undefined
if (pending) {
pending.then(
() => {
if (cacheKey) LoadCache.add(cacheKey)
onLoad?.(new Event('load'))
onReady?.()
},
(e) => onError?.(e)
)
return
}
const afterLoad = () => {
onReady?.()
if (cacheKey) LoadCache.add(cacheKey)
}
const el = document.createElement('script')
const loadPromise = new Promise<void>((resolve, reject) => {
el.addEventListener('load', (e) => {
resolve()
onLoad?.(e)
afterLoad()
})
el.addEventListener('error', (e) => reject(e))
})
// Stays rejected for instances waiting on it. Dropping the failed entry
// lets a later mount retry the script instead of treating it as loaded.
loadPromise.catch((e) => {
if (src) ScriptCache.delete(src)
el.remove()
onError?.(e)
})
if (dangerouslySetInnerHTML) {
el.innerHTML = String(dangerouslySetInnerHTML.__html || '')
afterLoad()
} else if (children) {
el.textContent =
typeof children === 'string' ? children : Array.isArray(children) ? children.join('') : ''
afterLoad()
} else if (src) {
el.src = src
// Added to LoadCache only once the load event fires.
ScriptCache.set(src, loadPromise)
}
setAttributesFromProps(el, props)
el.setAttribute('data-nscript', strategy)
document.body.appendChild(el)
}
function loadLazyScript(props: ScriptProps) {
if (document.readyState === 'complete') {
requestIdleCallback(() => loadScript(props))
} else {
window.addEventListener('load', () => requestIdleCallback(() => loadScript(props)), {
once: true,
})
}
}
// eslint-disable-next-line no-restricted-exports
export default function Script(props: ScriptProps) {
const { id, src = '', onReady, strategy = 'afterInteractive' } = props
// Fire onReady on a remount when the script already loaded. The ref guards
// against StrictMode's double effect run, as in Next.
const hasOnReadyEffectCalled = useRef(false)
useEffect(() => {
const cacheKey = id || src
if (!hasOnReadyEffectCalled.current) {
if (onReady && cacheKey && LoadCache.has(cacheKey)) onReady()
hasOnReadyEffectCalled.current = true
}
}, [onReady, id, src])
const hasLoadScriptEffectCalled = useRef(false)
useEffect(() => {
if (hasLoadScriptEffectCalled.current) return
hasLoadScriptEffectCalled.current = true
if (strategy === 'lazyOnload') {
loadLazyScript(props)
} else {
// Studio is a client-rendered SPA with no Next document to inject into
// or Partytown worker, so beforeInteractive and worker scripts load
// after mount like afterInteractive.
loadScript(props)
}
}, [props, strategy])
return null
}