@lolyjs/core 0.2.0-alpha.0 → 0.2.0-alpha.10

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.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import http from 'http';
2
2
  import { Request, Response } from 'express';
3
3
  import { Socket, Server } from 'socket.io';
4
- export { c as bootstrapClient } from './bootstrap-BiCQmSkx.mjs';
4
+ export { c as bootstrapClient } from './bootstrap-DgvWWDim.mjs';
5
5
  import { ZodSchema, z } from 'zod';
6
6
  import * as express_rate_limit from 'express-rate-limit';
7
7
  import pino, { Logger as Logger$1 } from 'pino';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import http from 'http';
2
2
  import { Request, Response } from 'express';
3
3
  import { Socket, Server } from 'socket.io';
4
- export { c as bootstrapClient } from './bootstrap-BiCQmSkx.js';
4
+ export { c as bootstrapClient } from './bootstrap-DgvWWDim.js';
5
5
  import { ZodSchema, z } from 'zod';
6
6
  import * as express_rate_limit from 'express-rate-limit';
7
7
  import pino, { Logger as Logger$1 } from 'pino';
package/dist/index.js CHANGED
@@ -42,17 +42,19 @@ __export(globals_exports, {
42
42
  NOT_FOUND_FILE_PREFIX: () => NOT_FOUND_FILE_PREFIX,
43
43
  NOT_FOUND_PATTERN: () => NOT_FOUND_PATTERN,
44
44
  PAGE_FILE_NAME: () => PAGE_FILE_NAME,
45
+ ROUTER_DATA_KEY: () => ROUTER_DATA_KEY,
45
46
  STATIC_PATH: () => STATIC_PATH,
46
47
  STYLE_FILE_NAME: () => STYLE_FILE_NAME,
47
48
  WINDOW_DATA_KEY: () => WINDOW_DATA_KEY
48
49
  });
