@valbuild/tanstack 0.148.1 → 0.148.3

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 (26) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +53 -0
  3. package/client/dist/valbuild-tanstack-client.cjs.dev.js +12 -4
  4. package/client/dist/valbuild-tanstack-client.cjs.prod.js +12 -4
  5. package/client/dist/valbuild-tanstack-client.esm.js +12 -4
  6. package/dist/{ValOverlayContext-510328ec.cjs.dev.js → ValOverlayContext-198b4bd1.cjs.dev.js} +4 -1
  7. package/dist/{ValOverlayContext-6278320b.esm.js → ValOverlayContext-6411bbe3.esm.js} +4 -1
  8. package/dist/{ValOverlayContext-d3acd157.cjs.prod.js → ValOverlayContext-d9b22d1a.cjs.prod.js} +4 -1
  9. package/dist/declarations/src/ValAnalytics.d.ts +23 -0
  10. package/dist/declarations/src/ValApp.d.ts +6 -1
  11. package/dist/declarations/src/ValProvider.d.ts +1 -0
  12. package/dist/declarations/src/external_exempt_from_val_quickjs.d.ts +3 -0
  13. package/dist/declarations/src/server/index.d.ts +1 -0
  14. package/dist/{routeFromVal-0ea4586f.cjs.dev.js → routeFromVal-2f65a4fc.cjs.dev.js} +11 -11
  15. package/dist/{routeFromVal-9f1f8725.esm.js → routeFromVal-40cd8a5b.esm.js} +11 -11
  16. package/dist/{routeFromVal-97a52f43.cjs.prod.js → routeFromVal-a13ac8a3.cjs.prod.js} +11 -11
  17. package/dist/valbuild-tanstack.cjs.dev.js +115 -28
  18. package/dist/valbuild-tanstack.cjs.prod.js +115 -28
  19. package/dist/valbuild-tanstack.esm.js +115 -28
  20. package/dist/{version-684beca4.cjs.prod.js → version-4353b65f.cjs.prod.js} +1 -1
  21. package/dist/{version-a77659ce.esm.js → version-79ab0f75.esm.js} +1 -1
  22. package/dist/{version-fd860c4b.cjs.dev.js → version-f0497731.cjs.dev.js} +1 -1
  23. package/package.json +8 -8
  24. package/server/dist/valbuild-tanstack-server.cjs.dev.js +10 -10
  25. package/server/dist/valbuild-tanstack-server.cjs.prod.js +10 -10
  26. package/server/dist/valbuild-tanstack-server.esm.js +10 -10
@@ -6,12 +6,12 @@ var asyncToGenerator = require('./asyncToGenerator-8e5c36c8.cjs.prod.js');
6
6
  var _typeof = require('./typeof-8998e126.cjs.prod.js');
7
7
  var core = require('@valbuild/core');
8
8
  var stega = require('@valbuild/react/stega');
9
- var version = require('./version-684beca4.cjs.prod.js');
9
+ var version = require('./version-4353b65f.cjs.prod.js');
10
10
  var internal = require('@valbuild/react/internal');
11
11
  var ui = require('@valbuild/ui');
12
12
  var reactRouter = require('@tanstack/react-router');
13
13
  var React = require('react');
14
- var ValOverlayContext = require('./ValOverlayContext-d3acd157.cjs.prod.js');
14
+ var ValOverlayContext = require('./ValOverlayContext-d9b22d1a.cjs.prod.js');
15
15
  var client = require('@valbuild/shared/client');
16
16
  var jsxRuntime = require('react/jsx-runtime');
17
17
  var demoServiceWorker = require('./demoServiceWorker-994710f7.cjs.prod.js');
@@ -951,7 +951,7 @@ var DRAFT_HANDSHAKE_POLL_MS = 100;
951
951
  */
952
952
  var DRAFT_HANDSHAKE_TIMEOUT_MS = 10000;
