@lovalingo/lovalingo 0.5.21 → 0.5.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/utils/api.js +28 -0
  2. package/package.json +1 -1
package/dist/utils/api.js CHANGED
@@ -1,4 +1,24 @@
1
1
  import { warnDebug, errorDebug } from './logger';
2
+ const OK_HTTP_STATUSES = new Set([200, 201]);
3
+ function getNavigationResponseStatus() {
4
+ if (typeof performance === "undefined" || typeof performance.getEntriesByType !== "function")
5
+ return null;
6
+ const entries = performance.getEntriesByType("navigation");
7
+ if (!entries || entries.length === 0)
8
+ return null;
9
+ const entry = entries[0];
10
+ const rawStatus = entry?.responseStatus;
11
+ const status = typeof rawStatus === "number" ? Math.floor(rawStatus) : NaN;
12
+ if (!Number.isFinite(status) || status <= 0)
13
+ return null;
14
+ return status;
15
+ }
16
+ // Why: avoid tracking misses/pageviews for non-OK responses (404/5xx) so bad routes don't pollute discovery.
17
+ function isOkHttpStatus(status) {
18
+ if (typeof status !== "number")
19
+ return true;
20
+ return OK_HTTP_STATUSES.has(status);
21
+ }
2
22
  export class LovalingoAPI {
3
23
  constructor(apiKey, apiBase, pathConfig, editKey) {
4
24
  this.entitlements = null;
@@ -126,6 +146,9 @@ export class LovalingoAPI {
126
146
  try {
127
147
  if (!this.hasApiKey())
128
148
  return;
149
+ const status = getNavigationResponseStatus();
150
+ if (!isOkHttpStatus(status))
151
+ return;
129
152
  const params = new URLSearchParams();
130
153
  params.set("key", this.apiKey);
131
154
  params.set("path", pathOrUrl);
@@ -154,6 +177,10 @@ export class LovalingoAPI {
154
177
  return null;
155
178
  if (!Array.isArray(misses) || misses.length === 0)
156
179
  return null;
180
+ const status = getNavigationResponseStatus();
181
+ if (!isOkHttpStatus(status)) {
182
+ return { ignored: true, reason: "http_status" };
183
+ }
157
184
  const pathParam = this.buildPathParam(opts?.pathOrUrl);
158
185
  const response = await fetch(`${this.apiBase}/functions/v1/misses`, {
159
186
  method: "POST",
@@ -164,6 +191,7 @@ export class LovalingoAPI {
164
191
  path: pathParam,
165
192
  source_locale: opts?.sourceLocale,
166
193
  locales: Array.isArray(opts?.locales) ? opts?.locales : undefined,
194
+ page_status: typeof status === "number" ? status : undefined,
167
195
  misses,
168
196
  }),
169
197
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovalingo/lovalingo",
3
- "version": "0.5.21",
3
+ "version": "0.5.22",
4
4
  "description": "React translation runtime with i18n routing, deterministic bundles + DOM rules, and zero-flash rendering.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",