49
- 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;
50
+ 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;
50
51
  var init_globals = __esm({
51
52
  "constants/globals.ts"() {
52
53
  "use strict";
53
54
  BUILD_FOLDER_NAME = ".loly";
54
55
  STYLE_FILE_NAME = "styles.css";
55
56
  WINDOW_DATA_KEY = "__FW_DATA__";
57
+ ROUTER_DATA_KEY = "__LOLY_ROUTER_DATA__";
56
58
  APP_CONTAINER_ID = "__app";
57
59
  STATIC_PATH = "/static";
58
60
  NOT_FOUND_PATTERN = "/not-found";
@@ -3835,6 +3837,7 @@ function createDocumentTree(options) {
3835
3837
  const {
3836
3838
  appTree,
3837
3839
  initialData,
3840
+ routerData,
3838
3841
  meta,
3839
3842
  titleFallback,
3840
3843
  descriptionFallback,
@@ -3872,6 +3875,9 @@ function createDocumentTree(options) {
3872
3875
  ...initialData,
3873
3876
  theme
3874
3877
  });
3878
+ const routerSerialized = JSON.stringify({
3879
+ ...routerData
3880
+ });
3875
3881
  const documentTree = React.createElement(
3876
3882
  "html",
3877
3883
  { lang },
@@ -3919,6 +3925,12 @@ function createDocumentTree(options) {
3919
3925
  dangerouslySetInnerHTML: {
3920
3926
  __html: `window.${WINDOW_DATA_KEY} = ${serialized};`
3921
3927
  }
3928
+ }),
3929
+ React.createElement("script", {
3930
+ nonce,
3931
+ dangerouslySetInnerHTML: {
3932
+ __html: `window.${ROUTER_DATA_KEY} = ${routerSerialized};`
3933
+ }
3922
3934
  })
3923
3935
  );
3924
3936
  return documentTree;
@@ -3941,6 +3953,15 @@ function buildInitialData(urlPath, params, loaderResult) {
3941
3953
  };
3942
3954
  }
3943
3955
 
3956
+ // modules/rendering/routerData/index.ts
3957
+ var buildRouterData = (req) => {
3958
+ return {
3959
+ pathname: req.path,
3960
+ params: req.params,
3961
+ searchParams: req.query
3962
+ };
3963
+ };
3964
+
3944
3965
  // modules/server/handlers/middleware.ts
3945
3966
  async function runRouteMiddlewares(route, ctx) {
3946
3967
  for (const mw of route.middlewares) {
@@ -4102,6 +4123,7 @@ async function handlePageRequestInternal(options) {
4102
4123
  }
4103
4124
  }
4104
4125
  const matched = matchRoute(routes, urlPath);
4126
+ const routerData = buildRouterData(req);
4105
4127
  if (!matched) {
4106
4128
  if (notFoundPage) {
4107
4129
  const ctx2 = {
@@ -4122,6 +4144,7 @@ async function handlePageRequestInternal(options) {
4122
4144
  const documentTree2 = createDocumentTree({
4123
4145
  appTree: appTree2,
4124
4146
  initialData: initialData2,
4147
+ routerData,
4125
4148
  meta: loaderResult2.metadata ?? null,
4126
4149
  titleFallback: "Not found",
4127
4150
  descriptionFallback: "Loly demo",
@@ -4229,6 +4252,7 @@ async function handlePageRequestInternal(options) {
4229
4252
  const documentTree = createDocumentTree({
4230
4253
  appTree,
4231
4254
  initialData,
4255
+ routerData,
4232
4256
  meta: loaderResult.metadata,
4233
4257
  titleFallback: "Loly framework",
4234
4258
  descriptionFallback: "Loly demo",
@@ -4286,6 +4310,7 @@ async function renderErrorPageWithStream(errorPage, req, res, error, routeChunks
4286
4310
  loaderResult.theme = theme;
4287
4311
  }
4288
4312
  const initialData = buildInitialData(req.path, { error: String(error) }, loaderResult);
4313
+ const routerData = buildRouterData(req);
4289
4314
  initialData.error = true;
4290
4315
  if (isDataReq) {
4291
4316
  res.statusCode = 500;
@@ -4316,6 +4341,7 @@ async function renderErrorPageWithStream(errorPage, req, res, error, routeChunks
4316
4341
  const documentTree = createDocumentTree({
4317
4342
  appTree,
4318
4343
  initialData,
4344
+ routerData,
4319
4345
  meta: loaderResult.metadata ?? null,
4320
4346
  titleFallback: "Error",
4321
4347
  descriptionFallback: "An error occurred",
@@ -4898,10 +4924,12 @@ async function renderStaticRoute(projectRoot, ssgOutDir, route, urlPath, params)
4898
4924
  return;
4899
4925
  }
4900
4926
  const initialData = buildInitialData(urlPath, params, loaderResult);
4927
+ const routerData = buildRouterData(req);
4901
4928
  const appTree = buildAppTree(route, params, initialData.props);
4902
4929
  const documentTree = createDocumentTree({
4903
4930
  appTree,
4904
4931
  initialData,
4932
+ routerData,
4905
4933
  meta: loaderResult.metadata,
4906
4934
  titleFallback: "My Framework Dev",
4907
4935
  descriptionFallback: "Static page generated by @lolyjs/core.",
@@ -5092,12 +5120,23 @@ import { hydrateRoot } from "react-dom/client";
5092
5120
 
5093
5121
  // modules/runtime/client/constants.ts
5094
5122
  var WINDOW_DATA_KEY2 = "__FW_DATA__";
5123
+ var ROUTER_DATA_KEY2 = "__LOLY_ROUTER_DATA__";
5095
5124
  var APP_CONTAINER_ID2 = "__app";
5125
+ var ROUTER_NAVIGATE_KEY = "__LOLY_ROUTER_NAVIGATE__";
5096
5126
 
5097
5127
  // modules/runtime/client/window-data.ts
5098
5128
  function getWindowData() {
5129
+ if (typeof window === "undefined") {
5130
+ return null;
5131
+ }
5099
5132
  return window[WINDOW_DATA_KEY2] ?? null;
5100
5133
  }
5134
+ function getRouterData() {
5135
+ if (typeof window === "undefined") {
5136
+ return null;
5137
+ }
5138
+ return window[ROUTER_DATA_KEY2] ?? null;
5139
+ }
5101
5140
  function setWindowData(data) {
5102
5141
  window[WINDOW_DATA_KEY2] = data;
5103
5142
  if (typeof window !== "undefined") {
@@ -5108,6 +5147,16 @@ function setWindowData(data) {
5108
5147
  );
5109
5148
  }
5110
5149
  }
5150
+ function setRouterData(data) {
5151
+ window[ROUTER_DATA_KEY2] = data;
5152
+ if (typeof window !== "undefined") {
5153
+ window.dispatchEvent(
5154
+ new CustomEvent("fw-router-data-refresh", {
5155
+ detail: { data }
5156
+ })
5157
+ );
5158
+ }
5159
+ }
5111
5160
  function getCurrentTheme() {
5112
5161
  return getWindowData()?.theme ?? null;
5113
5162
  }
@@ -5172,7 +5221,7 @@ function applyMetadata(md) {
5172
5221
  }
5173
5222
 
5174
5223
  // modules/runtime/client/AppShell.tsx
5175
- import { useEffect, useState, useRef } from "react";
5224
+ import { useEffect, useState, useRef, useCallback } from "react";
5176
5225
 
5177
5226
  // modules/runtime/client/RouterView.tsx
5178
5227
  import { jsx } from "react/jsx-runtime";
@@ -5369,6 +5418,13 @@ async function handleErrorRoute(nextUrl, json, errorRoute, setState) {
5369
5418
  error: true
5370
5419
  };
5371
5420
  setWindowData(windowData);
5421
+ const url = new URL(nextUrl, typeof window !== "undefined" ? window.location.origin : "http://localhost");
5422
+ const routerData = {
5423
+ pathname: url.pathname,
5424
+ params: json.params || {},
5425
+ searchParams: Object.fromEntries(url.searchParams.entries())
5426
+ };
5427
+ setRouterData(routerData);
5372
5428
  setState({
5373
5429
  url: nextUrl,
5374
5430
  route: errorRoute,
@@ -5415,6 +5471,13 @@ async function handleNotFoundRoute(nextUrl, json, notFoundRoute, setState) {
5415
5471
  error: false
5416
5472
  };
5417
5473
  setWindowData(windowData);
5474
+ const url = new URL(nextUrl, typeof window !== "undefined" ? window.location.origin : "http://localhost");
5475
+ const routerData = {
5476
+ pathname: url.pathname,
5477
+ params: {},
5478
+ searchParams: Object.fromEntries(url.searchParams.entries())
5479
+ };
5480
+ setRouterData(routerData);
5418
5481
  if (notFoundRoute) {
5419
5482
  const components = await notFoundRoute.load();
5420
5483
  setState({
@@ -5472,6 +5535,13 @@ async function handleNormalRoute(nextUrl, json, routes, setState) {
5472
5535
  error: false
5473
5536
  };
5474
5537
  setWindowData(windowData);
5538
+ const url = new URL(nextUrl, typeof window !== "undefined" ? window.location.origin : "http://localhost");
5539
+ const routerData = {
5540
+ pathname: url.pathname,
5541
+ params: matched.params,
5542
+ searchParams: Object.fromEntries(url.searchParams.entries())
5543
+ };
5544
+ setRouterData(routerData);
5475
5545
  const components = await matched.route.load();
5476
5546
  window.scrollTo({
5477
5547
  top: 0,
@@ -5591,6 +5661,10 @@ function createPopStateHandler(navigate2) {
5591
5661
  };
5592
5662
  }
5593
5663
 
5664
+ // modules/runtime/client/RouterContext.tsx
5665
+ import { createContext, useContext } from "react";
5666
+ var RouterContext = createContext(null);
5667
+
5594
5668
  // modules/runtime/client/AppShell.tsx
5595
5669
  import { jsx as jsx2 } from "react/jsx-runtime";
5596
5670
  function AppShell({
@@ -5614,14 +5688,30 @@ function AppShell({
5614
5688
  errorRoute
5615
5689
  };
5616
5690
  }, [routes, notFoundRoute, errorRoute]);
5691
+ const handleNavigate = useCallback(
5692
+ async (nextUrl, options) => {
5693
+ await navigate(nextUrl, handlersRef.current, {
5694
+ revalidate: options?.revalidate
5695
+ });
5696
+ },
5697
+ []
5698
+ );
5699
+ useEffect(() => {
5700
+ if (typeof window !== "undefined") {
5701
+ window[ROUTER_NAVIGATE_KEY] = handleNavigate;
5702
+ return () => {
5703
+ delete window[ROUTER_NAVIGATE_KEY];
5704
+ };
5705
+ }
5706
+ }, [handleNavigate]);
5617
5707
  useEffect(() => {
5618
5708
  let isMounted = true;
5619
- async function handleNavigate(nextUrl, options) {
5709
+ async function handleNavigateInternal(nextUrl, options) {
5620
5710
  if (!isMounted) return;
5621
5711
  await navigate(nextUrl, handlersRef.current, options);
5622
5712
  }
5623
- const handleClick = createClickHandler(handleNavigate);
5624
- const handlePopState = createPopStateHandler(handleNavigate);
5713
+ const handleClick = createClickHandler(handleNavigateInternal);
5714
+ const handlePopState = createPopStateHandler(handleNavigateInternal);
5625
5715
  window.addEventListener("click", handleClick, false);
5626
5716
  window.addEventListener("popstate", handlePopState, false);
5627
5717
  return () => {
@@ -5630,11 +5720,33 @@ function AppShell({
5630
5720
  window.removeEventListener("popstate", handlePopState, false);
5631
5721
  };
5632
5722
  }, []);
5723
+ useEffect(() => {
5724
+ const handleDataRefresh = () => {
5725
+ const freshData = window?.__FW_DATA__;
5726
+ if (!freshData) return;
5727
+ const currentPathname = window.location.pathname;
5728
+ const freshPathname = freshData.pathname;
5729
+ if (freshPathname === currentPathname) {
5730
+ if (freshData.metadata !== void 0) {
5731
+ applyMetadata(freshData.metadata);
5732
+ }
5733
+ setState((prevState) => ({
5734
+ ...prevState,
5735
+ props: freshData.props ?? prevState.props,
5736
+ params: freshData.params ?? prevState.params
5737
+ }));
5738
+ }
5739
+ };
5740
+ window.addEventListener("fw-data-refresh", handleDataRefresh);
5741
+ return () => {
5742
+ window.removeEventListener("fw-data-refresh", handleDataRefresh);
5743
+ };
5744
+ }, [state.url]);
5633
5745
  const isError = state.route === errorRoute;
5634
5746
  const isNotFound = state.route === notFoundRoute;
5635
5747
  const routeType = isError ? "error" : isNotFound ? "notfound" : "normal";
5636
5748
  const routeKey = `${state.url}:${routeType}`;
5637
- return /* @__PURE__ */ jsx2(RouterView, { state }, routeKey);
5749
+ return /* @__PURE__ */ jsx2(RouterContext.Provider, { value: { navigate: handleNavigate }, children: /* @__PURE__ */ jsx2(RouterView, { state }, routeKey) });
5638
5750
  }
5639
5751
 
5640
5752
  // modules/runtime/client/bootstrap.tsx
@@ -5679,6 +5791,11 @@ async function loadInitialRoute(initialUrl, initialData, routes, notFoundRoute,
5679
5791
  };
5680
5792
  }
5681
5793
  function setupHotReload2() {
5794
+ const nodeEnv = typeof process !== "undefined" && process?.env?.NODE_ENV || "production";
5795
+ const isDev = nodeEnv !== "production";
5796
+ if (!isDev) {
5797
+ return;
5798
+ }
5682
5799
  try {
5683
5800
  console.log("[hot-reload] Attempting to connect to /__fw/hot...");
5684
5801
  const eventSource = new EventSource("/__fw/hot");
@@ -5729,6 +5846,16 @@ function bootstrapClient(routes, notFoundRoute, errorRoute = null) {
5729
5846
  return;
5730
5847
  }
5731
5848
  const initialUrl = window.location.pathname + window.location.search;
5849
+ let routerData = getRouterData();
5850
+ if (!routerData) {
5851
+ const url = new URL(initialUrl, window.location.origin);
5852
+ routerData = {
5853
+ pathname: url.pathname,
5854
+ params: initialData?.params || {},
5855
+ searchParams: Object.fromEntries(url.searchParams.entries())
5856
+ };
5857
+ setRouterData(routerData);
5858
+ }
5732
5859
  try {
5733
5860
  const initialState = await loadInitialRoute(
5734
5861
  initialUrl,
@@ -5767,7 +5894,6 @@ function bootstrapClient(routes, notFoundRoute, errorRoute = null) {
5767
5894
  function withCache(fn, options) {
5768
5895
  const ttl = options.ttl ?? 60;
5769
5896
  return async function cachedGssp(ctx) {
5770
- console.log("TTL", ttl);
5771
5897
  return await fn(ctx);
5772
5898
  };
5773
5899
  }