953
953
  var ValTanStackProvider = function ValTanStackProvider(props) {
954
- var _props$draft, _draftModeReady$curre2;
954
+ var _props$draft, _props$analytics, _draftModeReady$curre2;
955
955
  // TODO: use config:
956
956
  var route = "/api/val";
957
957
  /**
@@ -1006,6 +1006,14 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1006
1006
  * not a draft: it must not turn draft mode on or say the draft is synced.
1007
1007
  */
1008
1008
  var hasDraft = initialDraft !== null && initialDraft.draftMode !== false;
1009
+ /*
1010
+ * As the page was rendered. A ref, not the prop, for the reason
1011
+ * `initialDraft` is one: a later render's prop is a client navigation's
1012
+ * loader, which reads nothing.
1013
+ */
1014
+ var analyticsSetting = React__default["default"].useRef((_props$analytics = props.analytics) !== null && _props$analytics !== void 0 ? _props$analytics : null).current;
1015
+ var analytics = (analyticsSetting === null || analyticsSetting === void 0 ? void 0 : analyticsSetting.enabled) === true;
1016
+ var contentVersion = analyticsSetting === null || analyticsSetting === void 0 ? void 0 : analyticsSetting.contentVersion;
1009
1017
  var snapshot = initialDraft === null || initialDraft === void 0 ? void 0 : initialDraft.snapshot;
1010
1018
  /*
1011
1019
  * The base the Studio's overlay must start from, where it is not the
@@ -1157,7 +1165,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1157
1165
  useConsoleLogEnableVal(mountOverlay);
1158
1166
  React__default["default"].useEffect(function () {
1159
1167
  if (location.search === "?message_onready=true") {
1160
- console.warn("Val is verifying draft mode...");
1168
+ core.Internal.log.debug("verifying draft mode...");
1161
1169
  return;
1162
1170
  }
1163
1171
  if (isValStudioPath(location.pathname)) {
@@ -1177,6 +1185,54 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1177
1185
  setIsCanvas(client.isValCanvasFrame(location.search, readParentPathname()));
1178
1186
  setMountOverlay(hasValEnableCookie(document.cookie));
1179
1187
  }, []);
1188
+
1189
+ /**
1190
+ * The analytics script, for a visitor and nobody else.
1191
+ *
1192
+ * `mountOverlay === false` is "known not to be an editor": no Val enable
1193
+ * cookie, so no draft mode and no canvas. A page the server rendered from a
1194
+ * draft is an editor's too, whatever the enable cookie says -- the draft
1195
+ * mode cookie can outlive it. So is one at a proposal's address, which
1196
+ * comes with a snapshot: its content is not published, and counting it
1197
+ * would credit the live site with it. The Studio's own pages count nothing
1198
+ * either. Loaded with a dynamic import after hydration, so a site
1199
+ * with analytics off never downloads it, and one with it on downloads it
1200
+ * off the critical path. It imports nothing from core, shared or zod.
1201
+ */
1202
+ React__default["default"].useEffect(function () {
1203
+ if (!analytics || mountOverlay !== false || hasDraft || snapshot !== undefined || isCanvas || isValStudioPath(location.pathname)) {
1204
+ return;
1205
+ }
1206
+ // Where the page was hydrated, read now: the script loads later, and the
1207
+ // visitor may have moved on by then. See `landingPath`.
1208
+ var landingPath = location.pathname;
1209
+ var cancelled = false;
1210
+ var tracker = null;
1211
+ Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@valbuild/react/analytics')); }).then(function (_ref2) {
1212
+ var startValAnalytics = _ref2.startValAnalytics;
1213
+ if (!cancelled) {
1214
+ tracker = startValAnalytics({
1215
+ endpoint: "".concat(route, "/analytics"),
1216
+ landingPath: landingPath,
1217
+ contentVersion: contentVersion,
1218
+ // The route's pattern (`/posts/$postId`), never the pathname,
1219
+ // which can hold something about one visitor. A pathname no
1220
+ // route matches is not a page of the site.
1221
+ routeFor: function routeFor(pathname) {
1222
+ var _router$getMatchedRou, _router$getMatchedRou2;
1223
+ return (_router$getMatchedRou = (_router$getMatchedRou2 = router.getMatchedRoutes(pathname)[2]) === null || _router$getMatchedRou2 === void 0 ? void 0 : _router$getMatchedRou2.fullPath) !== null && _router$getMatchedRou !== void 0 ? _router$getMatchedRou : "(not found)";
1224
+ }
1225
+ });
1226
+ }
1227
+ })["catch"](function (error) {
1228
+ core.Internal.log.warn("could not load analytics", error);
1229
+ });
1230
+ return function () {
1231
+ var _tracker;
1232
+ cancelled = true;
1233
+ (_tracker = tracker) === null || _tracker === void 0 || _tracker.stop();
1234
+ };
1235
+ }, [analytics, contentVersion, mountOverlay, hasDraft, snapshot, isCanvas, route, router]);
1180
1236
  React__default["default"].useEffect(function () {
1181
1237
  // Activate the Suspense gate after hydration. Inside a transition so
1182
1238
  // already-visible (static) content stays on screen while useValStega
@@ -1324,7 +1380,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1324
1380
  var valProviderOverlayListener = function valProviderOverlayListener(event) {
1325
1381
  if (event instanceof CustomEvent) {
1326
1382
  if (!(event !== null && event !== void 0 && event.detail.type)) {
1327
- console.error("Val: invalid event detail (val-overlay-provider)", event);
1383
+ core.Internal.log.error("invalid event detail (val-overlay-provider)", event);
1328
1384
  }
1329
1385
  if (event.detail.type === "spa-ready") {
1330
1386
  setSpaReady(true);
@@ -1346,10 +1402,10 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1346
1402
  });
1347
1403
  }
1348
1404
  } else {
1349
- console.error("Val: invalid event detail (val-overlay-provider)", event.detail);
1405
+ core.Internal.log.error("invalid event detail (val-overlay-provider)", event.detail);
1350
1406
  }
1351
1407
  } else {
1352
- console.error("Val: invalid event (val-overlay-provider)", event);
1408
+ core.Internal.log.error("invalid event (val-overlay-provider)", event);
1353
1409
  }
