|
| 1 | +// Partytown adapter — sugar around the existing `partytown` flag on every |
| 2 | +// provider's load(). Use this when you want the provider's CDN script to run |
| 3 | +// in a worker via Builder's Partytown library. |
| 4 | +// |
| 5 | +// Setup: |
| 6 | +// 1. Install @builder.io/partytown and add <PartytownScript /> to your <head>. |
| 7 | +// 2. Forward the provider URL via Partytown's forward config: |
| 8 | +// forwardSettings([ |
| 9 | +// 'Intercom', 'intercomSettings', |
| 10 | +// 'Tawk_API', 'Tawk_LoadStart', |
| 11 | +// // … |
| 12 | +// ]) |
| 13 | +// 3. Load with `partytown: true`. |
| 14 | + |
| 15 | +import type { ProviderName } from "../_types.ts"; |
| 16 | + |
| 17 | +const FORWARD: Partial<Record<ProviderName, readonly string[]>> = { |
| 18 | + intercom: ["Intercom", "intercomSettings"], |
| 19 | + crisp: ["$crisp", "CRISP_WEBSITE_ID", "CRISP_TOKEN_ID", "CRISP_RUNTIME_CONFIG"], |
| 20 | + tawk: ["Tawk_API", "Tawk_LoadStart"], |
| 21 | + zendesk: ["zE", "zESettings"], |
| 22 | + hubspot: ["HubSpotConversations", "hsConversationsSettings", "hsConversationsOnReady", "_hsq"], |
| 23 | + chatwoot: ["chatwootSDK", "$chatwoot", "chatwootSettings"], |
| 24 | + livechat: ["__lc", "LiveChatWidget"], |
| 25 | + drift: ["drift", "driftt"], |
| 26 | + freshchat: ["fcWidget", "fcSettings"], |
| 27 | + olark: ["olark"], |
| 28 | + userlike: ["userlikeMessenger"], |
| 29 | + helpscout: ["Beacon"], |
| 30 | + smartsupp: ["smartsupp", "_smartsupp"], |
| 31 | + liveagent: ["LiveAgent"], |
| 32 | + gist: ["gist", "gistAppId"], |
| 33 | + jivochat: [ |
| 34 | + "jivo_api", |
| 35 | + "jivo_onLoadCallback", |
| 36 | + "jivo_onOpen", |
| 37 | + "jivo_onClose", |
| 38 | + "jivo_onMessageSent", |
| 39 | + ], |
| 40 | + tidio: ["tidioChatApi"], |
| 41 | + sendbird: ["__sb_widget_settings"], |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * Returns the list of `forwardSettings` Partytown needs for a given provider. |
| 46 | + * Pass these (flat-spread across providers) into the @builder.io/partytown |
| 47 | + * <PartytownScript forward={...} />. |
| 48 | + */ |
| 49 | +export function partytownForward(...providers: ProviderName[]): string[] { |
| 50 | + const out = new Set<string>(); |
| 51 | + for (const p of providers) for (const k of FORWARD[p] ?? []) out.add(k); |
| 52 | + return [...out]; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Convenience: returns a PartytownConfig fragment ready to merge into the |
| 57 | + * Partytown script tag's `data-config` attribute. |
| 58 | + */ |
| 59 | +export function partytownConfig(...providers: ProviderName[]): { forward: string[] } { |
| 60 | + return { forward: partytownForward(...providers) }; |
| 61 | +} |
0 commit comments