akanjs 3.3.0-rc.47 → 3.3.0-rc.49

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/CHANGELOG.md CHANGED
@@ -600,6 +600,26 @@
600
600
 
601
601
  Nothing to do. A workspace on an older `akanjs` without the `akanjs/client` entry builds as it did.
602
602
 
603
+ - fa2bd6c: fix(akanjs): a CSR page's async layouts and page start their renders together, and a pushed page slides in at once
604
+
605
+ A CSR layer mounted the layer under it only once its own async render had settled, so a page under three async
606
+ layouts made four server round trips in a row before it drew. A stack push waited up to 300ms for that, then slid in
607
+ with its loading fallbacks all the same: a native app's channel screen paused 0.2–0.5 s before it moved.
608
+
609
+ - A render reads only the params and search it is handed, never the output of the layer above, so a layer that mounts
610
+ now starts the async renders under it at once, and each layer takes its run as it mounts. A page draws after one
611
+ round trip, and the same query asked by two levels (`viewOrg` in a layout and its page) is sent once, since the
612
+ client already shares a query still in flight.
613
+ - A run started ahead still waits for the route's guards, so nothing under a refused guard renders. One that failed,
614
+ was refused or redirected before its layer mounted is not taken: the layer renders again on its own, and only a
615
+ failure a layer shows is logged.
616
+ - A sliding page (`transition: "stack"` and the other moving transitions) no longer waits to be drawn: it slides in
617
+ as soon as its frame is ready, showing its `.loading()` fallback until its content lands. A page switched in place
618
+ (a tab, `transition: "none"`) still waits up to 300ms out of sight, as before.
619
+
620
+ **Upgrading.** No code change is needed. A pushed page with no `.loading()` slides in blank until its content lands;
621
+ give a slow page a `.loading()` skeleton.
622
+
603
623
  - 9a3e6b0: In a native app (the CSR runtime), a push that changes only the query stays on its page, and a closing `Dialog.Modal` can play a leave animation.
604
624
 
605
625
  `router.push` or a `Link` to the page already on screen, with only a different query or hash, used to add a copy of that page to the CSR stack: a record opened over a list with `?id=`, a calendar stepped to the next week, a filter changed. Back then showed the same screen again, once per push. Such a push now rewrites the entry it is on, as `router.replace` does, so one back leaves the page. A push to another path stacks as before, and a web page served by the server is unchanged.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.47",
3
+ "version": "3.3.0-rc.49",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -25,6 +25,8 @@ const layerChildContext = createContext<ReactNode>(null);
25
25
  const LayerOutlet = () => useContext(layerChildContext);
26
26
  const layerOutlet = <LayerOutlet />;
27
27
 
