Merge pull request #199 from PR0M3TH3AN/codex/fix-video-view-count-on-cold-start

Wait for app initialization before handling hash views
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2025-09-30 23:54:07 -04:00
commit 41a0d15e7a
2 files changed
+46 -26

No files matched your search

+2 -1
View File
@@ -10853,5 +10853,6 @@ function dedupeToNewestByRoot(videos) {
}
export const app = new bitvidApp();
app.init();
export const appReady = app.init();
window.app = app;
window.appReady = appReady;
+44 -25
View File
@@ -362,37 +362,56 @@ function handleQueryParams() {
}
}
function handleHashChange() {
async function waitForAppInitialization() {
const maybePromise = window.appReady;
if (!maybePromise || typeof maybePromise.then !== "function") {
return;
}
try {
await maybePromise;
} catch (error) {
console.warn(
"Proceeding with hash handling despite app initialization failure:",
error
);
}
}
async function handleHashChange() {
console.log("handleHashChange called, current hash =", window.location.hash);
await waitForAppInitialization();
const hash = window.location.hash || "";
// Use a regex that captures up to the first ampersand or end of string.
// E.g. "#view=channel-profile&npub=..." => viewName = "channel-profile"
const match = hash.match(/^#view=([^&]+)/);
if (!match || !match[1]) {
// No valid "#view=..." => default to "most-recent-videos"
import("./viewManager.js").then(({ loadView, viewInitRegistry }) => {
loadView("views/most-recent-videos.html").then(() => {
const initFn = viewInitRegistry["most-recent-videos"];
if (typeof initFn === "function") {
initFn();
}
});
});
return;
try {
const { loadView, viewInitRegistry } = await import("./viewManager.js");
if (!match || !match[1]) {
// No valid "#view=..." => default to "most-recent-videos"
await loadView("views/most-recent-videos.html");
const initFn = viewInitRegistry["most-recent-videos"];
if (typeof initFn === "function") {
initFn();
}
return;
}
const viewName = match[1]; // only the chunk before any '&'
const viewUrl = `views/${viewName}.html`;
// Now dynamically load that partial, then call its init function
await loadView(viewUrl);
const initFn = viewInitRegistry[viewName];
if (typeof initFn === "function") {
initFn();
}
} catch (error) {
console.error("Failed to handle hash change:", error);
}
const viewName = match[1]; // only the chunk before any '&'
const viewUrl = `views/${viewName}.html`;
// Now dynamically load that partial, then call its init function
import("./viewManager.js").then(({ loadView, viewInitRegistry }) => {
loadView(viewUrl).then(() => {
const initFn = viewInitRegistry[viewName];
if (typeof initFn === "function") {
initFn();
}
});
});
}