Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/__tests__/lib/thirdParty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,36 @@ describe("thirdParty script registry", () => {
}
});

it("handles script load errors via onError callback", () => {
const onError = jest.fn();
const mod = loadWithEnv({
NEXT_PUBLIC_ANALYTICS_PROVIDER: "plausible",
NEXT_PUBLIC_ANALYTICS_DOMAIN: "proofofheart.xyz",
});

// Script configs returned by the registry have no onError by default.
for (const s of mod.getThirdPartyScripts()) {
mod.handleScriptError(s);
}
expect(onError).not.toHaveBeenCalled();

// When a consumer wires onError, handleScriptError invokes it.
const script = mod.getThirdPartyScripts()[0];
mod.handleScriptError({ ...script, onError });
expect(onError).toHaveBeenCalledTimes(1);
});

it("does not return duplicate script ids", () => {
const mod = loadWithEnv({
NEXT_PUBLIC_ANALYTICS_PROVIDER: "plausible",
NEXT_PUBLIC_ANALYTICS_DOMAIN: "proofofheart.xyz",
NEXT_PUBLIC_SUPPORT_WIDGET_SRC: "https://widget.example.com/w.js",
});

const ids = mod.getThirdPartyScripts().map((s) => s.id);
expect(new Set(ids).size).toBe(ids.length);
});

it("derives CSP origins from the configured scripts", () => {
const mod = loadWithEnv({
NEXT_PUBLIC_ANALYTICS_PROVIDER: "plausible",
Expand Down
3 changes: 2 additions & 1 deletion src/components/ThirdPartyScripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ export default function ThirdPartyScripts() {

return (
<>
{scripts.map(({ id, src, strategy, attributes }) => (
{scripts.map(({ id, src, strategy, attributes, onError }) => (
<Script
key={id}
id={id}
src={src}
strategy={strategy}
onError={onError}
// Funnel events fired during hydration are buffered by `analytics.ts`
// until the vendor global exists; this is where they get drained.
onLoad={id === analyticsId ? flushAnalyticsQueue : undefined}
Expand Down
17 changes: 15 additions & 2 deletions src/lib/thirdParty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface ThirdPartyScript {
strategy: ScriptStrategy;
/** Extra DOM attributes, e.g. Plausible's `data-domain`. */
attributes?: Record<string, string>;
/** Called when the script fails to load. */
onError?: () => void;
}

/** Supported privacy-first analytics vendors. */
Expand Down Expand Up @@ -124,10 +126,16 @@ export function getSupportWidgetScript(): ThirdPartyScript | null {
};
}

/** Every configured third-party script, in load order. */
/** Every configured third-party script, in load order (deduplicated by id). */
export function getThirdPartyScripts(): ThirdPartyScript[] {
const seen = new Set<string>();
return [getAnalyticsScript(), getSupportWidgetScript()].filter(
(script): script is ThirdPartyScript => script !== null,
(script): script is ThirdPartyScript => {
if (script === null) return false;
if (seen.has(script.id)) return false;
seen.add(script.id);
return true;
},
);
}

Expand Down Expand Up @@ -155,3 +163,8 @@ export function getThirdPartyScriptOrigins(): string[] {
export function getAnalyticsProvider(): AnalyticsProvider | null {
return getAnalyticsScript() ? readProvider() : null;
}

/** Invoke a script's error handler, if one is configured. */
export function handleScriptError(script: ThirdPartyScript): void {
script.onError?.();
}
Loading