@lolyjs/core 0.2.0-alpha.2 → 0.2.0-alpha.4

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/index.cjs CHANGED
@@ -47,17 +47,19 @@ __export(globals_exports, {
47
47
  NOT_FOUND_FILE_PREFIX: () => NOT_FOUND_FILE_PREFIX,
48
48
  NOT_FOUND_PATTERN: () => NOT_FOUND_PATTERN,
49
49
  PAGE_FILE_NAME: () => PAGE_FILE_NAME,
50
+ ROUTER_DATA_KEY: () => ROUTER_DATA_KEY,
50
51
  STATIC_PATH: () => STATIC_PATH,
51
52
  STYLE_FILE_NAME: () => STYLE_FILE_NAME,
52
53
  WINDOW_DATA_KEY: () => WINDOW_DATA_KEY
53
54
  });
54
- var BUILD_FOLDER_NAME, STYLE_FILE_NAME, WINDOW_DATA_KEY, APP_CONTAINER_ID, STATIC_PATH, NOT_FOUND_PATTERN, ERROR_PATTERN, NOT_FOUND_CHUNK_KEY, ERROR_CHUNK_KEY, NOT_FOUND_FILE_PREFIX, ERROR_FILE_PREFIX, PAGE_FILE_NAME, LAYOUT_FILE_NAME, FAVICON_PATH, CLIENT_CSS_PATH, CLIENT_JS_PATH, ASSETS_BASE_DIR;
55
+ var BUILD_FOLDER_NAME, STYLE_FILE_NAME, WINDOW_DATA_KEY, ROUTER_DATA_KEY, APP_CONTAINER_ID, STATIC_PATH, NOT_FOUND_PATTERN, ERROR_PATTERN, NOT_FOUND_CHUNK_KEY, ERROR_CHUNK_KEY, NOT_FOUND_FILE_PREFIX, ERROR_FILE_PREFIX, PAGE_FILE_NAME, LAYOUT_FILE_NAME, FAVICON_PATH, CLIENT_CSS_PATH, CLIENT_JS_PATH, ASSETS_BASE_DIR;
55
56
  var init_globals = __esm({
56
57
  "constants/globals.ts"() {
57
58
  "use strict";
58
59
  BUILD_FOLDER_NAME = ".loly";
59
60
  STYLE_FILE_NAME = "styles.css";
60
61
  WINDOW_DATA_KEY = "__FW_DATA__";
62
+ ROUTER_DATA_KEY = "__LOLY_ROUTER_DATA__";
61
63
  APP_CONTAINER_ID = "__app";
62
64
  STATIC_PATH = "/static";
63
65
  NOT_FOUND_PATTERN = "/not-found";
@@ -3877,6 +3879,7 @@ function createDocumentTree(options) {
3877
3879
  const {
3878
3880
  appTree,
3879
3881
  initialData,
3882
+ routerData,
3880
3883
  meta,
3881
3884
  titleFallback,
3882
3885
  descriptionFallback,
@@ -3914,6 +3917,9 @@ function createDocumentTree(options) {
3914
3917
  ...initialData,
3915
3918
  theme
3916
3919
  });
3920
+ const routerSerialized = JSON.stringify({
3921
+ ...routerData
3922
+ });
3917
3923
  const documentTree = import_react.default.createElement(
3918
3924
  "html",
3919
3925
  { lang },
@@ -3961,6 +3967,12 @@ function createDocumentTree(options) {
3961
3967
  dangerouslySetInnerHTML: {
3962
3968
  __html: `window.${WINDOW_DATA_KEY} = ${serialized};`
3963
3969
  }
3970
+ }),
3971
+ import_react.default.createElement("script", {
3972
+ nonce,
3973
+ dangerouslySetInnerHTML: {
3974
+ __html: `window.${ROUTER_DATA_KEY} = ${routerSerialized};`
3975
+ }
3964
3976
  })
3965
3977
  );
3966
3978
  return documentTree;
@@ -3983,6 +3995,15 @@ function buildInitialData(urlPath, params, loaderResult) {
3983
3995
  };
3984
3996
  }
3985
3997
 
3998
+ // modules/rendering/routerData/index.ts
3999
+ var buildRouterData = (req) => {
4000
+ return {
4001
+ pathname: req.path,
4002
+ params: req.params,
4003
+ searchParams: req.query
4004
+ };
4005
+ };
4006
+
3986
4007
  // modules/server/handlers/middleware.ts
3987
4008
  async function runRouteMiddlewares(route, ctx) {
3988
4009
  for (const mw of route.middlewares) {
@@ -4144,6 +4165,7 @@ async function handlePageRequestInternal(options) {
4144
4165
  }
4145
4166
  }
4146
4167
  const matched = matchRoute(routes, urlPath);
4168
+ const routerData = buildRouterData(req);
4147
4169
  if (!matched) {
4148
4170
  if (notFoundPage) {
4149
4171
  const ctx2 = {
@@ -4164,6 +4186,7 @@ async function handlePageRequestInternal(options) {
4164
4186
  const documentTree2 = createDocumentTree({
4165
4187
  appTree: appTree2,
4166
4188
  initialData: initialData2,
4189
+ routerData,
4167
4190
  meta: loaderResult2.metadata ?? null,
4168
4191
  titleFallback: "Not found",
4169
4192
  descriptionFallback: "Loly demo",
@@ -4271,6 +4294,7 @@ async function handlePageRequestInternal(options) {
4271
4294
  const documentTree = createDocumentTree({
4272
4295
  appTree,
4273
4296
  initialData,
4297
+ routerData,
4274
4298
  meta: loaderResult.metadata,
4275
4299
  titleFallback: "Loly framework",
4276
4300
  descriptionFallback: "Loly demo",
@@ -4328,6 +4352,7 @@ async function renderErrorPageWithStream(errorPage, req, res, error, routeChunks
4328
4352
  loaderResult.theme = theme;
4329
4353
  }
4330
4354
  const initialData = buildInitialData(req.path, { error: String(error) }, loaderResult);
4355
+ const routerData = buildRouterData(req);
4331
4356
  initialData.error = true;
4332
4357
  if (isDataReq) {
4333
4358
  res.statusCode = 500;
@@ -4358,6 +4383,7 @@ async function renderErrorPageWithStream(errorPage, req, res, error, routeChunks
4358
4383
  const documentTree = createDocumentTree({
4359
4384
  appTree,
4360
4385
  initialData,
4386
+ routerData,
4361
4387
  meta: loaderResult.metadata ?? null,
4362
4388
  titleFallback: "Error",
4363
4389
  descriptionFallback: "An error occurred",
@@ -4940,10 +4966,12 @@ async function renderStaticRoute(projectRoot, ssgOutDir, route, urlPath, params)
4940
4966
  return;
4941
4967
  }
4942
4968
  const initialData = buildInitialData(urlPath, params, loaderResult);
4969
+ const routerData = buildRouterData(req);
4943
4970
  const appTree = buildAppTree(route, params, initialData.props);
4944
4971
  const documentTree = createDocumentTree({
4945
4972
  appTree,
4946
4973
  initialData,
4974
+ routerData,
4947
4975
  meta: loaderResult.metadata,
4948
4976
  titleFallback: "My Framework Dev",
4949
4977
  descriptionFallback: "Static page generated by @lolyjs/core.",
@@ -5134,12 +5162,23 @@ var import_client5 = require("react-dom/client");
5134
5162
 
5135
5163
  // modules/runtime/client/constants.ts
5136
5164
  var WINDOW_DATA_KEY2 = "__FW_DATA__";
5165
+ var ROUTER_DATA_KEY2 = "__LOLY_ROUTER_DATA__";
5137
5166
  var APP_CONTAINER_ID2 = "__app";
5167
+ var ROUTER_NAVIGATE_KEY = "__LOLY_ROUTER_NAVIGATE__";
5138
5168
 
5139
5169
  // modules/runtime/client/window-data.ts
5140
5170
  function getWindowData() {
5171
+ if (typeof window === "undefined") {
5172
+ return null;
5173
+ }
5141
5174
  return window[WINDOW_DATA_KEY2] ?? null;
5142
5175
  }
5176
+ function getRouterData() {
5177
+ if (typeof window === "undefined") {
5178
+ return null;
5179
+ }
5180
+ return window[ROUTER_DATA_KEY2] ?? null;
5181
+ }
5143
5182
  function setWindowData(data) {
5144
5183
  window[WINDOW_DATA_KEY2] = data;
5145
5184
  if (typeof window !== "undefined") {
@@ -5150,6 +5189,16 @@ function setWindowData(data) {
5150
5189
  );
5151
5190
  }
5152
5191
  }
5192
+ function setRouterData(data) {
5193
+ window[ROUTER_DATA_KEY2] = data;
5194
+ if (typeof window !== "undefined") {
5195
+ window.dispatchEvent(
5196
+ new CustomEvent("fw-router-data-refresh", {
5197
+ detail: { data }
5198
+ })
5199
+ );
5200
+ }
5201
+ }
5153
5202
  function getCurrentTheme() {
5154
5203
  return getWindowData()?.theme ?? null;
5155
5204
  }
@@ -5214,7 +5263,7 @@ function applyMetadata(md) {
5214
5263
  }
5215
5264
 
5216
5265
  // modules/runtime/client/AppShell.tsx
5217
- var import_react2 = require("react");
5266
+ var import_react3 = require("react");
5218
5267
 
5219
5268
  // modules/runtime/client/RouterView.tsx
5220
5269
  var import_jsx_runtime = require("react/jsx-runtime");
@@ -5411,6 +5460,13 @@ async function handleErrorRoute(nextUrl, json, errorRoute, setState) {
5411
5460
  error: true
5412
5461
  };
5413
5462
  setWindowData(windowData);
5463
+ const url = new URL(nextUrl, typeof window !== "undefined" ? window.location.origin : "http://localhost");
5464
+ const routerData = {
5465
+ pathname: url.pathname,
5466
+ params: json.params || {},
5467
+ searchParams: Object.fromEntries(url.searchParams.entries())
5468
+ };
5469
+ setRouterData(routerData);
5414
5470
  setState({
5415
5471
  url: nextUrl,
5416
5472
  route: errorRoute,
@@ -5457,6 +5513,13 @@ async function handleNotFoundRoute(nextUrl, json, notFoundRoute, setState) {
5457
5513
  error: false
5458
5514
  };
5459
5515
  setWindowData(windowData);
5516
+ const url = new URL(nextUrl, typeof window !== "undefined" ? window.location.origin : "http://localhost");
5517
+ const routerData = {
5518
+ pathname: url.pathname,
5519
+ params: {},
5520
+ searchParams: Object.fromEntries(url.searchParams.entries())
5521
+ };
5522
+ setRouterData(routerData);
5460
5523
  if (notFoundRoute) {
5461
5524
  const components = await notFoundRoute.load();
5462
5525
  setState({
@@ -5514,6 +5577,13 @@ async function handleNormalRoute(nextUrl, json, routes, setState) {
5514
5577
  error: false
5515
5578
  };
5516
5579
  setWindowData(windowData);
5580
+ const url = new URL(nextUrl, typeof window !== "undefined" ? window.location.origin : "http://localhost");
5581
+ const routerData = {
5582
+ pathname: url.pathname,
5583
+ params: matched.params,
5584
+ searchParams: Object.fromEntries(url.searchParams.entries())
5585
+ };
5586
+ setRouterData(routerData);
5517
5587
  const components = await matched.route.load();
5518
5588
  window.scrollTo({
5519
5589
  top: 0,
@@ -5633,6 +5703,10 @@ function createPopStateHandler(navigate2) {
5633
5703
  };
5634
5704
  }
5635
5705
 
5706
+ // modules/runtime/client/RouterContext.tsx
5707
+ var import_react2 = require("react");
5708
+ var RouterContext = (0, import_react2.createContext)(null);
5709
+
5636
5710
  // modules/runtime/client/AppShell.tsx
5637
5711
  var import_jsx_runtime2 = require("react/jsx-runtime");
5638
5712
  function AppShell({
@@ -5641,14 +5715,14 @@ function AppShell({
5641
5715
  notFoundRoute,
5642
5716
  errorRoute
5643
5717
  }) {
5644
- const [state, setState] = (0, import_react2.useState)(initialState);
5645
- const handlersRef = (0, import_react2.useRef)({
5718
+ const [state, setState] = (0, import_react3.useState)(initialState);
5719
+ const handlersRef = (0, import_react3.useRef)({
5646
5720
  setState,
5647
5721
  routes,
5648
5722
  notFoundRoute,
5649
5723
  errorRoute
5650
5724
  });
5651
- (0, import_react2.useEffect)(() => {
5725
+ (0, import_react3.useEffect)(() => {
5652
5726
  handlersRef.current = {
5653
5727
  setState,
5654
5728
  routes,
@@ -5656,14 +5730,30 @@ function AppShell({
5656
5730
  errorRoute
5657
5731
  };
5658
5732
  }, [routes, notFoundRoute, errorRoute]);
5659
- (0, import_react2.useEffect)(() => {
5733
+ const handleNavigate = (0, import_react3.useCallback)(
5734
+ async (nextUrl, options) => {
5735
+ await navigate(nextUrl, handlersRef.current, {
5736
+ revalidate: options?.revalidate
5737
+ });
5738
+ },
5739
+ []
5740
+ );
5741
+ (0, import_react3.useEffect)(() => {
5742
+ if (typeof window !== "undefined") {
5743
+ window[ROUTER_NAVIGATE_KEY] = handleNavigate;
5744
+ return () => {
5745
+ delete window[ROUTER_NAVIGATE_KEY];
5746
+ };
5747
+ }
5748
+ }, [handleNavigate]);
5749
+ (0, import_react3.useEffect)(() => {
5660
5750
  let isMounted = true;
5661
- async function handleNavigate(nextUrl, options) {
5751
+ async function handleNavigateInternal(nextUrl, options) {
5662
5752
  if (!isMounted) return;
5663
5753
  await navigate(nextUrl, handlersRef.current, options);
5664
5754
  }
5665
- const handleClick = createClickHandler(handleNavigate);
5666
- const handlePopState = createPopStateHandler(handleNavigate);
5755
+ const handleClick = createClickHandler(handleNavigateInternal);
5756
+ const handlePopState = createPopStateHandler(handleNavigateInternal);
5667
5757
  window.addEventListener("click", handleClick, false);
5668
5758
  window.addEventListener("popstate", handlePopState, false);
5669
5759
  return () => {
@@ -5676,7 +5766,7 @@ function AppShell({
5676
5766
  const isNotFound = state.route === notFoundRoute;
5677
5767
  const routeType = isError ? "error" : isNotFound ? "notfound" : "normal";
5678
5768
  const routeKey = `${state.url}:${routeType}`;
5679
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RouterView, { state }, routeKey);
5769
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RouterContext.Provider, { value: { navigate: handleNavigate }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RouterView, { state }, routeKey) });
5680
5770
  }
5681
5771
 
5682
5772
  // modules/runtime/client/bootstrap.tsx
@@ -5776,6 +5866,16 @@ function bootstrapClient(routes, notFoundRoute, errorRoute = null) {
5776
5866
  return;
5777
5867
  }
5778
5868
  const initialUrl = window.location.pathname + window.location.search;
5869
+ let routerData = getRouterData();
5870
+ if (!routerData) {
5871
+ const url = new URL(initialUrl, window.location.origin);
5872
+ routerData = {
5873
+ pathname: url.pathname,
5874
+ params: initialData?.params || {},
5875
+ searchParams: Object.fromEntries(url.searchParams.entries())
5876
+ };
5877
+ setRouterData(routerData);
5878
+ }
5779
5879
  try {
5780
5880
  const initialState = await loadInitialRoute(
5781
5881
  initialUrl,