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.
@@ -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
- const res = await fetch(pathKey, {
250
- headers: { 'Accept': 'application/json' },
251
- });
252
- if (res.ok) {
253
- const json = await res.json() as { serverData: Record<string, unknown> };
254
- for (const [k, v] of Object.entries(json.serverData ?? {})) {
255
- clientServerDataCache.set(k, v);
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') {
@@ -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 { context: _, ...restProps } = getFinalProps ? await getFinalProps(props) : props;
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) {