UNPKG

9.15 kBTypeScriptView Raw
1/**
2 * @public
3 */
4export declare type ApplyHook = (opts: ApplyHookOptions) => any;
5
6/**
7 * @public
8 */
9declare interface ApplyHookOptions extends HookOptions {
10 args: any[];
11}
12
13declare const ApplyPathKey: unique symbol;
14
15/**
16 * @public
17 */
18export declare type GetHook = (opts: GetHookOptions) => any;
19
20/**
21 * @public
22 */
23declare interface GetHookOptions extends HookOptions {
24}
25
26declare interface HookOptions {
27 name: string;
28 continue: Symbol;
29 nodeName: string | undefined;
30 constructor: string | undefined;
31 instance: WorkerInstance;
32 window: Window;
33}
34
35declare const InstanceDataKey: unique symbol;
36
37declare type InstanceId = string;
38
39declare const InstanceIdKey: unique symbol;
40
41declare const InstanceStateKey: unique symbol;
42
43declare const NamespaceKey: unique symbol;
44
45/**
46 * https://partytown.builder.io/configuration
47 *
48 * @public
49 */
50export declare interface PartytownConfig {
51 /**
52 * The `resolveUrl()` hook can be used to modify the URL about to be
53 * requested, which could be used to rewrite urls so they go through a proxy.
54 *
55 * https://partytown.builder.io/proxying-requests
56 *
57 * @param url - The URL to be resolved. This is a URL https://developer.mozilla.org/en-US/docs/Web/API/URL, not a string.
58 * @param location - The current window location.
59 * @param type - The type of resource the url is being resolved for. For example, `fetch` is the value when resolving for `fetch()`, and `a` would be the value when resolving for an anchor element's `href`.
60 * @returns The returned value must be a URL interface, otherwise the default resolved URL is used.
61 */
62 resolveUrl?(url: URL, location: Location, type: ResolveUrlType): URL | undefined | null;
63 /**
64 * The `resolveSendBeaconRequestParameters()` hook can be used to modify the RequestInit parameters
65 * being used by the fetch request that polyfills the navigator.sendBeacon API in the worker context.
66 *
67 * @param url - The URL to be resolved. This is a URL https://developer.mozilla.org/en-US/docs/Web/API/URL, not a string.
68 * @param location - The current window location.
69 * @returns The returned value must be a SendBeaconParameters interface, otherwise the default parameters are used.
70 */
71 resolveSendBeaconRequestParameters?(url: URL, location: Location): SendBeaconParameters | undefined | null;
72 /**
73 * When set to `true`, Partytown scripts are not inlined and not minified.
74 *
75 * https://partytown.builder.io/debugging
76 */
77 debug?: boolean;
78 /**
79 * Many third-party scripts provide a global variable which user code calls
80 * in order to send data to the service. For example, Google Tag Manager uses
81 * a [Data Layer](https://developers.google.com/tag-manager/devguide) array,
82 * and by pushing data to the array, the data is then sent on to GTM. Because
83 * we're moving third-party scripts to a web worker, the main thread needs to
84 * know which variables to patch first, and when Partytown loads, it can then
85 * forward the event data on to the service.
86 *
87 * Below is an example of Google Tag Manager and Facebook Pixel:
88 *
89 * ```js
90 * ['dataLayer.push', 'fbq']
91 * ```
92 *
93 * https://partytown.builder.io/forwarding-events
94 */
95 forward?: PartytownForwardProperty[];
96 /**
97 * The css selector where the sandbox should be placed.
98 * Default: body
99 */
100 sandboxParent?: string;
101 mainWindowAccessors?: string[];
102 /**
103 * Rarely, a script will add a named function to the global scope with the
104 * intent that other scripts can call the named function (like Adobe Launch).
105 * Due to how Partytown scopes each script, these named functions do not get
106 * added to `window`. The `globalFns` config can be used to manually ensure
107 * each function is added to the global scope. Consider this an escape hatch
108 * when a third-party script rudely pollutes `window` with functions.
109 */
110 globalFns?: string[];
111 /**
112 * This array can be used to filter which script are executed via
113 * Partytown and which you would like to execute on the main thread.
114 *
115 * @example loadScriptsOnMainThread:['https://test.com/analytics.js', 'inline-script-id', /regex-matched-script\.js/]
116 * // Loads the `https://test.com/analytics.js` script on the main thread
117 */
118 loadScriptsOnMainThread?: (string | RegExp)[];
119 get?: GetHook;
120 set?: SetHook;
121 apply?: ApplyHook;
122 /**
123 * When set to true, the Partytown Web Worker will respect the `withCredentials` option of XMLHttpRequests.
124 * Default: false
125 */
126 allowXhrCredentials?: boolean;
127 /**
128 * An absolute path to the root directory which Partytown library files
129 * can be found. The library path must start and end with a `/`.
130 * By default the files will load from the server's `/~partytown/` directory.
131 * Note that the library path must be on the same origin as the html document,
132 * and is also used as the `scope` of the Partytown service worker.
133 */
134 lib?: string;
135 /**
136 * Log method calls (debug mode required)
137 */
138 logCalls?: boolean;
139 /**
140 * Log getter calls (debug mode required)
141 */
142 logGetters?: boolean;
143 /**
144 * Log setter calls (debug mode required)
145 */
146 logSetters?: boolean;
147 /**
148 * Log Image() src requests (debug mode required)
149 */
150 logImageRequests?: boolean;
151 /**
152 * Log calls to main access, which also shows how many tasks were sent per message (debug mode required)
153 */
154 logMainAccess?: boolean;
155 /**
156 * Log script executions (debug mode required)
157 */
158 logScriptExecution?: boolean;
159 /**
160 * Log navigator.sendBeacon() requests (debug mode required)
161 */
162 logSendBeaconRequests?: boolean;
163 /**
164 * Log stack traces (debug mode required)
165 */
166 logStackTraces?: boolean;
167 /**
168 * Path to the service worker file. Defaults to `partytown-sw.js`.
169 */
170 swPath?: string;
171 /**
172 * The nonce property may be set on script elements created by Partytown.
173 * This should be set only when dealing with content security policies
174 * and when the use of `unsafe-inline` is disabled (using `nonce-*` instead).
175 *
176 * Given the following example:
177 * ```html
178 * <head>
179 * <script nonce="THIS_SHOULD_BE_REPLACED">
180 * partytown = {
181 * nonce: 'THIS_SHOULD_BE_REPLACED'
182 * };
183 * </script>
184 * </head>
185 * ```
186 *
187 * The `nonce` property should be generated by the server, and it should be unique
188 * for each request. You can leave a placeholder, as shown in the above example,
189 * to facilitate replacement through a regular expression on the server side.
190 * For instance, you can use the following code:
191 *
192 * ```js
193 * html.replace(/THIS_SHOULD_BE_REPLACED/g, nonce);
194 * ```
195 */
196 nonce?: string;
197}
198
199/**
200 * A forward property to patch on `window`. The forward config property is an string,
201 * representing the call to forward, such as `dataLayer.push` or `fbq`.
202 *
203 * https://partytown.builder.io/forwarding-events
204 *
205 * @public
206 */
207export declare type PartytownForwardProperty = string | PartytownForwardPropertyWithSettings;
208
209/**
210 * @public
211 */
212export declare type PartytownForwardPropertySettings = {
213 preserveBehavior?: boolean;
214};
215
216/**
217 * @public
218 */
219export declare type PartytownForwardPropertyWithSettings = [string, PartytownForwardPropertySettings?];
220
221/**
222 * Function that returns the Partytown snippet as a string, which can be
223 * used as the innerHTML of the inlined Partytown script in the head.
224 *
225 * @public
226 */
227export declare const partytownSnippet: (config?: PartytownConfig) => string;
228
229/**
230 * @public
231 */
232export declare type ResolveUrlType = 'fetch' | 'xhr' | 'script' | 'iframe' | 'image';
233
234/**
235 * The `type` attribute for Partytown scripts, which does two things:
236 *
237 * 1. Prevents the `<script>` from executing on the main thread.
238 * 2. Is used as a selector so the Partytown library can find all scripts to execute in a web worker.
239 *
240 * @public
241 */
242export declare const SCRIPT_TYPE = "text/partytown";
243
244/**
245 * @public
246 */
247declare type SendBeaconParameters = Pick<RequestInit, 'keepalive' | 'mode' | 'headers' | 'signal' | 'cache'>;
248
249/**
250 * @public
251 */
252export declare type SetHook = (opts: SetHookOptions) => any;
253
254/**
255 * @public
256 */
257declare interface SetHookOptions extends HookOptions {
258 value: any;
259 prevent: Symbol;
260}
261
262declare type WinId = string;
263
264declare const WinIdKey: unique symbol;
265
266declare interface WorkerInstance {
267 [WinIdKey]: WinId;
268 [InstanceIdKey]: InstanceId;
269 [ApplyPathKey]: string[];
270 [InstanceDataKey]: string | undefined;
271 [NamespaceKey]: string | undefined;
272 [InstanceStateKey]: {
273 [key: string]: any;
274 };
275}
276
277export { }