@ovineko/spa-guard 0.0.1-alpha-14 → 0.0.1-alpha-16

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/node/index.js +28 -17
  2. package/package.json +3 -3
@@ -9,10 +9,10 @@ import "../chunk-MLKGABMK.js";
9
9
 
10
10
  // src/node/index.ts
11
11
  import { negotiate } from "@fastify/accept-negotiator";
12
- import { Window } from "happy-dom";
13
12
  import { createHash } from "crypto";
14
13
  import { promisify } from "util";
15
14
  import { brotliCompress, gzip, zstdCompress } from "zlib";
15
+ import { parse, serialize, html as parse5Html } from "parse5";
16
16
  var gzipAsync = promisify(gzip);
17
17
  var brotliAsync = promisify(brotliCompress);
18
18
  var zstdAsync = promisify(zstdCompress);
@@ -93,23 +93,34 @@ function patchHtmlI18n(options) {
93
93
  if (!t) {
94
94
  return html;
95
95
  }
96
- const window = new Window();
97
- try {
98
- const document = window.document;
99
- document.write(html);
100
- document.documentElement.setAttribute("lang", resolvedLang);
101
- const meta = document.createElement("meta");
102
- meta.setAttribute("name", "spa-guard-i18n");
103
- meta.setAttribute("content", JSON.stringify(t));
104
- if (document.head) {
105
- document.head.prepend(meta);
106
- }
107
- const doctypeMatch = html.match(/^[\s\uFEFF]*(<!doctype[^>]*>)/i);
108
- const doctype = doctypeMatch ? doctypeMatch[1] : "";
109
- return doctype + document.documentElement.outerHTML;
110
- } finally {
111
- window.close();
96
+ const doc = parse(html);
97
+ const htmlEl = doc.childNodes.find(
98
+ (n) => n.nodeName === "html"
99
+ );
100
+ if (!htmlEl) return html;
101
+ const langAttr = htmlEl.attrs.find((a) => a.name === "lang");
102
+ if (langAttr) {
103
+ langAttr.value = resolvedLang;
104
+ } else {
105
+ htmlEl.attrs.push({ name: "lang", value: resolvedLang });
112
106
  }
107
+ const headEl = htmlEl.childNodes.find(
108
+ (n) => n.nodeName === "head"
109
+ );
110
+ if (!headEl) return html;
111
+ const meta = {
112
+ nodeName: "meta",
113
+ tagName: "meta",
114
+ attrs: [
115
+ { name: "name", value: "spa-guard-i18n" },
116
+ { name: "content", value: JSON.stringify(t) }
117
+ ],
118
+ childNodes: [],
119
+ parentNode: headEl,
120
+ namespaceURI: parse5Html.NS.HTML
121
+ };
122
+ headEl.childNodes.unshift(meta);
123
+ return serialize(doc);
113
124
  }
114
125
  function mergeTranslations(customTranslations) {
115
126
  const merged = { ...translations };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ovineko/spa-guard",
3
- "version": "0.0.1-alpha-14",
3
+ "version": "0.0.1-alpha-16",
4
4
  "description": "Chunk load error handling for SPAs with automatic recovery, beacon reporting, and deployment monitoring",
5
5
  "keywords": [
6
6
  "spa",
@@ -99,7 +99,7 @@
99
99
  "eslint": "^9 || ^10",
100
100
  "fastify": "^5 || ^4",
101
101
  "fastify-plugin": "^5 || ^4",
102
- "happy-dom": "^20",
102
+ "parse5": "^8",
103
103
  "react": "^19",
104
104
  "react-router": "^7",
105
105
  "typebox": "^1",
@@ -115,7 +115,7 @@
115
115
  "fastify-plugin": {
116
116
  "optional": true
117
117
  },
118
- "happy-dom": {
118
+ "parse5": {
119
119
  "optional": true
120
120
  },
121
121
  "react": {