@nuxt/scripts 1.0.0-beta.4 → 1.0.0-beta.6

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.
@@ -1,8 +1,6 @@
1
1
  import { defineEventHandler, getHeaders, getRequestIP, readBody, getQuery, setResponseHeader, createError } from "h3";
2
2
  import { useRuntimeConfig } from "#imports";
3
- import { useStorage, useNitroApp } from "nitropack/runtime";
4
- import { hash } from "ohash";
5
- import { rewriteScriptUrls } from "../utils/pure.js";
3
+ import { useNitroApp } from "nitropack/runtime";
6
4
  import {
7
5
  SENSITIVE_HEADERS,
8
6
  anonymizeIP,
@@ -32,7 +30,7 @@ export default defineEventHandler(async (event) => {
32
30
  statusMessage: "First-party proxy not configured"
33
31
  });
34
32
  }
35
- const { routes, privacy: globalPrivacy, routePrivacy, cacheTtl = 3600, debug = import.meta.dev } = proxyConfig;
33
+ const { routes, privacy: globalPrivacy, routePrivacy, debug = import.meta.dev } = proxyConfig;
36
34
  const path = event.path;
37
35
  const log = debug ? (message, ...args) => {
38
36
  console.debug(message, ...args);
@@ -225,22 +223,7 @@ export default defineEventHandler(async (event) => {
225
223
  const responseContentType = response.headers.get("content-type") || "";
226
224
  const isTextContent = responseContentType.includes("text") || responseContentType.includes("javascript") || responseContentType.includes("json");
227
225
  if (isTextContent) {
228
- let content = await response.text();
229
- if (responseContentType.includes("javascript") && proxyConfig?.rewrites?.length) {
230
- const cacheKey = `nuxt-scripts:proxy:${hash(targetUrl + JSON.stringify(proxyConfig.rewrites))}`;
231
- const storage = useStorage("cache");
232
- const cached = await storage.getItem(cacheKey);
233
- if (cached && typeof cached === "string") {
234
- log("[proxy] Serving rewritten script from cache");
235
- content = cached;
236
- } else {
237
- content = rewriteScriptUrls(content, proxyConfig.rewrites);
238
- await storage.setItem(cacheKey, content, { ttl: cacheTtl });
239
- log("[proxy] Rewrote URLs in JavaScript response and cached");
240
- }
241
- setResponseHeader(event, "cache-control", `public, max-age=${cacheTtl}, stale-while-revalidate=${cacheTtl * 2}`);
242
- }
243
- return content;
226
+ return await response.text();
244
227
  }
245
228
  return Buffer.from(await response.arrayBuffer());
246
229
  });
@@ -7,7 +7,3 @@ export interface ProxyRewrite {
7
7
  /** Local path to rewrite to (e.g., '/_scripts/c/ga/g/collect') */
8
8
  to: string;
9
9
  }
10
- /**
11
- * Rewrite URLs in script content based on proxy config.
12
- */
13
- export declare function rewriteScriptUrls(content: string, rewrites: ProxyRewrite[]): string;
@@ -1,67 +0,0 @@
1
- import { parseURL, joinURL } from "ufo";
2
- export function rewriteScriptUrls(content, rewrites) {
3
- let result = content;
4
- const literalRegex = /(['"`])(.*?)\1/g;
5
- for (const { from, to } of rewrites) {
6
- const isSuffixMatch = from.startsWith(".");
7
- const fromSlashIdx = from.indexOf("/");
8
- const fromHost = fromSlashIdx > 0 ? from.slice(0, fromSlashIdx) : from;
9
- const fromPath = fromSlashIdx > 0 ? from.slice(fromSlashIdx) : "";
10
- result = result.replace(literalRegex, (match, quote, inner) => {
11
- if (!inner.includes(fromHost)) return match;
12
- const url = parseURL(inner);
13
- let shouldRewrite = false;
14
- let rewriteSuffix = "";
15
- if (url.host) {
16
- const hostMatches = isSuffixMatch ? url.host.endsWith(fromHost) : url.host === fromHost;
17
- if (hostMatches) {
18
- const fullPath = url.pathname + (url.search || "") + (url.hash || "");
19
- if (fromPath && fullPath.startsWith(fromPath)) {
20
- shouldRewrite = true;
21
- rewriteSuffix = fullPath.slice(fromPath.length);
22
- } else if (!fromPath) {
23
- shouldRewrite = true;
24
- rewriteSuffix = fullPath;
25
- }
26
- }
27
- } else if (inner.startsWith("//")) {
28
- const hostPart = inner.slice(2).split("/")[0];
29
- const hostMatches = isSuffixMatch ? hostPart?.endsWith(fromHost) ?? false : hostPart === fromHost;
30
- if (hostMatches) {
31
- const remainder = inner.slice(2 + (hostPart?.length ?? 0));
32
- if (fromPath && remainder.startsWith(fromPath)) {
33
- shouldRewrite = true;
34
- rewriteSuffix = remainder.slice(fromPath.length);
35
- } else if (!fromPath) {
36
- shouldRewrite = true;
37
- rewriteSuffix = remainder;
38
- }
39
- }
40
- } else if (fromPath && (inner.startsWith(from) || isSuffixMatch && inner.includes(from))) {
41
- const domainEnd = inner.indexOf(from) + from.length;
42
- const nextChar = inner[domainEnd];
43
- if (!nextChar || nextChar === "/" || nextChar === "?" || nextChar === "#") {
44
- shouldRewrite = true;
45
- rewriteSuffix = inner.slice(domainEnd);
46
- }
47
- }
48
- if (shouldRewrite) {
49
- const rewritten = rewriteSuffix === "/" || rewriteSuffix.startsWith("?") || rewriteSuffix.startsWith("#") ? to + rewriteSuffix : joinURL(to, rewriteSuffix);
50
- return quote + rewritten + quote;
51
- }
52
- return match;
53
- });
54
- }
55
- const gaRewrite = rewrites.find((r) => r.from.includes("google-analytics.com/g/collect"));
56
- if (gaRewrite) {
57
- result = result.replace(
58
- /"https:\/\/"\+\(.*?\)\+"\.google-analytics\.com\/g\/collect"/g,
59
- `"${gaRewrite.to}"`
60
- );
61
- result = result.replace(
62
- /"https:\/\/"\+\(.*?\)\+"\.analytics\.google\.com\/g\/collect"/g,
63
- `"${gaRewrite.to}"`
64
- );
65
- }
66
- return result;
67
- }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nuxt/scripts",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.4",
4
+ "version": "1.0.0-beta.6",
5
5
  "description": "Load third-party scripts with better performance, privacy and DX in Nuxt Apps.",
6
6
  "author": {
7
7
  "website": "https://harlanzw.com",
@@ -125,14 +125,14 @@
125
125
  "knitwork": "^1.3.0",
126
126
  "nuxt": "^4.3.1",
127
127
  "playwright-core": "^1.58.2",
128
- "posthog-js": "^1.353.1",
129
- "shiki": "^3.23.0",
128
+ "posthog-js": "^1.356.1",
129
+ "shiki": "^4.0.0",
130
130
  "typescript": "5.9.3",
131
131
  "vitest": "^4.0.18",
132
132
  "vue": "^3.5.29",
133
133
  "vue-router": "^5.0.3",
134
134
  "vue-tsc": "^3.2.5",
135
- "@nuxt/scripts": "1.0.0-beta.4"
135
+ "@nuxt/scripts": "1.0.0-beta.6"
136
136
  },
137
137
  "resolutions": {
138
138
  "@nuxt/scripts": "workspace:*"