hadars 0.2.1 → 0.2.2-rc.0
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 +177 -14
- package/cli-lib.ts +97 -5
- package/dist/{chunk-HWOLYLPF.js → chunk-H72BZXOA.js} +2 -2
- package/dist/cli.js +519 -16
- package/dist/cloudflare.cjs +2 -2
- package/dist/cloudflare.d.cts +1 -1
- package/dist/cloudflare.d.ts +1 -1
- package/dist/cloudflare.js +1 -1
- package/dist/{hadars-DEBSYAQl.d.cts → hadars-mKu5txjW.d.cts} +93 -2
- package/dist/{hadars-DEBSYAQl.d.ts → hadars-mKu5txjW.d.ts} +93 -2
- package/dist/index.cjs +11 -8
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -8
- package/dist/lambda.cjs +2 -2
- package/dist/lambda.d.cts +1 -1
- package/dist/lambda.d.ts +1 -1
- package/dist/lambda.js +1 -1
- package/dist/utils/Head.tsx +17 -8
- package/dist/utils/clientScript.tsx +9 -0
- package/index.ts +3 -0
- package/package.json +1 -1
- package/src/build.ts +29 -0
- package/src/index.tsx +3 -0
- package/src/source/context.ts +113 -0
- package/src/source/graphiql.ts +96 -0
- package/src/source/inference.ts +188 -0
- package/src/source/runner.ts +114 -0
- package/src/source/store.ts +48 -0
- package/src/static.ts +106 -0
- package/src/types/hadars.ts +91 -1
- package/src/utils/Head.tsx +17 -8
- package/src/utils/clientScript.tsx +9 -0
- package/src/utils/response.tsx +4 -3
package/src/utils/Head.tsx
CHANGED
|
@@ -246,14 +246,23 @@ export function useServerData<T>(key: string | string[], fn: () => Promise<T> |
|
|
|
246
246
|
// render is registered against the same deferred before the fetch starts.
|
|
247
247
|
queueMicrotask(async () => {
|
|
248
248
|
try {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
249
|
+
let json: { serverData: Record<string, unknown> } | null = null;
|
|
250
|
+
|
|
251
|
+
if ((globalThis as any).__hadarsStatic) {
|
|
252
|
+
// Static export: the __hadarsStatic flag was embedded in the
|
|
253
|
+
// page by `hadars export static`. Fetch the pre-generated
|
|
254
|
+
// index.json sidecar directly — no live SSR server exists.
|
|
255
|
+
const sidecarUrl = pathKey.replace(/\/$/, '') + '/index.json';
|
|
256
|
+
const res = await fetch(sidecarUrl).catch(() => null);
|
|
257
|
+
if (res?.ok) json = await res.json().catch(() => null);
|
|
258
|
+
} else {
|
|
259
|
+
// Live server: request the current URL with Accept: application/json.
|
|
260
|
+
const res = await fetch(pathKey, { headers: { 'Accept': 'application/json' } });
|
|
261
|
+
if (res.ok) json = await res.json();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
for (const [k, v] of Object.entries(json?.serverData ?? {})) {
|
|
265
|
+
clientServerDataCache.set(k, v);
|
|
257
266
|
}
|
|
258
267
|
} finally {
|
|
259
268
|
fetchedPaths.add(pathKey);
|
|
@@ -22,6 +22,15 @@ const getProps = () => {
|
|
|
22
22
|
const main = async () => {
|
|
23
23
|
let props = getProps();
|
|
24
24
|
|
|
25
|
+
// Extract the static-export flag before it reaches user code. When set,
|
|
26
|
+
// useServerData fetches index.json sidecars directly on client navigation
|
|
27
|
+
// instead of requesting the live SSR server with Accept: application/json.
|
|
28
|
+
if ((props as any).__hadarsStatic) {
|
|
29
|
+
(globalThis as any).__hadarsStatic = true;
|
|
30
|
+
const { __hadarsStatic: _, ...rest } = props as any;
|
|
31
|
+
props = rest;
|
|
32
|
+
}
|
|
33
|
+
|
|
25
34
|
// Seed the useServerData client cache from server-resolved values before
|
|
26
35
|
// hydration so that hooks return the same data on the first render.
|
|
27
36
|
if (props.__serverData && typeof props.__serverData === 'object') {
|
package/src/utils/response.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
|
-
import type { AppHead, HadarsRequest, HadarsEntryBase, HadarsEntryModule, HadarsProps, AppContext } from "../types/hadars";
|
|
2
|
+
import type { AppHead, HadarsRequest, HadarsEntryBase, HadarsEntryModule, HadarsProps, AppContext, HadarsStaticContext } from "../types/hadars";
|
|
3
3
|
import { renderToString, renderPreflight, createElement } from '../slim-react/index';
|
|
4
4
|
|
|
5
5
|
interface ReactResponseOptions {
|
|
@@ -10,6 +10,7 @@ interface ReactResponseOptions {
|
|
|
10
10
|
getInitProps: HadarsEntryModule<HadarsEntryBase>['getInitProps'];
|
|
11
11
|
getFinalProps: HadarsEntryModule<HadarsEntryBase>['getFinalProps'];
|
|
12
12
|
}
|
|
13
|
+
staticCtx?: HadarsStaticContext;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
// ── Head HTML serialisation ────────────────────────────────────────────────
|
|
@@ -71,7 +72,7 @@ export const getReactResponse = async (
|
|
|
71
72
|
};
|
|
72
73
|
|
|
73
74
|
let props: HadarsEntryBase = {
|
|
74
|
-
...(getInitProps ? await getInitProps(req) : {}),
|
|
75
|
+
...(getInitProps ? await getInitProps(req, opts.staticCtx) : {}),
|
|
75
76
|
location: req.location,
|
|
76
77
|
context,
|
|
77
78
|
} as HadarsEntryBase;
|
|
@@ -118,7 +119,7 @@ export const getReactResponse = async (
|
|
|
118
119
|
};
|
|
119
120
|
|
|
120
121
|
const finalize = async (): Promise<{ clientProps: Record<string, unknown> }> => {
|
|
121
|
-
const
|
|
122
|
+
const restProps = getFinalProps ? await getFinalProps(props) : props;
|
|
122
123
|
const serverData: Record<string, unknown> = {};
|
|
123
124
|
let hasServerData = false;
|
|
124
125
|
for (const [key, entry] of unsuspend.cache) {
|