Skip to content

Commit

Permalink
fix: hit counter counting on all pages;
Browse files Browse the repository at this point in the history
chore: update deps:
  • Loading branch information
okikio committed Sep 21, 2021
1 parent caec998 commit cd3d718
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 75 deletions.
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions src/ts/critical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { App } from "@okikio/native";
import { Navbar } from "./services/Navbar";

import { themeSet, themeGet, runTheme } from "./scripts/theme";
import { hit } from "countapi-js";

import { Workbox } from "workbox-window";
import { animate } from "@okikio/animate";
Expand All @@ -20,21 +19,6 @@ try {
console.warn("Theming seems to break on this browser.", e);
}

// countapi-js hit counter. It counts the number of time the website is loaded
(async () => {
try {
let { value } = await hit("bundle.js.org", "visits");
let visitCounterEl = document.querySelector("#visit-counter");
if (visitCounterEl)
visitCounterEl.textContent = `(${value} Page Visits)`;
} catch (err) {
console.warn(
"Visit Counter Error (please create a new issue in the repo)",
err
);
}
})();

// navbar focus on scroll effect
let canScroll = true;
const navbar = document.querySelector(".navbar") as HTMLElement;
Expand Down
112 changes: 56 additions & 56 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
setState,
} from "./components/SearchResults";

import { hit } from "countapi-js";

import type { editor as Editor } from "monaco-editor";
import ESBUILD_WORKER_URL from "worker:./workers/esbuild.ts";
import WebWorker from "./util/WebWorker";
Expand Down Expand Up @@ -259,61 +261,59 @@ BundleEvents.on({
});
})();

// @ts-ignore
globalThis.requestIdleCallback =
globalThis.requestIdleCallback ??
function (cb) {
let start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
},
});
}, 1);
};
// Bundle worker
(() => {
const BundleWorker = new WebWorker(ESBUILD_WORKER_URL, WorkerArgs);

globalThis.requestIdleCallback(
() => {
const BundleWorker = new WebWorker(ESBUILD_WORKER_URL, WorkerArgs);

// bundles using esbuild and returns the result
BundleEvents.on("bundle", () => {
if (!initialized) return;
console.log("Bundle");
value = `` + editor?.getValue();

fileSizeEl.innerHTML = `<div class="loading"></div>`;
bundleTime.textContent = ``;

start = Date.now();
BundleWorker.postMessage(value);
});

// Emit bundle events based on WebWorker messages
BundleWorker.addEventListener(
"message",
({ data }: MessageEvent<{ event: string; details: any }>) => {
let { event, details } = data;
BundleEvents.emit(event, details);
}
);
// bundles using esbuild and returns the result
BundleEvents.on("bundle", () => {
if (!initialized) return;
console.log("Bundle");
value = `` + editor?.getValue();

window.addEventListener("pageshow", function (event) {
if (!event.persisted) {
BundleWorker?.start();
}
});

window.addEventListener("pagehide", function (event) {
if (event.persisted === true) {
console.log("This page *might* be entering the bfcache.");
} else {
console.log("This page will unload normally and be discarded.");
BundleWorker?.close();
}
});
},
{ timeout: 2000 }
);
fileSizeEl.innerHTML = `<div class="loading"></div>`;
bundleTime.textContent = ``;

start = Date.now();
BundleWorker.postMessage(value);
});

// Emit bundle events based on WebWorker messages
BundleWorker.addEventListener(
"message",
({ data }: MessageEvent<{ event: string; details: any }>) => {
let { event, details } = data;
BundleEvents.emit(event, details);
}
);

window.addEventListener("pageshow", function (event) {
if (!event.persisted) {
BundleWorker?.start();
}
});

window.addEventListener("pagehide", function (event) {
if (event.persisted === true) {
console.log("This page *might* be entering the bfcache.");
} else {
console.log("This page will unload normally and be discarded.");
BundleWorker?.close();
}
});
})();

// countapi-js hit counter. It counts the number of time the website is loaded
(async () => {
try {
let { value } = await hit("bundle.js.org", "visits");
let visitCounterEl = document.querySelector("#visit-counter");
if (visitCounterEl)
visitCounterEl.textContent = `(${value} Page Visits)`;
} catch (err) {
console.warn(
"Visit Counter Error (please create a new issue in the repo)",
err
);
}
})();

0 comments on commit cd3d718

Please sign in to comment.