@lovrozagar/flare 0.1.2 → 0.2.1

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 (85) hide show
  1. package/README.md +42 -19
  2. package/package.json +8 -6
  3. package/src/broadcast/index.ts +1 -1
  4. package/src/broadcast/provider.tsx +2 -2
  5. package/src/caches/index.ts +26 -0
  6. package/src/components/await.tsx +36 -39
  7. package/src/components/dev-error-overlay.tsx +8 -9
  8. package/src/components/direction-script.tsx +2 -2
  9. package/src/components/locale-script.tsx +2 -2
  10. package/src/components/reset-css.tsx +1 -1
  11. package/src/components/ssr-context.tsx +7 -14
  12. package/src/components/theme-script.tsx +2 -2
  13. package/src/components/view-transition-css.tsx +1 -1
  14. package/src/defer/index.ts +22 -0
  15. package/src/direction/index.tsx +61 -36
  16. package/src/errors/index.ts +45 -11
  17. package/src/fonts/component.tsx +2 -1
  18. package/src/form/index.tsx +18 -12
  19. package/src/generators/index.ts +73 -30
  20. package/src/head-client/index.ts +86 -29
  21. package/src/history/index.ts +6 -0
  22. package/src/hydrate/index.tsx +90 -60
  23. package/src/hydration/index.ts +26 -6
  24. package/src/i18n/index.ts +11 -2
  25. package/src/image/index.tsx +7 -4
  26. package/src/intercept-outlet/index.tsx +6 -2
  27. package/src/lazy/index.tsx +50 -50
  28. package/src/link/index.tsx +97 -58
  29. package/src/loader-pipeline/index.ts +119 -38
  30. package/src/locale/cookie.ts +15 -0
  31. package/src/locale/index.tsx +24 -35
  32. package/src/locale.ts +1 -0
  33. package/src/middleware/builtins/api-proxy.ts +58 -4
  34. package/src/middleware/builtins/cdn-proxy.ts +24 -3
  35. package/src/middleware/builtins/i18n.ts +23 -25
  36. package/src/middleware/builtins/keepalive.ts +2 -3
  37. package/src/mount/index.ts +8 -1
  38. package/src/navigation/index.ts +575 -129
  39. package/src/navigation/types.ts +11 -1
  40. package/src/navigation-progress/index.tsx +17 -16
  41. package/src/ndjson-client/index.ts +42 -17
  42. package/src/ndjson-server/index.ts +78 -29
  43. package/src/outlet/index.tsx +192 -97
  44. package/src/outlet/types.ts +9 -1
  45. package/src/plugins/dev-cdn-cache.ts +18 -5
  46. package/src/plugins/dev-dashboard/client.tsx +130 -118
  47. package/src/plugins/dev-prerender.ts +6 -1
  48. package/src/plugins/dev-server.ts +12 -3
  49. package/src/plugins/image-plugin.ts +18 -4
  50. package/src/plugins/index.ts +10 -7
  51. package/src/plugins/prerender-plugin.ts +3 -3
  52. package/src/plugins/server-fn.ts +15 -20
  53. package/src/plugins/solid-jsx.d.ts +3 -3
  54. package/src/plugins/sx-ast/atomic.ts +5 -5
  55. package/src/plugins/sx-ast/index.ts +9 -0
  56. package/src/plugins/sx.d.ts +4 -5
  57. package/src/plugins/virtual.ts +11 -6
  58. package/src/prerender/index.ts +68 -32
  59. package/src/protocol.ts +65 -0
  60. package/src/query-client/index.tsx +73 -14
  61. package/src/rewrite/index.ts +10 -4
  62. package/src/route-builder/create-layout.ts +1 -1
  63. package/src/route-builder/create-root-layout.ts +1 -1
  64. package/src/route-builder/types.ts +5 -1
  65. package/src/router-primitives/types.ts +2 -0
  66. package/src/security/index.ts +41 -5
  67. package/src/server/index.ts +1 -2
  68. package/src/server-fn/get-input.ts +90 -0
  69. package/src/server-fn/http-error.ts +13 -0
  70. package/src/server-fn/index.ts +94 -70
  71. package/src/server-fn-query.ts +9 -17
  72. package/src/server-handler/etag.ts +4 -2
  73. package/src/server-handler/headers.ts +1 -2
  74. package/src/server-handler/index.ts +166 -58
  75. package/src/server-handler/types.ts +2 -0
  76. package/src/service-worker/template.ts +10 -42
  77. package/src/ssr/head-prefix.ts +2 -1
  78. package/src/ssr/hoist-head-markers.ts +25 -0
  79. package/src/ssr/index.tsx +109 -92
  80. package/src/state-parser/index.ts +38 -26
  81. package/src/styles/index.ts +89 -60
  82. package/src/testing/index.ts +8 -3
  83. package/src/theme/index.tsx +35 -32
  84. package/src/tracing/timing.ts +6 -1
  85. package/src/url/index.ts +9 -0