1354
1410
  };
1355
1411
  window.addEventListener("val-overlay-provider", valProviderOverlayListener);
@@ -1394,7 +1450,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1394
1450
  return;
1395
1451
  }
1396
1452
  if (res.status !== 200) {
1397
- console.error("Val: could not get draft mode status", res);
1453
+ core.Internal.log.error("could not get draft mode status", res);
1398
1454
  return;
1399
1455
  }
1400
1456
  setDraftMode(function (prev) {
@@ -1406,7 +1462,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1406
1462
  return prev;
1407
1463
  });
1408
1464
  })["catch"](function (err) {
1409
- console.error("Val: could not get draft mode status", err);
1465
+ core.Internal.log.error("could not get draft mode status", err);
1410
1466
  })["finally"](function () {
1411
1467
  if (pollDraftStatIdRef.current !== pollDraftStatId) {
1412
1468
  return;
@@ -1423,7 +1479,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1423
1479
  * unrecoverable, because the ONLY other thing that clears it is the
1424
1480
  * `val-ready` message the frame never sent.
1425
1481
  */
1426
- console.warn("Val: draft mode did not confirm in time. Falling back to the " + "idle poll — try toggling preview again.");
1482
+ core.Internal.log.warn("draft mode did not confirm in time. Falling back to the " + "idle poll — try toggling preview again.");
1427
1483
  setIframeSrc(null);
1428
1484
  return;
1429
1485
  }
@@ -1456,8 +1512,14 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1456
1512
  }, [mountOverlay, draftMode, spaReady]);
1457
1513
  React__default["default"].useEffect(function () {
1458
1514
  if (!mountOverlay) {
1459
- stega.SET_AUTO_TAG_JSX_ENABLED(false);
1515
+ // A visitor: only analytics tags content, and only after hydration
1516
+ // (`useEncodesEditTags`), so this cannot reach a component still
1517
+ // hydrating either.
1518
+ stega.SET_AUTO_TAG_JSX_ENABLED(analytics);
1460
1519
  } else {
1520
+ if (analytics) {
1521
+ stega.SET_AUTO_TAG_JSX_ENABLED(true);
1522
+ }
1461
1523
  if (draftMode) {
1462
1524
  // Safe for a page rendered from a server draft too: what a component
1463
1525
  // renders while it hydrates has no edit tags to turn into attributes
@@ -1496,13 +1558,13 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1496
1558
  window.dispatchEvent(new Event(VAL_EDIT_LANDED));
1497
1559
  }
1498
1560
  } else {
1499
- console.error("Val: invalid event detail", event.detail);
1561
+ core.Internal.log.error("invalid event detail", event.detail);
1500
1562
  }
1501
1563
  } else {
1502
- console.error("Val: invalid custom event details (val-event)", event.detail);
1564
+ core.Internal.log.error("invalid custom event details (val-event)", event.detail);
1503
1565
  }
1504
1566
  } else {
1505
- console.error("Val: invalid custom event (val-event)", event);
1567
+ core.Internal.log.error("invalid custom event (val-event)", event);
1506
1568
  }
1507
1569
  };
1508
1570
  window.addEventListener("val-event", reactServerComponentRefreshListener);
@@ -1511,7 +1573,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1511
1573
  };
1512
1574
  }
1513
1575
  }
