hadars 0.2.0 → 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.
@@ -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;
@@ -82,6 +83,9 @@ export const getReactResponse = async (
82
83
  // await continuations even when concurrent requests are in flight.
83
84
  const unsuspend = { cache: new Map<string, any>() };
84
85
  (globalThis as any).__hadarsUnsuspend = unsuspend;
86
+ // Expose the head context so HadarsHead can write into it without needing
87
+ // the user to manually wrap their App with HadarsContext.
88
+ (globalThis as any).__hadarsContext = context;
85
89
 
86
90
  const element = createElement(App as any, props as any);
87
91
 
@@ -94,6 +98,7 @@ export const getReactResponse = async (
94
98
  // Clear the global immediately — the closure-captured `unsuspend`
95
99
  // keeps the cache alive. Re-set inside getAppBody() for the second pass.
96
100
  (globalThis as any).__hadarsUnsuspend = null;
101
+ (globalThis as any).__hadarsContext = null;
97
102
  }
98
103
 
99
104
  // Head is fully populated — status is known.
@@ -104,15 +109,17 @@ export const getReactResponse = async (
104
109
  // (no async waits). The caller flushes head BEFORE calling this.
105
110
  const getAppBody = async (): Promise<string> => {
106
111
  (globalThis as any).__hadarsUnsuspend = unsuspend;
112
+ (globalThis as any).__hadarsContext = context;
107
113
  try {
108
114
  return await renderToString(element);
109
115
  } finally {
110
116
  (globalThis as any).__hadarsUnsuspend = null;
117
+ (globalThis as any).__hadarsContext = null;
111
118
  }
112
119
  };
113
120
 
114
121
  const finalize = async (): Promise<{ clientProps: Record<string, unknown> }> => {
115
- const { context: _, ...restProps } = getFinalProps ? await getFinalProps(props) : props;
122
+ const restProps = getFinalProps ? await getFinalProps(props) : props;
116
123
  const serverData: Record<string, unknown> = {};
117
124
  let hasServerData = false;
118
125
  for (const [key, entry] of unsuspend.cache) {