@@ -46,28 +46,60 @@ export class UnauthorizedError extends Error {
46
46
  }
47
47
 
48
48
  const SAFE_PROTOCOLS = new Set(["http:", "https:"]);
49
+ const SENTINEL_ORIGIN = "https://flare.invalid";
50
+ const HAS_SCHEME = /^[a-zA-Z][a-zA-Z+\-.]*:/;
49
51
 
50
52
  /**
51
53
  * Allowlist-based redirect URL validation using the URL constructor.
52
- * Lets the platform's URL parser handle all encoding/unicode normalization
53
- * instead of a regex blocklist that can be bypassed.
54
+ * Lets the platform's URL parser handle encoding, backslash normalization,
55
+ * and unicode instead of a regex blocklist that can be bypassed.
56
+ *
57
+ * Protocol-relative (`//host`) is not a same-origin path — `startsWith("/")`
58
+ * would treat it as one. Internal `to` must stay on the sentinel origin.
54
59
  */
55
- function validateExternalRedirectUrl(href: string): string {
56
- /* Relative paths are safe — no protocol to exploit */
57
- if (href.startsWith("/") || href.startsWith(".")) return href;
60
+ function validateRedirectUrl(raw: string, mode: "internal" | "external"): string {
61
+ if (/[\0\r\n]/.test(raw)) {
62
+ throw new Error(`Unsafe redirect URL: ${raw}`);
63
+ }
58
64
 
59
65
  let parsed: URL;
60
66
  try {
61
- parsed = new URL(href, "http://localhost");
67
+ parsed = new URL(raw, SENTINEL_ORIGIN);
62
68
  } catch {
63
- throw new Error(`Unsafe redirect URL: ${href}`);
69
+ throw new Error(`Unsafe redirect URL: ${raw}`);
64
70
  }
65
71
 
66
72
  if (!SAFE_PROTOCOLS.has(parsed.protocol)) {
67
- throw new Error(`Unsafe redirect URL: ${href}`);
73
+ throw new Error(`Unsafe redirect URL: ${raw}`);
74
+ }
75
+
76
+ const sameOrigin = parsed.origin === SENTINEL_ORIGIN;
77
+ if (mode === "internal") {
78
+ let decodedPath = parsed.pathname;
79
+ try {
80
+ decodedPath = decodeURIComponent(parsed.pathname);
81
+ } catch {
82
+ /* keep pathname */
83
+ }
84
+ if (
85
+ !sameOrigin ||
86
+ !raw.startsWith("/") ||
87
+ raw.startsWith("//") ||
88
+ raw.includes("\\") ||
89
+ parsed.pathname.startsWith("//") ||
90
+ decodedPath.includes("\\") ||
91
+ decodedPath.startsWith("//")
92
+ ) {
93
+ throw new Error(`Unsafe redirect URL: ${raw}`);
94
+ }
95
+ return `${parsed.pathname}${parsed.search}${parsed.hash}`;
96
+ }
97
+
98
+ if (!sameOrigin && !HAS_SCHEME.test(raw.trimStart())) {
99
+ throw new Error(`Unsafe redirect URL: ${raw}`);
68
100
  }
69
101
 
70
- return href;
102
+ return raw;
71
103
  }
72
104
 
73
105
  export class RedirectResponse extends Error {
@@ -80,10 +112,10 @@ export class RedirectResponse extends Error {
80
112
  constructor(options: RawRedirectOptions) {
81
113
  super("Redirect");
82
114
  if ("href" in options) {
83
- this.url = validateExternalRedirectUrl(options.href);
115
+ this.url = validateRedirectUrl(options.href, "external");
84
116
  this.external = true;
85
117
  } else {
86
- this.url = options.to;
118
+ this.url = validateRedirectUrl(options.to, "internal");
87
119
  this.external = false;
88
120
  }
89
121
  this.status = options.status ?? 303;
@@ -136,6 +168,8 @@ export function redirect(options: {
136
168
  if (options.to === undefined) {
137
169
  throw new Error("redirect() requires either 'to' or 'href'");
138
170
  }
171
+ /* Validate the raw path before buildUrl collapses leading slashes (`//host` → `/host`) */
172
+ validateRedirectUrl(options.to, "internal");
139
173
  const url = buildUrl({ params: options.params, search: options.search, to: options.to });
140
174
  throw new RedirectResponse({ replace: options.replace, status: options.status, to: url });
141
175
  }
@@ -1,4 +1,5 @@
1
- import { For, type JSX } from "solid-js";
1
+ import { For } from "solid-js";
2
+ import type { JSX } from "@solidjs/web";
2
3
  import { useSSRContext } from "../components/ssr-context.tsx";
3
4
  import type { Font } from "./types.ts";
4
5
 
@@ -1,7 +1,9 @@
1
- import { createMemo, createSignal, For, type JSX, Show, splitProps } from "solid-js";
2
- import { Dynamic } from "solid-js/web";
1
+ import { createMemo, createSignal, For, omit, Show } from "solid-js";
2
+ import type { JSX } from "@solidjs/web";
3
+ import { Dynamic } from "@solidjs/web";
3
4
  import type { FlattenedError } from "../errors/index.ts";
4
5
  import { ServerFnValidationError } from "../errors/index.ts";
6
+ import { FORM_FN_FIELD, serverFnPath } from "../protocol.ts";
5
7
  import type { ServerFn } from "../server-fn/index.ts";
6
8
 
7
9
  /* Duplicated here to avoid importing server-context (uses node:async_hooks)
@@ -51,16 +53,20 @@ interface FormOwnProps<TInput, TOutput> {
51
53
  export type FormProps<TInput, TOutput> = FormOwnProps<TInput, TOutput> &
52
54
  Omit<JSX.FormHTMLAttributes<HTMLFormElement>, "action" | "children" | "enctype" | "method">;
53
55
 
56
+ /** Seed `form.error()` after a no-JS PE POST with form-level validation errors. */
57
+ export function seedFormErrorFromSsr(ssrCtx: FormActionContext | undefined): Error | null {
58
+ if (!ssrCtx?.formErrors.length) return null;
59
+ return new ServerFnValidationError({
60
+ fieldErrors: ssrCtx.fieldErrors,
61
+ formErrors: ssrCtx.formErrors,
62
+ });
63
+ }
64
+
54
65
  /* ── Form component ────────────────────────────────────────────────── */
55
66
 
56
67
  export function Form<TInput, TOutput>(props: FormProps<TInput, TOutput>): JSX.Element {
57
- const [local, rest] = splitProps(props as FormOwnProps<TInput, TOutput> & Record<string, unknown>, [
58
- "action",
59
- "children",
60
- "enctype",
61
- "onError",
62
- "onSuccess",
63
- ]);
68
+ const local = props as FormOwnProps<TInput, TOutput> & Record<string, unknown>;
69
+ const rest = omit(local, "action", "children", "enctype", "onError", "onSuccess");
64
70
 
65
71
  const reg = createMemo(() => local.action._registration);
66
72
  const fnId = createMemo(() => reg()?.id ?? "");
@@ -70,7 +76,7 @@ export function Form<TInput, TOutput>(props: FormProps<TInput, TOutput>): JSX.El
70
76
  const ssrCtx = typeof window === "undefined" && _getFormActionContext ? _getFormActionContext(fnId()) : undefined;
71
77
 
72
78
  const [pending, setPending] = createSignal(false);
73
- const [error, setError] = createSignal<Error | null>(null);
79
+ const [error, setError] = createSignal<Error | null>(seedFormErrorFromSsr(ssrCtx));
74
80
  const [fieldErrors, setFieldErrors] = createSignal<Record<string, string[]>>(ssrCtx?.fieldErrors ?? {});
75
81
  const [result, setResult] = createSignal<TOutput | undefined>(undefined);
76
82
 
@@ -116,7 +122,7 @@ export function Form<TInput, TOutput>(props: FormProps<TInput, TOutput>): JSX.El
116
122
  setFieldErrors({});
117
123
 
118
124
  try {
119
- const url = `/_fn/${id}/${name}`;
125
+ const url = serverFnPath(id, name);
120
126
  const res = await fetch(url, {
121
127
  body: formData,
122
128
  method: "POST",
@@ -165,7 +171,7 @@ export function Form<TInput, TOutput>(props: FormProps<TInput, TOutput>): JSX.El
165
171
  method="post"
166
172
  onSubmit={handleSubmit}
167
173
  >
168
- <input name="__flare_fn" type="hidden" value={fnId()} />
174
+ <input name={FORM_FN_FIELD} type="hidden" value={fnId()} />
169
175
  {local.children(formCtx)}
170
176
  </form>
171
177
  ) as JSX.Element;
@@ -11,6 +11,7 @@ export type PrefetchStrategy = false | "intent" | "render" | "viewport";
11
11
  export type ExtractedStaticDeferMode = "resolve" | "stream";
12
12
 
13
13
  export interface ExtractedCacheConfig {
14
+ cdnTags?: string[];
14
15
  client?: {
15
16
  cacheDeferred?: boolean;
16
17
  gcTime?: number;
@@ -37,6 +38,7 @@ export interface RouteDefinition {
37
38
  exportName: string;
38
39
  filePath: string;
39
40
  cache: ExtractedCacheConfig;
41
+ hasAuthorize?: boolean;
40
42
  hasInput: boolean;
41
43
  intercept?: ExtractedInterceptConfig;
42
44
  responseRoute: boolean;
@@ -76,6 +78,23 @@ function writeGenFile(filePath: string, body: string, generator: string): void {
76
78
  writeFileSync(filePath, fullContent, "utf-8");
77
79
  }
78
80
 
81
+ /**
82
+ * Recursive source listing, normalized to posix separators and sorted.
83
+ *
84
+ * `readdirSync` order is filesystem- and runtime-dependent — node, bun, and
85
+ * deno disagree — and generated files are checked in, so an unsorted scan
86
+ * makes `routes.gen.ts` churn based on who ran the build. Sorting by code
87
+ * unit (not `localeCompare`, which follows ICU locale data) pins the order.
88
+ * Returns [] when the directory is missing.
89
+ */
90
+ function listSourceEntries(srcPath: string): string[] {
91
+ try {
92
+ return (readdirSync(srcPath, { recursive: true }) as string[]).map((entry) => entry.replace(/\\/g, "/")).sort();
93
+ } catch {
94
+ return [];
95
+ }
96
+ }
97
+
79
98
  const BUILDER_TO_TYPE: Record<string, RouteDefinition["type"]> = {
80
99
  createLayout: "layout",
81
100
  createPage: "page",
@@ -206,6 +225,41 @@ function extractNestedBlock(body: string, key: string): string | null {
206
225
  return null;
207
226
  }
208
227
 
228
+ function extractStringArray(text: string, key: string): string[] | undefined {
229
+ const re = new RegExp(`${key}\\s*:\\s*\\[`);
230
+ const match = re.exec(text);
231
+ if (!match) return undefined;
232
+
233
+ const start = (match.index ?? 0) + match[0].length;
234
+ let depth = 1;
235
+ let end = start;
236
+ for (let i = start; i < text.length; i++) {
237
+ const skipped = skipStringOrComment(text, i);
238
+ if (skipped !== i) {
239
+ i = skipped;
240
+ continue;
241
+ }
242
+ if (text[i] === "[") depth++;
243
+ else if (text[i] === "]") {
244
+ depth--;
245
+ if (depth === 0) {
246
+ end = i;
247
+ break;
248
+ }
249
+ }
250
+ }
251
+ const inner = text.slice(start, end);
252
+ const items: string[] = [];
253
+ const itemRe = /["'`]([^"'`]*)["'`]/g;
254
+ let itemMatch: RegExpExecArray | null = itemRe.exec(inner);
255
+ while (itemMatch) {
256
+ const value = itemMatch[1];
257
+ if (value) items.push(value);
258
+ itemMatch = itemRe.exec(inner);
259
+ }
260
+ return items;
261
+ }
262
+
209
263
  function extractNumeric(text: string, key: string, unit?: "ms" | "s"): number | undefined {
210
264
  /* try duration string first: key: "5m" */
211
265
  const dm = DURATION_STR_RE(key).exec(text);
@@ -295,6 +349,13 @@ export function extractCacheFromChain(chainText: string): ExtractedCacheConfig {
295
349
  config.isr = true;
296
350
  }
297
351
 
352
+ /* cdn: { tags: ["a", "b"] } — static string arrays only; function tags are computed at render */
353
+ const cdnBlock = extractNestedBlock(body, "cdn");
354
+ if (cdnBlock) {
355
+ const tags = extractStringArray(cdnBlock, "tags");
356
+ if (tags && tags.length > 0) config.cdnTags = tags;
357
+ }
358
+
298
359
  return config;
299
360
  }
300
361
 
@@ -436,6 +497,7 @@ export function extractRouteDefinitions(src: string, filePath: string): RouteDef
436
497
  const hasAuthenticateOptional = /\.authenticateOptional\s*\(/.test(chainText);
437
498
  const hasAuthenticate = /\.authenticate\s*\(/.test(chainText);
438
499
  const authenticateMode: AuthenticateMode = hasAuthenticateOptional ? "optional" : !!hasAuthenticate;
500
+ const hasAuthorize = /\.authorize\s*\(/.test(chainText);
439
501
  const hasInput = /\.input\s*(?:<[^>]*>)?\s*\(/.test(chainText);
440
502
  const responseRoute = /\.response\s*\(/.test(chainText);
441
503
  const cache = extractCacheFromChain(chainText);
@@ -446,6 +508,7 @@ export function extractRouteDefinitions(src: string, filePath: string): RouteDef
446
508
  cache,
447
509
  exportName,
448
510
  filePath,
511
+ hasAuthorize,
449
512
  hasInput,
450
513
  intercept,
451
514
  responseRoute,
@@ -581,6 +644,11 @@ function formatRouteMeta(def: RouteDefinition): string {
581
644
  const parts: string[] = [];
582
645
  if (def.authenticateMode === true) parts.push("authenticate: true");
583
646
  else if (def.authenticateMode === "optional") parts.push(`authenticate: "optional"`);
647
+ if (def.hasAuthorize) parts.push("authorize: true");
648
+ if (def.cache.cdnTags && def.cache.cdnTags.length > 0) {
649
+ const tagLit = def.cache.cdnTags.map((t) => `"${escapeCodegenStr(t)}"`).join(", ");
650
+ parts.push(`tags: [${tagLit}]`);
651
+ }
584
652
 
585
653
  if (def.cache.client) {
586
654
  const cp: string[] = [];
@@ -1106,17 +1174,7 @@ export function scanSourceFiles(options: ScanOptions): RouteDefinition[] {
1106
1174
  const prefix = options.ignorePrefix ?? "_";
1107
1175
  const defs: RouteDefinition[] = [];
1108
1176
 
1109
- let entries: string[];
1110
- try {
1111
- entries = readdirSync(srcPath, { recursive: true }) as string[];
1112
- } catch {
1113
- return defs;
1114
- }
1115
-
1116
- for (const entry of entries) {
1117
- /* Normalize to posix separators */
1118
- const rel = entry.replace(/\\/g, "/");
1119
-
1177
+ for (const rel of listSourceEntries(srcPath)) {
1120
1178
  /* Skip non-ts files */
1121
1179
  if (!rel.endsWith(".ts") && !rel.endsWith(".tsx")) continue;
1122
1180
 
@@ -1254,15 +1312,7 @@ export function inspectFsCodegenLayout(options: ScanOptions): FsLayoutIssue[] {
1254
1312
  const prefix = options.ignorePrefix ?? "_";
1255
1313
  const issues: FsLayoutIssue[] = [];
1256
1314
 
1257
- let entries: string[];
1258
- try {
1259
- entries = readdirSync(srcPath, { recursive: true }) as string[];
1260
- } catch {
1261
- return issues;
1262
- }
1263
-
1264
- for (const entry of entries) {
1265
- const rel = entry.replace(/\\/g, "/");
1315
+ for (const rel of listSourceEntries(srcPath)) {
1266
1316
  if (!rel.endsWith(".ts") && !rel.endsWith(".tsx") && !rel.endsWith(".js") && !rel.endsWith(".jsx")) continue;
1267
1317
  if (rel.includes("_gen/") || rel.includes("node_modules")) continue;
1268
1318
  if (rel.endsWith(".gen.ts") || rel.endsWith(".gen.tsx")) continue;
@@ -1421,16 +1471,7 @@ export function scanSourceFilesFsCodegen(options: ScanOptions): RouteDefinition[
1421
1471
  const routesDir = "routes";
1422
1472
  const defs: RouteDefinition[] = [];
1423
1473
 
1424
- let entries: string[];
1425
- try {
1426
- entries = readdirSync(srcPath, { recursive: true }) as string[];
1427
- } catch {
1428
- return defs;
1429
- }
1430
-
1431
- for (const entry of entries) {
1432
- const rel = entry.replace(/\\/g, "/");
1433
-
1474
+ for (const rel of listSourceEntries(srcPath)) {
1434
1475
  /* Only process files under routes/ */
1435
1476
  if (!rel.startsWith(`${routesDir}/`)) continue;
1436
1477
 
@@ -1464,6 +1505,7 @@ export function scanSourceFilesFsCodegen(options: ScanOptions): RouteDefinition[
1464
1505
  const hasAuthenticateOptional = /\.authenticateOptional\s*\(/.test(content);
1465
1506
  const hasAuthenticate = /\.authenticate\s*\(/.test(content);
1466
1507
  const authenticateMode: AuthenticateMode = hasAuthenticateOptional ? "optional" : !!hasAuthenticate;
1508
+ const hasAuthorize = /\.authorize\s*\(/.test(content);
1467
1509
  const hasInput = /\.input\s*(?:<[^>]*>)?\s*\(/.test(content);
1468
1510
  const responseRoute = /\.response\s*\(/.test(content);
1469
1511
  const cache = extractCacheFromChain(content);
@@ -1474,6 +1516,7 @@ export function scanSourceFilesFsCodegen(options: ScanOptions): RouteDefinition[
1474
1516
  cache,
1475
1517
  exportName: "route",
1476
1518
  filePath: posix.join(options.srcDir ?? "src", rel),
1519
+ hasAuthorize,
1477
1520
  hasInput,
1478
1521
  intercept,
1479
1522
  responseRoute,
@@ -17,6 +17,7 @@ const managedHreflangTags = new Set<string>();
17
17
  /* CSS lifecycle: per-route elements + refcounted dedup for shared stylesheets */
18
18
  const cssElementsByRoute = new Map<string, HTMLElement[]>();
19
19
  const cssRefCount = new Map<string, { element: HTMLElement; refCount: number }>();
20
+ const styleRefCount = new Map<string, { element: HTMLElement; refCount: number }>();
20
21
 
21
22
  export function clearRouteTracking(): void {
22
23
  baseTitle = "";
@@ -26,6 +27,7 @@ export function clearRouteTracking(): void {
26
27
  managedHreflangTags.clear();
27
28
  cssElementsByRoute.clear();
28
29
  cssRefCount.clear();
30
+ styleRefCount.clear();
29
31
  }
30
32
 
31
33
  export function initRouteHierarchy(matchIds: string[], rootTitle?: string): void {
@@ -561,15 +563,70 @@ function addCssLinkForRoute(matchId: string, href: string): void {
561
563
  cssElementsByRoute.set(matchId, list);
562
564
  }
563
565
 
566
+ function stampStyleNonce(el: HTMLElement): void {
567
+ if (el.getAttribute("nonce") || el.nonce) return;
568
+ const meta = document.querySelector?.('meta[name="csp-nonce"]');
569
+ const tagged = document.querySelector?.("script[nonce], style[nonce]") as HTMLElement | null;
570
+ const nonce = meta?.getAttribute("content") || tagged?.nonce || tagged?.getAttribute("nonce") || "";
571
+ if (!nonce) return;
572
+ el.setAttribute("nonce", nonce);
573
+ el.nonce = nonce;
574
+ }
575
+
576
+ function isInfraStyle(el: HTMLElement): boolean {
577
+ const id = el.getAttribute("id");
578
+ return id === "flare-runtime" || id === "flare-critical";
579
+ }
580
+
581
+ function isStyleTracked(el: HTMLElement): boolean {
582
+ for (const ref of styleRefCount.values()) {
583
+ if (ref.element === el) return true;
584
+ }
585
+ return false;
586
+ }
587
+
588
+ function findAdoptableStyle(children: string): HTMLElement | null {
589
+ const nodes = document.head?.querySelectorAll("style");
590
+ if (!nodes) return null;
591
+ for (const node of nodes) {
592
+ const el = node as HTMLElement;
593
+ if (isInfraStyle(el) || isStyleTracked(el)) continue;
594
+ if (el.textContent === children) return el;
595
+ }
596
+ return null;
597
+ }
598
+
599
+ function trackStyleForRoute(matchId: string, el: HTMLElement): void {
600
+ const list = cssElementsByRoute.get(matchId) ?? [];
601
+ list.push(el);
602
+ cssElementsByRoute.set(matchId, list);
603
+ }
604
+
564
605
  function addCssStyleForRoute(matchId: string, children: string): void {
565
606
  if (typeof document === "undefined") return;
607
+ const ref = styleRefCount.get(children);
608
+ if (ref) {
609
+ ref.refCount++;
610
+ trackStyleForRoute(matchId, ref.element);
611
+ return;
612
+ }
613
+
614
+ const existing = findAdoptableStyle(children);
615
+ if (existing) {
616
+ existing.setAttribute("data-flare-route", matchId);
617
+ stampStyleNonce(existing);
618
+ styleRefCount.set(children, { element: existing, refCount: 1 });
619
+ trackStyleForRoute(matchId, existing);
620
+ return;
621
+ }
622
+
566
623
  const el = document.createElement("style");
624
+ stampStyleNonce(el);
567
625
  el.setAttribute("data-flare-route", matchId);
568
626
  el.textContent = children;
569
627
  document.head.appendChild(el);
570
- const list = cssElementsByRoute.get(matchId) ?? [];
571
- list.push(el);
572
- cssElementsByRoute.set(matchId, list);
628
+ styleRefCount.set(children, { element: el, refCount: 1 });
629
+ trackStyleForRoute(matchId, el);
573
630
  }
574
631
 
575
632
  function applyCssForRoute(matchId: string, config: HeadConfig): void {
@@ -601,20 +658,34 @@ function applyCssForRoute(matchId: string, config: HeadConfig): void {
601
658
 
602
659
  /* THEN decrement old refcounts — shared elements stay because new refs were added above */
603
660
  for (const el of oldElements) {
604
- const href = el.getAttribute("href");
605
- if (href && el.getAttribute("rel") === "stylesheet") {
606
- const ref = cssRefCount.get(href);
607
- if (ref) {
608
- ref.refCount--;
609
- if (ref.refCount <= 0) {
610
- ref.element.remove();
611
- cssRefCount.delete(href);
612
- }
613
- continue;
661
+ releaseCssElement(el);
662
+ }
663
+ }
664
+
665
+ function releaseCssElement(el: HTMLElement): void {
666
+ const href = el.getAttribute("href");
667
+ if (href && el.getAttribute("rel") === "stylesheet") {
668
+ const ref = cssRefCount.get(href);
669
+ if (ref) {
670
+ ref.refCount--;
671
+ if (ref.refCount <= 0) {
672
+ ref.element.remove();
673
+ cssRefCount.delete(href);
614
674
  }
675
+ return;
615
676
  }
616
- el.remove();
617
677
  }
678
+ const content = el.textContent ?? "";
679
+ const styleRef = styleRefCount.get(content);
680
+ if (styleRef && styleRef.element === el) {
681
+ styleRef.refCount--;
682
+ if (styleRef.refCount <= 0) {
683
+ styleRef.element.remove();
684
+ styleRefCount.delete(content);
685
+ }
686
+ return;
687
+ }
688
+ el.remove();
618
689
  }
619
690
 
620
691
  function removeCssForRoute(routeId: string): void {
@@ -622,21 +693,7 @@ function removeCssForRoute(routeId: string): void {
622
693
  if (!elements) return;
623
694
 
624
695
  for (const el of elements) {
625
- const href = el.getAttribute("href");
626
- /* Refcounted stylesheet links */
627
- if (href && el.getAttribute("rel") === "stylesheet") {
628
- const ref = cssRefCount.get(href);
629
- if (ref) {
630
- ref.refCount--;
631
- if (ref.refCount <= 0) {
632
- ref.element.remove();
633
- cssRefCount.delete(href);
634
- }
635
- continue;
636
- }
637
- }
638
- /* <style> elements or untracked links — just remove */
639
- el.remove();
696
+ releaseCssElement(el);
640
697
  }
641
698
  cssElementsByRoute.delete(routeId);
642
699
  }
@@ -150,6 +150,12 @@ export function createScrollStore(maxSize = 200, ttl = DEFAULT_SCROLL_TTL): Scro
150
150
 
151
151
  return {
152
152
  get(key: string): ScrollPosition | null {
153
+ const ts = timestamps.get(key);
154
+ if (ts !== undefined && Date.now() - ts > ttl) {
155
+ store.delete(key);
156
+ timestamps.delete(key);
157
+ return null;
158
+ }
153
159
  return store.get(key) ?? null;
154
160
  },
155
161