weifuwu 0.17.8 → 0.17.9

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/README.md CHANGED
@@ -1155,19 +1155,19 @@ In-memory page view tracking with a built-in dashboard. Zero extra dependencies.
1155
1155
  ```ts
1156
1156
  import { analytics } from 'weifuwu'
1157
1157
 
1158
- app.use(analytics()) // mounts middleware + /analytics + /__analytics/data
1158
+ app.use(analytics()) // mounts middleware + /__analytics + /__analytics/data
1159
1159
  ```
1160
1160
 
1161
1161
  | Endpoint | Description |
1162
1162
  |----------|-------------|
1163
- | `GET /analytics` | Dashboard — PV trend, top pages, referrers, device breakdown |
1163
+ | `GET /__analytics` | Dashboard — PV trend, top pages, referrers, device breakdown |
1164
1164
  | `GET /__analytics/data?days=7` | Raw JSON data for custom dashboards |
1165
1165
 
1166
- Excluded paths (not recorded): `/__analytics/*`, `__wfw/*`, `/static/*`, `/analytics`.
1166
+ Excluded paths (not recorded): `/__analytics/*`, `/__wfw/*`, `/static/*`.
1167
1167
 
1168
1168
  ### Dashboard
1169
1169
 
1170
- The built-in `/analytics` page renders a server-generated HTML dashboard with:
1170
+ The built-in `/__analytics` page renders a server-generated HTML dashboard with:
1171
1171
 
1172
1172
  - **Summary cards** — total PV, unique pages, mobile/desktop ratio
1173
1173
  - **Bar chart** — daily page views for the selected period
package/dist/index.js CHANGED
@@ -579,7 +579,8 @@ import chokidar from "chokidar";
579
579
 
580
580
  // tsx-context.ts
581
581
  import { useSyncExternalStore, createContext } from "react";
582
- var _ctx = { params: {}, query: {} };
582
+ var fallbackT = (key, _params, fallback) => fallback ?? key;
583
+ var _ctx = { params: {}, query: {}, parsed: {}, prefs: {}, env: {}, t: fallbackT, user: {} };
583
584
  var _listeners = /* @__PURE__ */ new Set();
