@qwik.dev/core
Version:
An open source framework for building instant loading web apps at any scale, without the extra effort.
96 lines (95 loc) • 3.78 kB
JavaScript
import { sync$, component$, isDev } from "@qwik.dev/core";
import { jsx } from "@qwik.dev/core/jsx-runtime";
const insightsPing = sync$(
() => ((w, d, l, n, p, r, S) => {
var publicApiKey = __QI_KEY__, postUrl = __QI_URL__, qVersion = d.querySelector(`[q\\:version]`)?.getAttribute(`q:version`) || "unknown", manifestHash = d.querySelector(`[q\\:manifest-hash]`)?.getAttribute(`q:manifest-hash`) || "dev", qSymbols = [], existingSymbols = /* @__PURE__ */ new Set(), flushSymbolIndex = 0, lastReqTime = 0, timeoutID, qRouteChangeTime = p.now(), qRouteEl = d.querySelector(`[q\\:route]`), flush = () => {
timeoutID = void 0;
if (qSymbols.length > flushSymbolIndex) {
var payload = {
qVersion,
publicApiKey,
manifestHash,
previousSymbol: flushSymbolIndex == 0 ? void 0 : qSymbols[flushSymbolIndex - 1].symbol,
symbols: qSymbols.slice(flushSymbolIndex)
};
n.sendBeacon(postUrl, S(payload));
flushSymbolIndex = qSymbols.length;
}
}, debounceFlush = () => {
timeoutID != void 0 && clearTimeout(timeoutID);
timeoutID = setTimeout(flush, 1e3);
};
w.qSymbolTracker = {
symbols: qSymbols,
publicApiKey
};
if (qRouteEl) {
new MutationObserver((mutations) => {
var mutation = mutations.find((m) => m.attributeName === `q:route`);
if (mutation) {
qRouteChangeTime = p.now();
}
}).observe(qRouteEl, { attributes: true });
}
d.addEventListener("visibilitychange", () => d.visibilityState === "hidden" && flush());
d.addEventListener(`qsymbol`, (_event) => {
var event = _event, detail = event.detail, symbolRequestTime = detail.reqTime, symbolDeliveredTime = event.timeStamp, symbol = detail.symbol;
if (!existingSymbols.has(symbol)) {
existingSymbols.add(symbol);
var route = qRouteEl?.getAttribute(`q:route`) || "/";
qSymbols.push({
symbol,
route,
delay: r(0 - lastReqTime + symbolRequestTime),
latency: r(symbolDeliveredTime - symbolRequestTime),
timeline: r(0 - qRouteChangeTime + symbolRequestTime),
interaction: !!detail.element
});
lastReqTime = symbolDeliveredTime;
debounceFlush();
}
});
w.addEventListener("error", (event) => {
var error = event.error;
if (!(error && typeof error === "object")) {
return;
}
var payload = {
url: `${l}`,
manifestHash,
timestamp: (/* @__PURE__ */ new Date()).getTime(),
source: event.filename,
line: event.lineno,
column: event.colno,
message: event.message,
error: "message" in error ? error.message : `${error}`,
stack: "stack" in error ? error.stack || "" : ""
};
n.sendBeacon(`${postUrl}error/`, S(payload));
});
})(window, document, location, navigator, performance, Math.round, JSON.stringify)
);
const Insights = component$(() => {
if (!__EXPERIMENTAL__.insights) {
throw new Error(
'Insights is experimental and must be enabled with `experimental: ["insights"]` in the `qwikVite` plugin.'
);
}
const key = globalThis.__QI_KEY__;
const url = globalThis.__QI_URL__;
if (!key || !url) {
if (!isDev) {
console.warn("<Insights />: no config from qwikInsights plugin, skipping...");
}
return null;
}
return /* @__PURE__ */ jsx("script", {
"document:onQInit$": insightsPing,
// We must pass the vite injected variables via window because sync$ code doesn't get replaced by the vite plugin
dangerouslySetInnerHTML: `__QI_KEY__=${JSON.stringify(key)};__QI_URL__=${JSON.stringify(url)}`
});
});
export {
Insights,
insightsPing
};