1514
- }, [mountOverlay, draftMode, props.disableRefresh]);
1576
+ }, [mountOverlay, draftMode, analytics, props.disableRefresh]);
1515
1577
  React__default["default"].useEffect(function () {
1516
1578
  if (!mountOverlay) {
1517
1579
  return;
@@ -1596,6 +1658,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1596
1658
  draftModeReady: (_draftModeReady$curre2 = draftModeReady.current) === null || _draftModeReady$curre2 === void 0 ? void 0 : _draftModeReady$curre2.promise,
1597
1659
  draftSourcesSynced: draftSourcesSynced,
1598
1660
  serverDraft: hasDraft,
1661
+ analytics: analytics,
1599
1662
  snapshot: snapshot,
1600
1663
  suspend: suspendActive,
1601
1664
  store: valStore,
@@ -1671,7 +1734,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1671
1734
  function useConsoleLogEnableVal(showOverlay) {
1672
1735
  React__default["default"].useEffect(function () {
1673
1736
  if (process.env["NODE_ENV"] === "development" && showOverlay === false && !isValStudioPath(location.pathname)) {
1674
- console.warn("\n###########\n###########\n########### @@@@\n########### @@\n########### @@ @@ @@@@@@ @ @@\n########### @@ @@ @@ @@ @@\n########### @@ @@ %@ @ @@\n#### ##### @@ @@ .@ .@ @@\n### #### @@@@ @@: @@@. @@\n#### ##### @@@@ @@@@ =@@@@@@@@@\n###########\n\nThis page is built with Val Build - the lightweight CMS where content is code.\n\nVal is currently hidden.\n\nTo show Val, go to the following URL:\n".concat(window.location.origin, "/api/val/enable?redirect_to=").concat(encodeURIComponent(window.location.href), "\n \nYou are seeing this message because you are in development mode."));
1737
+ core.Internal.log.warn("\n###########\n###########\n########### @@@@\n########### @@\n########### @@ @@ @@@@@@ @ @@\n########### @@ @@ @@ @@ @@\n########### @@ @@ %@ @ @@\n#### ##### @@ @@ .@ .@ @@\n### #### @@@@ @@: @@@. @@\n#### ##### @@@@ @@@@ =@@@@@@@@@\n###########\n\nThis page is built with Val Build - the lightweight CMS where content is code.\n\nVal is currently hidden.\n\nTo show Val, go to the following URL:\n".concat(window.location.origin, "/api/val/enable?redirect_to=").concat(encodeURIComponent(window.location.href), "\n \nYou are seeing this message because you are in development mode."));
1675
1738
  }
1676
1739
  }, [showOverlay]);
1677
1740
  }
@@ -1710,8 +1773,13 @@ function getPositionClassName(dropZone) {
1710
1773
  }
1711
1774
  return className;
1712
1775
  }
1776
+
1777
+ /**
1778
+ * The Studio is `/val` and what is below it -- not every route that starts
1779
+ * with the letters, which would leave `/values` without its overlay.
1780
+ */
1713
1781
  function isValStudioPath(pathname) {
1714
- return pathname.startsWith("/val");
1782
+ return pathname === "/val" || pathname.startsWith("/val/");
1715
1783
  }
1716
1784
 
1717
1785
  /**
@@ -1945,7 +2013,7 @@ function ValVideo(props) {
1945
2013
  }
1946
2014
  var loadHls = hlsRef.current;
1947
2015
  if (!loadHls) {
1948
- console.warn('Val: this browser cannot play HLS streams on its own. Pass `hls={() => import("hls.js")}` to ValVideo.');
2016
+ core.Internal.log.warn('this browser cannot play HLS streams on its own. Pass `hls={() => import("hls.js")}` to ValVideo.');
1949
2017
  return;
1950
2018
  }
1951
2019
  var destroyed = false;
@@ -2126,7 +2194,7 @@ function _startDemoRelay() {
2126
2194
  answer(fetch, event.data).then(function (res) {
2127
2195
  return port.postMessage(res, res.body ? [res.body] : []);
2128
2196
  })["catch"](function (err) {
2129
- console.error("Val: the demo could not answer a relayed request", err);
2197
+ core.Internal.log.error("the demo could not answer a relayed request", err);
2130
2198
  port.postMessage({
2131
2199
  status: 500,
2132
2200
  headers: [["Content-Type", "application/json"]],
@@ -2177,7 +2245,7 @@ function _startDemoRelay() {
2177
2245
  case 6:
2178
2246
  _context2.p = 6;
2179
2247
  _t2 = _context2.v;
2180
- console.error("Val: the demo's service worker did not start", _t2);
2248
+ core.Internal.log.error("the demo's service worker did not start", _t2);
2181
2249
  return _context2.a(2, false);
2182
2250
  }
2183
2251
  }, _callee2, null, [[2, 6]]);
@@ -2205,12 +2273,18 @@ var ValApp = function ValApp(_ref) {
2205
2273
  children = _ref.children,
2206
2274
  demo = _ref.demo;
2207
2275
  var route = "/api/val"; // TODO: make configurable
2208
- var demoStart = useDemoServer(demo === true, config);
2209
2276
  var _useState = React.useState(),
2210
2277
  _useState2 = _typeof._slicedToArray(_useState, 2),
2211
2278
  inMessageMode = _useState2[0],
2212
2279
  setInMessageMode = _useState2[1];
2213
2280
  var isClientSIde = inMessageMode === undefined;
2281
+ // Not before `children` are rendered, which is not on the first render: the
2282
+ // modules the demo's server needs are registered by one of them
2283
+ // (`ValModulesClient`), and a child's effects run before this component's
2284
+ // in the commit that mounts it. Started on the first render, the demo read
2285
+ // them whenever the server's code had loaded -- before the child was there,
2286
+ // on a page that had that code already.
2287
+ var demoStart = useDemoServer(demo !== undefined && demo !== false && inMessageMode === false, config, demo === undefined || typeof demo === "boolean" ? undefined : demo);
2214
2288
  useConfigStorageSave(config);
2215
2289
  var container = React.useRef(null);
2216
2290
  React.useEffect(function () {
@@ -2367,8 +2441,9 @@ var ValApp = function ValApp(_ref) {
2367
2441
  }
2368
2442
  if (demo && demoStart.status !== "ready") {
2369
2443
  // The Studio reads `window.__VAL_DEMO__` when its script starts, so the
2370
- // server it talks to has to be there first. The modules are registered
2371
- // by a child (`ValModulesClient`), whose effect has run by then.
2444
+ // server it talks to has to be there first. The children are rendered
2445
+ // meanwhile: one of them registers the modules that server is started
2446
+ // with.
2372
2447
  return /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, {
2373
2448
  children: children
2374
2449
  });
@@ -2390,19 +2465,28 @@ var ValApp = function ValApp(_ref) {
2390
2465
  * half of the server, and only a demo should download it.
2391
2466
  */
2392
2467
 
2393
- function useDemoServer(demo, config) {
2468
+ function useDemoServer(demo, config, options) {
2394
2469
  var _useState7 = React.useState({
2395
2470
  status: "starting"
2396
2471
  }),
2397
2472
  _useState8 = _typeof._slicedToArray(_useState7, 2),
2398
2473
  start = _useState8[0],
2399
2474
  setStart = _useState8[1];
2475
+ /*
2476
+ * Read when the demo starts, and not a dependency of the effect: options
2477
+ * written inline (`demo={{ tour }}`) are a new object on every render, and
2478
+ * starting Val's server again for each would throw away every edit. The
2479
+ * Studio reads them once anyway, when its script starts.
2480
+ */
2481
+ var optionsRef = React.useRef(options);
2482
+ optionsRef.current = options;
2400
2483
  React.useEffect(function () {
2401
2484
  if (!demo) return;
2402
2485
  var cancelled = false;
2403
2486
  var run = /*#__PURE__*/function () {
2404
2487
  var _ref2 = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee() {
2405
- var _yield$import, createInTabValFetch, modules, fetch, uploads, runtime;
2488
+ var _optionsRef$current;
2489
+ var _yield$import, createInTabValFetch, modules, fetch, uploads, tour, runtime;
2406
2490
  return asyncToGenerator._regenerator().w(function (_context) {
2407
2491
  while (1) switch (_context.n) {
2408
2492
  case 0:
@@ -2430,10 +2514,13 @@ function useDemoServer(demo, config) {
2430
2514
  return startDemoRelay(fetch);
2431
2515
  case 3:
2432
2516
  uploads = _context.v;
2433
- runtime = {
2517
+ tour = (_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 ? void 0 : _optionsRef$current.tour;
2518
+ runtime = _typeof._objectSpread2({
2434
2519
  fetch: fetch,
2435
2520
  uploads: uploads
2436
- };
2521
+ }, tour ? {
2522
+ tour: tour
2523
+ } : {});
2437
2524
  window.__VAL_DEMO__ = runtime;
2438
2525
  return _context.a(2, {
2439
2526
  status: "ready"
@@ -2446,7 +2533,7 @@ function useDemoServer(demo, config) {
2446
2533
  };
2447
2534
  }();
2448
2535
  run()["catch"](function (err) {
2449
- console.error("Val: the demo could not start", err);
2536
+ core.Internal.log.error("the demo could not start", err);
2450
2537
  return {
2451
2538
  status: "failed",
2452
2539
  // Most often the server's code did not download: the page is fine,