584
585
  function setCtx(value) {
585
586
  _ctx = { ..._ctx, ...value };
@@ -587,7 +588,7 @@ function setCtx(value) {
587
588
  }
588
589
  function _buildT() {
589
590
  const messages2 = typeof window !== "undefined" ? window.__LOCALE_DATA__ : globalThis.__LOCALE_DATA__;
590
- if (!messages2) return void 0;
591
+ if (!messages2) return fallbackT;
591
592
  return (key, params, fallback) => {
592
593
  const msg = key.split(".").reduce((o, k) => o?.[k], messages2);
593
594
  if (msg === void 0 || msg === null) return fallback ?? key;
@@ -610,11 +611,9 @@ function useCtx() {
610
611
  );
611
612
  const data = typeof window !== "undefined" ? window.__WEIFUWU_CTX : null;
612
613
  const t = data?.t ?? _ctx.t ?? _buildT();
613
- const result = { ..._ctx, ...data };
614
- if (t) result.t = t;
615
- return result;
614
+ return { ..._ctx, ...data, t };
616
615
  }
617
- var TsxContext = createContext({ params: {}, query: {} });
616
+ var TsxContext = createContext({ params: {}, query: {}, parsed: {}, prefs: {}, env: {}, t: fallbackT, user: {} });
618
617
 
619
618
  // tsx-instance.ts
620
619
  var liveReloadClients = /* @__PURE__ */ new Set();
@@ -904,8 +903,6 @@ var TsxInstance = class {
904
903
  user: ctx.user,
905
904
  parsed: ctx.parsed,
906
905
  prefs: ctx.prefs,
907
- locale: ctx.locale,
908
- theme: ctx.theme,
909
906
  t: ctx.t,
910
907
  env: ctx.env
911
908
  });
@@ -1075,8 +1072,6 @@ ${src}`;
1075
1072
  user: ctx.user,
1076
1073
  parsed: ctx.parsed,
1077
1074
  prefs: ctx.prefs,
1078
- locale: ctx.locale,
1079
- theme: ctx.theme,
1080
1075
  t: ctx.t,
1081
1076
  env: ctx.env
1082
1077
  });
@@ -1345,7 +1340,7 @@ function streamResponse(reactStream, opts) {
1345
1340
  function buildHeadPayload(opts) {
1346
1341
  const { ctx, base, compiledTailwindCss } = opts;
1347
1342
  let result = "";
1348
- if (ctx.theme) {
1343
+ if (ctx.prefs?.theme) {
1349
1344
  result += `<script>!function(){var t=(document.cookie.match(/(?:^|;\\s*)theme=([^;]+)/)||[])[1]||'system';if(t==='system'){t=window.matchMedia('(prefers-color-scheme:dark)').matches?'dark':'light'}document.documentElement.setAttribute('data-theme',t)}()</script>
1350
1345
  `;
1351
1346
  }
@@ -1362,11 +1357,9 @@ function buildHeadPayload(opts) {
1362
1357
  params: ctx.params,
1363
1358
  query: ctx.query,
1364
1359
  user: ctx.user,
1365
- parsed: ctx.parsed
1360
+ parsed: ctx.parsed,
1361
+ prefs: ctx.prefs
1366
1362
  };
1367
- if (ctx.prefs) ctxData.prefs = ctx.prefs;
1368
- if (ctx.locale) ctxData.locale = ctx.locale;
1369
- if (ctx.theme) ctxData.theme = ctx.theme;
1370
1363
  const publicEnv = {};
1371
1364
  for (const key of Object.keys(process.env)) {
1372
1365
  if (key.startsWith("WEIFUWU_PUBLIC_")) {
@@ -6799,7 +6792,7 @@ function health(options) {
6799
6792
  }
6800
6793
 
6801
6794
  // analytics.ts
6802
- var DEFAULT_EXCLUDED = ["/__analytics", "/__wfw", "/static", "/analytics"];
6795
+ var DEFAULT_EXCLUDED = ["/__analytics", "/__wfw", "/static"];
6803
6796
  var MemStore = class {
6804
6797
  days = /* @__PURE__ */ new Map();
6805
6798
  pages = /* @__PURE__ */ new Map();
@@ -6996,7 +6989,7 @@ function analytics(options) {
6996
6989
  const router = () => {
6997
6990
  const r = new Router();
6998
6991
  r.get("/__analytics/data", handler);
6999
- r.get("/analytics", handler);
6992
+ r.get("/__analytics", handler);
7000
6993
  return r;
7001
6994
  };
7002
6995
  const migrate = async () => {
@@ -7091,8 +7084,6 @@ function preferences(options) {
7091
7084
  const locale = detectLocale(req, localeOpts);
7092
7085
  const theme = detectTheme(req, themeOpts);
7093
7086
  ctx.prefs = { locale, theme };
7094
- ctx.locale = locale;
7095
- ctx.theme = theme;
7096
7087
  if (dir) {
7097
7088
  const msgs = await load(locale);
7098
7089
  ctx.t = (key, params, fallback) => translate(msgs, key, params, fallback);
package/dist/react.js CHANGED
@@ -143,7 +143,8 @@ import { createElement, useCallback as useCallback3, useState as useState3, useE
143
143
 
144
144
  // tsx-context.ts
145
145
  import { useSyncExternalStore, createContext } from "react";
146
- var _ctx = { params: {}, query: {} };
146
+ var fallbackT = (key, _params, fallback) => fallback ?? key;
147
+ var _ctx = { params: {}, query: {}, parsed: {}, prefs: {}, env: {}, t: fallbackT, user: {} };
147
148
  var _listeners = /* @__PURE__ */ new Set();
148
149
  function setCtx(value) {
149
150
  _ctx = { ..._ctx, ...value };
@@ -151,7 +152,7 @@ function setCtx(value) {
151
152
  }
152
153
  function _buildT() {
153
154
  const messages = typeof window !== "undefined" ? window.__LOCALE_DATA__ : globalThis.__LOCALE_DATA__;
154
- if (!messages) return void 0;
155
+ if (!messages) return fallbackT;
155
156
  return (key, params, fallback) => {
156
157
  const msg = key.split(".").reduce((o, k) => o?.[k], messages);
157
158
  if (msg === void 0 || msg === null) return fallback ?? key;
@@ -174,11 +175,9 @@ function useCtx() {
174
175
  );
175
176
  const data = typeof window !== "undefined" ? window.__WEIFUWU_CTX : null;
176
177
  const t = data?.t ?? _ctx.t ?? _buildT();
177
- const result = { ..._ctx, ...data };
178
- if (t) result.t = t;
179
- return result;
178
+ return { ..._ctx, ...data, t };
180
179
  }
181
- var TsxContext = createContext({ params: {}, query: {} });
180
+ var TsxContext = createContext({ params: {}, query: {}, parsed: {}, prefs: {}, env: {}, t: fallbackT, user: {} });
182
181
 
183
182
  // client-router.ts
184
183
  var _navigating = false;
@@ -210,12 +209,10 @@ async function navigate(href) {
210
209
  const data = await res.json();
211
210
  const ctx = { ...window.__WEIFUWU_CTX || {}, params: {}, query: {} };
212
211
  if (data.locale) {
213
- ctx.locale = data.locale;
214
212
  ctx.prefs = { ...ctx.prefs, locale: data.locale };
215
213
  if (data.messages) window.__LOCALE_DATA__ = data.messages;
216
214
  }
217
215
  if (data.theme) {
218
- ctx.theme = data.theme;
219
216
  ctx.prefs = { ...ctx.prefs, theme: data.theme };
220
217
  }
221
218
  ;
@@ -1,13 +1,13 @@
1
1
  export interface CtxValue {
2
2
  params: Record<string, string>;
3
3
  query: Record<string, string>;
4
- user?: unknown;
5
- parsed?: Record<string, unknown>;
6
- prefs?: Record<string, string>;
7
- locale?: string;
8
- theme?: string;
9
- t?: (key: string, params?: Record<string, string>, fallback?: string) => string;
10
- env?: Record<string, string>;
4
+ user: {
5
+ id?: string;
6
+ };
7
+ parsed: Record<string, unknown>;
8
+ prefs: Record<string, string>;
9
+ t: (key: string, params?: Record<string, string>, fallback?: string) => string;
10
+ env: Record<string, string>;
11
11
  }
12
12
  export declare function setCtx(value: Partial<CtxValue>): void;
13
13
  export declare function useCtx(): CtxValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "weifuwu",
3
- "version": "0.17.8",
3
+ "version": "0.17.9",
4
4
  "description": "Web-standard HTTP framework for Node.js — (req, ctx) => Response",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",