-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
126 lines (106 loc) · 3.04 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Plugin } from "$fresh/server.ts";
import {
PartytownConfig,
partytownSnippet,
} from "https://esm.sh/@builder.io/[email protected]/integration";
import { copyLibFiles } from "./copyFiles.ts";
import { clearForward, readForward } from "./shared.ts";
interface Options {
copyFiles?: boolean;
proxyUrl?: string;
mainWindowAccessors?: string[];
}
declare global {
interface Window {
partytown: PartytownConfig;
}
}
function snippet(state: PartytownConfig) {
const params = new URLSearchParams(window.location.search);
const disabled = localStorage.getItem("disablePartytown");
const getState = (): PartytownConfig => {
if (params.has("disablePartytown") || params.has("gtm_debug") || disabled) {
console.debug("🎉 Running partytown scripts on main thread");
document
.querySelectorAll('script[type="text/partytown"]')
.forEach((node) => {
node.remove();
node.setAttribute("type", "text/javascript");
node.setAttribute("async", "");
document.body.appendChild(node);
});
localStorage.setItem("disablePartytown", "true");
console.log(
'Disabling partytown. To enable it again, run: localStorage.setItem("disablePartytown", "")',
);
return {
...state,
forward: undefined,
};
}
if (params.has("debugPartytown")) {
return {
...state,
debug: true,
};
}
return state;
};
window.partytown = getState();
}
/**
* Somehow, the partytown comment breaks the building process
* for newer versions of fresh
*/
const snippetNoComments = partytownSnippet().replace(
/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g,
"",
);
const partytown = (
{ copyFiles, proxyUrl, mainWindowAccessors }: Options = {},
): Plugin => {
if (copyFiles !== false) {
copyLibFiles().catch(console.error);
}
// TODO: Remove jitsu verification
return {
name: "partytown",
entrypoints: {
"main": `data:application/javascript,export default function(state){
(${snippet})(state);
window.partytown.mainWindowAccessors = ${
JSON.stringify(mainWindowAccessors ?? [])
};
window.partytown.resolveUrl = function (url, location, type) {
const proxyUrl = ${proxyUrl ? `'${proxyUrl}'` : "undefined"};
if (!proxyUrl) { return url }
if (url.href.includes(proxyUrl) || url.hostname.includes("jitsu")) {
return url;
}
if (proxyUrl) {
const finalProxyUrl = new URL(location.origin + proxyUrl);
finalProxyUrl.searchParams.append("url", url.href);
return finalProxyUrl;
}
return url;
};
if (window === top) {
${snippetNoComments}
} else {
console.info("Partytown snippet not loaded due to being inside an iframe");
}
}`,
},
render(ctx) {
clearForward();
ctx.render();
return {
scripts: [{
entrypoint: "main",
state: readForward(),
}],
};
},
};
};
export default partytown;