28
+ const prestartContext = createContext<Map<number, RenderedRoute> | null>(null);
29
+
28
30
  interface RenderedRoute {
29
31
  render: RouteRender["render"];
30
32
  argsKey: string;
@@ -41,6 +43,10 @@ interface RenderedRoute {
41
43
  /** Asked after the layer's last run was refused, so it draws nothing until its own answer arrives. */
42
44
  afterRefusal: boolean;
43
45
  refreshEpoch: number;
46
+ /** Started ahead of its layer: its failure is logged only once a layer has taken it. */
47
+ owned: boolean;
48
+ onFailure: (() => void) | null;
49
+ prestartedBelow: Map<number, RenderedRoute> | null;
44
50
  }
45
51
 
46
52
  interface RenderLayerProps {
@@ -52,13 +58,8 @@ interface RenderLayerProps {
52
58
  }
53
59
  export const RenderLayer = memo((props: RenderLayerProps) => {
54
60
  const { renders, index, params, searchParams, leaf = null } = props;
55
- const isLast = index >= renders.length - 1;
56
- const children = isLast ? (
57
- leaf
58
- ) : (
59
- <RenderLayer renders={renders} index={index + 1} params={params} searchParams={searchParams} leaf={leaf} />
60
- );
61
61
  const routeRender = renders[index];
62
+ const inherited = useContext(prestartContext);
62
63
  const isAsyncRender = CsrPageReadiness.isAsync(routeRender);
63
64
  const renderedRef = useRef<RenderedRoute | null>(null);
64
65
  const [, setFailures] = useState(0);
@@ -117,13 +118,7 @@ export const RenderLayer = memo((props: RenderLayerProps) => {
117
118
  const drawnPropsRef = useRef<Pick<RenderLayerProps, "params" | "searchParams"> | null>(null);
118
119
  if (isSyncAdmitted) drawnPropsRef.current = { params, searchParams };
119
120
  if (routeRender && (mayAsk || !guarded) && (isAsyncRender || (guarded && !isSyncAdmitted))) {
120
- const argsKey = isAsyncRender
121
- ? RouteDefinition.renderArgsKey(
122
- routeRender.render,
123
- { params, searchParams },
124
- { isPage: routeRender.kind === "page", paramNames: routeRender.paramNames },
125
- )
126
- : (guardKey ?? "");
121
+ const argsKey = isAsyncRender ? argsKeyOf(routeRender, params, searchParams) : (guardKey ?? "");
127
122
  const rendered = renderedRef.current;
128
123
  const returned = activity === "current" && !onScreenRef.current;
129
124
  if (
@@ -137,55 +132,28 @@ export const RenderLayer = memo((props: RenderLayerProps) => {
137
132
  refresh,
138
133
  })
139
134
  ) {
140
- const redirectsBefore = router.redirectCount();
141
- const layer = `${routeRender.kind ?? "layout"} ${index} of ${path}`;
142
- let refusedAt: string | null = null;
143
- let admittedKey: string | null = null;
144
- const draw = () =>
145
- isAsyncRender
146
- ? Translator.renderAt(
147
- renderPath,
148
- () => routeRender.render({ children: layerOutlet, params, searchParams } as never) ?? null,
149
- )
150
- : true;
151
- const result = reportFailure(
152
- guarded
153
- ? CsrGuards.of(guarded).then((admitted) => {
154
- if (admitted) admittedKey = guardKey;
155
- else refusedAt = locationId;
156
- return admitted ? draw() : null;
157
- })
158
- : draw(),
159
- layer,
160
- );
161
- const next: RenderedRoute = {
162
- render: routeRender.render,
163
- argsKey,
164
- result,
165
- record: isThenable(result) ? CsrFrameDump.track(layer, result as Promise<ReactNode>) : null,
166
- redirected: false,
167
- refusedAt: null,
168
- admittedKey: null,
169
- failure: null,
170
- afterRefusal: !!rendered?.refusedAt,
171
- refreshEpoch: refresh.epoch,
135
+ const input: LayerRunInput = {
136
+ ...{ routeRender, index, params, searchParams, argsKey, isAsyncRender },
137
+ ...{ guarded, guardKey, locationId, renderPath, path, refreshEpoch: refresh.epoch },
172
138
  };
173
- if (isThenable(result))
174
- (result as Promise<ReactNode>).then(
175
- () => {
176
- next.refusedAt = refusedAt;
177
- next.admittedKey = admittedKey;
178
- next.redirected = !!refusedAt || router.redirectCount() !== redirectsBefore;
179
- },
180
- (error: unknown) => {
181
- if (router.redirectCount() !== redirectsBefore || renderedRef.current !== next) return;
182
- next.failure = { error };
183
- setFailures((count) => count + 1);
184
- },
185
- );
139
+ const next = takePrestart(inherited, input) ?? startRun(input, true);
140
+ next.afterRefusal = !!rendered?.refusedAt;
141
+ next.onFailure = () => {
142
+ if (renderedRef.current === next) setFailures((count) => count + 1);
143
+ };
144
+ if (!rendered) next.prestartedBelow = prestartBelow(renders, input, inherited);
186
145
  renderedRef.current = next;
187
146
  }
188
147
  }
148
+ const handedDown = renderedRef.current?.prestartedBelow ?? inherited;
149
+ const children =
150
+ index >= renders.length - 1 ? (
151
+ leaf
152
+ ) : (
153
+ <prestartContext.Provider value={handedDown}>
154
+ <RenderLayer renders={renders} index={index + 1} params={params} searchParams={searchParams} leaf={leaf} />
155
+ </prestartContext.Provider>
156
+ );
189
157
  const { value, fulfilled } = useFetch(renderedRef.current?.result ?? null, { keepPrevious: true });
190
158
  const record = renderedRef.current?.record ?? null;
191
159
  if (record && fulfilled) record.delivered = true;
@@ -220,6 +188,118 @@ export const RenderLayer = memo((props: RenderLayerProps) => {
220
188
  return <layerChildContext.Provider value={children}>{value}</layerChildContext.Provider>;
221
189
  });
222
190
 
191
+ interface LayerRunInput {
192
+ routeRender: RouteRender;
193
+ index: number;
194
+ params: Record<string, string>;
195
+ searchParams: Record<string, string | string[]>;
196
+ argsKey: string;
197
+ isAsyncRender: boolean;
198
+ guarded: Parameters<typeof CsrGuards.of>[0] | null;
199
+ guardKey: string | null;
200
+ locationId: string | null;
201
+ renderPath: string | undefined;
202
+ path: string;
203
+ refreshEpoch: number;
204
+ }
205
+
206
+ function argsKeyOf(
207
+ routeRender: RouteRender,
208
+ params: Record<string, string>,
209
+ searchParams: Record<string, string | string[]>,
210
+ ): string {
211
+ return RouteDefinition.renderArgsKey(
212
+ routeRender.render,
213
+ { params, searchParams },
214
+ { isPage: routeRender.kind === "page", paramNames: routeRender.paramNames },
215
+ );
216
+ }
217
+
218
+ function startRun(input: LayerRunInput, owned: boolean): RenderedRoute {
219
+ const { routeRender, index, params, searchParams, isAsyncRender, guarded, guardKey, locationId } = input;
220
+ const redirectsBefore = router.redirectCount();
221
+ const layer = `${routeRender.kind ?? "layout"} ${index} of ${input.path}`;
222
+ let refusedAt: string | null = null;
223
+ let admittedKey: string | null = null;
224
+ const draw = () =>
225
+ isAsyncRender
226
+ ? Translator.renderAt(
227
+ input.renderPath,
228
+ () => routeRender.render({ children: layerOutlet, params, searchParams } as never) ?? null,
229
+ )
230
+ : true;
231
+ const result = traceRender(
232
+ guarded
233
+ ? CsrGuards.of(guarded).then((admitted) => {
234
+ if (admitted) admittedKey = guardKey;
235
+ else refusedAt = locationId;
236
+ return admitted ? draw() : null;
237
+ })
238
+ : draw(),
239
+ layer,
240
+ );
241
+ const run: RenderedRoute = {
242
+ render: routeRender.render,
243
+ argsKey: input.argsKey,
244
+ result,
245
+ record: isThenable(result) ? CsrFrameDump.track(layer, result as Promise<ReactNode>) : null,
246
+ redirected: false,
247
+ refusedAt: null,
248
+ admittedKey: null,
249
+ failure: null,
250
+ afterRefusal: false,
251
+ refreshEpoch: input.refreshEpoch,
252
+ owned,
253
+ onFailure: null,
254
+ prestartedBelow: null,
255
+ };
256
+ if (isThenable(result))
257
+ (result as Promise<ReactNode>).then(
258
+ () => {
259
+ run.refusedAt = refusedAt;
260
+ run.admittedKey = admittedKey;
261
+ run.redirected = !!refusedAt || router.redirectCount() !== redirectsBefore;
262
+ },
263
+ (error: unknown) => {
264
+
265
+ if (run.owned)
266
+ Logger.error(
267
+ `render of ${layer} failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
268
+ );
269
+ if (router.redirectCount() !== redirectsBefore) return;
270
+ run.failure = { error };
271
+ run.onFailure?.();
272
+ },
273
+ );
274
+ return run;
275
+ }
276
+
277
+ function takePrestart(inherited: Map<number, RenderedRoute> | null, input: LayerRunInput): RenderedRoute | null {
278
+ const run = inherited?.get(input.index);
279
+ if (!run) return null;
280
+ inherited?.delete(input.index);
281
+ const isSame =
282
+ run.render === input.routeRender.render && run.argsKey === input.argsKey && run.refreshEpoch === input.refreshEpoch;
283
+ if (!isSame || run.refusedAt || run.redirected || run.failure) return null;
284
+ run.owned = true;
285
+ return run;
286
+ }
287
+
288
+ function prestartBelow(
289
+ renders: RouteRender[],
290
+ input: LayerRunInput,
291
+ inherited: Map<number, RenderedRoute> | null,
292
+ ): Map<number, RenderedRoute> | null {
293
+ const below = new Map(inherited);
294
+ for (let index = input.index + 1; index < renders.length; index++) {
295
+ const routeRender = renders[index];
296
+ if (!routeRender || !CsrPageReadiness.isAsync(routeRender) || below.has(index)) continue;
297
+ const argsKey = argsKeyOf(routeRender, input.params, input.searchParams);
298
+ below.set(index, startRun({ ...input, routeRender, index, argsKey, isAsyncRender: true }, false));
299
+ }
300
+ return below.size ? below : inherited;
301
+ }
302
+
223
303
  function isRefusedAt(rendered: RenderedRoute | null, locationId: string | null, fulfilled: boolean): boolean {
224
304
  return (!!locationId && rendered?.refusedAt === locationId) || (!!rendered?.afterRefusal && !fulfilled);
225
305
  }
@@ -255,17 +335,13 @@ function mustRerun(rendered: RenderedRoute | null, run: LayerRun): boolean {
255
335
  return run.locationId === null || run.refresh.locationId === run.locationId;
256
336
  }
257
337
 
258
- function reportFailure(result: ReactNode | Promise<ReactNode>, layer: string): ReactNode | Promise<ReactNode> {
338
+ function traceRender(result: ReactNode | Promise<ReactNode>, layer: string): ReactNode | Promise<ReactNode> {
259
339
  if (!isThenable(result)) return result;
260
340
  debugFrame("layer.render", { layer });
261
341
  (result as Promise<ReactNode>).then(
262
342
  () => debugFrame("layer.settle", { layer }),
263
- (error: unknown) => {
264
- debugFrame("layer.fail", { layer, message: error instanceof Error ? error.message : String(error) });
265
- Logger.error(
266
- `render of ${layer} failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
267
- );
268
- },
343
+ (error: unknown) =>
344
+ debugFrame("layer.fail", { layer, message: error instanceof Error ? error.message : String(error) }),
269
345
  );
270
346
  return result;
271
347
  }
@@ -142,11 +142,7 @@ const commitWhenFrameReady = ({
142
142
  pendingFrameSlots: latestPendingFrameSlots.current,
143
143
  elapsedMs,
144
144
  });
145
- const isDrawn =
146
- !CsrPageReadiness.drawsLater(toLocation) ||
147
- CsrPageReadiness.isDrawn(toLocation.entryId) ||
148
- elapsedMs >= PAGE_DRAWN_TIMEOUT_MS;
149
- if (isFrameReady && isDrawn) {
145
+ if (isFrameReady) {
150
146
  void commitNavigation(elapsedMs >= PENDING_FRAME_READY_TIMEOUT_MS);
151
147
  return;
152
148
  }