Files
david_bai dceaae8efa fix(ssr): guard DOM/window access and client-only listeners
- Prevent server-side exceptions (Application error) on mobile after redirects
  - useOneShotSlowHint: guard document; use global setTimeout; conditionally attach visibilitychange
  - useConnectionFeedback: guard document.visibilityState; register/remove listeners only on client
  - usePageSetup: guard window before tracking referrer and parsing roomId
  - tracking: early return when window is undefined
  - docs(flows): add “SSR & DOM access guard (must-read)” checklist; renumber next section
2025-12-06 12:00:03 +08:00

22 lines
834 B
TypeScript

import { setTrack } from "@/app/config/api";
// The website tracks the source through ?ref=reddit..., here to get the source, for example https://yourdomain.com?ref=producthunt
export const trackReferrer = async () => {
if (typeof window === "undefined") return;
// Get URL parameters
const urlParams = new URLSearchParams(window.location.search);
let ref = urlParams.get("ref");
if (process.env.NODE_ENV === "production") {
ref = urlParams.get("ref") || "noRef"; // Production environment, count daily active users, record as noRef if there is no ref
}
if (ref) {
try {
setTrack(ref);
// Optional: Store the source in localStorage for subsequent tracking
// localStorage.setItem('initial_ref', ref);
} catch (error) {
console.error("Failed to track referrer:", error);
}
}
};