-
Notifications
You must be signed in to change notification settings - Fork 69
/
Link.astro
73 lines (62 loc) · 1.43 KB
/
Link.astro
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
---
let phid = false;
try {
const to = new URL(Astro.props.href);
if (
to &&
to.hostname != "status.determinate.systems" &&
to.hostname != "trust.determinate.systems" &&
(to.hostname == "flakehub.com" ||
to.hostname == "determinate.systems" ||
to.hostname.endsWith(".determinate.systems"))
) {
phid = true;
}
} catch {
// that's fine
}
const external = (Astro.props.href || "").startsWith("http");
const rel = external && !phid ? "noopener noreferrer" : undefined;
const target = external ? "_blank" : undefined;
---
{
phid ? (
<ph-id>
<a {rel} {target} {...Astro.props}>
<slot />
</a>
</ph-id>
) : (
<a {rel} {target} {...Astro.props}>
<slot />
</a>
)
}
<script>
class Phid extends HTMLElement {
connectedCallback() {
try {
const link = this.querySelector("a");
if (!link) {
return;
}
const to = new URL(link.href);
const params = to.searchParams;
link.addEventListener("click", () => {
if (!window.posthog) {
return;
}
const ogHref = link.href;
setTimeout(() => {
link.href = ogHref;
}, 500);
params.set("phid", window.posthog.get_distinct_id());
link.href = to.toString();
});
} catch {
// that's fine
}
}
}
customElements.define("ph-id", Phid);
</script>