Skip to content

Commit eea0506

Browse files
fix: tracker script for astro
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 76f0dfe commit eea0506

File tree

7 files changed

+28
-8
lines changed

7 files changed

+28
-8
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/licenses-cargo.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

scripts/deploy-demo.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env bash
22
cd "$(dirname "$0")" && cd ..
3+
cd web && bun run build
4+
cd ..
35
cargo zigbuild --release --target x86_64-unknown-linux-musl
46
rsync -avzP target/x86_64-unknown-linux-musl/release/liwan pegasus:~/.local/bin/liwan

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ async fn main() -> Result<()> {
1717
}
1818

1919
let app = Liwan::try_new(config)?;
20-
app.run_background_tasks();
2120

21+
app.run_background_tasks();
2222
tokio::select! {
2323
res = web::start_webserver(app.clone(), s) => res,
2424
res = tokio::task::spawn_blocking(move || app.clone().events.process(r)) => res?

tracker/script.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare module "script" {
22
global {
33
interface Window {
44
__liwan_loaded?: boolean;
5+
navigation?: any;
56
}
67
}
78
export type EventOptions = {

tracker/script.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tracker/script.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
declare global {
22
interface Window {
33
__liwan_loaded?: boolean;
4+
// biome-ignore lint/suspicious/noExplicitAny:
5+
navigation?: any;
46
}
57
}
68

@@ -110,15 +112,30 @@ const trackPageviews = () => {
110112
event("pageview");
111113
};
112114

113-
if (window.history.pushState) {
115+
if (window.navigation) {
116+
// best case scenario, we can listen for navigation events
117+
// sadly this is not available on firefox
118+
window.navigation.addEventListener("navigate", () => page());
119+
} else {
120+
// duplicate navigation events don't matter
121+
// as we check if the page has changed above
122+
123+
// try to intercept history.pushState, but it's not always possible
114124
window.history.pushState = new Proxy(window.history.pushState, {
115125
apply: (target, thisArg, argArray) => {
116126
target.apply(thisArg, argArray);
117127
page();
118128
},
119129
});
130+
131+
// popstate is pretty buggy
120132
window.addEventListener("popstate", page);
133+
134+
// astro copies history.pushState, so we need to listen for astro:page-load
135+
document.addEventListener("astro:page-load", () => page());
121136
}
137+
138+
// initial pageview
122139
page();
123140
};
124141

0 commit comments

Comments
 (0)