@stacksjs/stx 0.2.156 → 0.2.158

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.
package/dist/render.d.ts CHANGED
@@ -1,18 +1,4 @@
1
1
  import type { StxOptions } from './types';
2
- /**
3
- * Render a .stx template file to an HTML string.
4
- *
5
- * This is the primary rendering API. It:
6
- * 1. Reads the template file
7
- * 2. Extracts and executes server-side scripts
8
- * 3. Processes all stx directives (conditionals, loops, expressions, etc.)
9
- * 4. Injects client-side scripts
10
- * 5. Returns the final HTML string
11
- *
12
- * @param filePath - Path to the .stx template file
13
- * @param renderOptions - Additional rendering options
14
- * @returns The rendered HTML string
15
- */
16
2
  export declare function renderTemplate(filePath: string, renderOptions?: RenderOptions): Promise<string>;
17
3
  /**
18
4
  * Render an inline stx template string to HTML.
package/dist/render.js CHANGED
@@ -66,6 +66,10 @@ ${html}
66
66
  </body>
67
67
  </html>`;
68
68
  }
69
+ function recordOn(target, key, value) {
70
+ if (target && typeof target === "object")
71
+ target[key] = value;
72
+ }
69
73
  export async function renderTemplate(filePath, renderOptions = {}) {
70
74
  const resolvedPath = resolve(filePath), file = Bun.file(resolvedPath);
71
75
  if (!await file.exists())
@@ -112,6 +116,18 @@ export async function renderTemplateString(content, filePath, renderOptions = {}
112
116
  __stx_sfc_mode: !0,
113
117
  __stx_inject_css: renderOptions.injectCSS === !0,
114
118
  __stxServeContext: void 0,
119
+ setResponseStatus: (status) => {
120
+ if (Number.isInteger(status) && status >= 100 && status <= 599)
121
+ recordOn(renderOptions.context, "__stxResponseStatus", status);
122
+ },
123
+ setResponseHeader: (name, value) => {
124
+ if (!name)
125
+ return;
126
+ const headers = renderOptions.context?.__stxResponseHeaders || {};
127
+ headers[String(name)] = String(value);
128
+ recordOn(renderOptions.context, "__stxResponseHeaders", headers);
129
+ },
130
+ definePageMeta: () => {},
115
131
  ...renderOptions.context || {}
116
132
  };
117
133
  for (const scriptContent of serverScripts)
@@ -1,5 +1,6 @@
1
1
  import { getOwnedRouteMatchers } from "./owned-routes.js";
2
2
  import { toScriptJson } from "./script-json.js";
3
+ import { findBodyOpenTag, replaceBodyOpenTag } from "./find-body-tag.js";
3
4
  export function injectBrowserRuntime(template) {
4
5
  if (template.includes("window.StacksBrowser") || template.includes("StacksBrowser ="))
5
6
  return template;
@@ -56,8 +57,8 @@ import '@stacksjs/browser'
56
57
  return template.replace("</head>", `${browserScript}
57
58
  </head>`);
58
59
  }
59
- if (template.includes("<body"))
60
- return template.replace(/<body([^>]*)>/, `<body$1>
60
+ if (findBodyOpenTag(template))
61
+ return replaceBodyOpenTag(template, (tag) => `${tag}
61
62
  ${browserScript}`);
62
63
  return browserScript + `
63
64
  ` + template;
@@ -21,7 +21,11 @@ export function injectThemeBootstrap(html, site) {
21
21
  ${metas}`);
22
22
  }
23
23
  if (!colorModeOwnsTheme && !html.includes("__stxThemeGuard") && /<head\b[^>]*>/i.test(html)) {
24
- const guard = `<script data-stx-theme-guard="1">${FOUC_SCRIPT(defaultTheme, storageKey, lightColor, darkColor)};window.__stxThemeGuard=1;</script>`;
24
+ const guard = `<script data-stx-theme-guard="1">${`window.__STX_COLOR_MODE__=window.__STX_COLOR_MODE__||${JSON.stringify({
25
+ storageKey,
26
+ initialMode: defaultTheme,
27
+ darkClass: "dark"
28
+ })};`}${FOUC_SCRIPT(defaultTheme, storageKey, lightColor, darkColor)};window.__stxThemeGuard=1;</script>`;
25
29
  html = html.replace(/<head\b([^>]*)>/i, `<head$1>
26
30
  ${guard}`);
27
31
  }