akanjs 3.3.0-rc.22 → 3.3.0-rc.24

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.
@@ -8,8 +8,11 @@ import {
8
8
  RequestLocale,
9
9
  } from "akanjs/common";
10
10
  import { untrackedHeaders, untrackedRequest } from "akanjs/fetch";
11
- import type { ReactNode } from "react";
11
+ import * as React from "react";
12
+ import { type Context, type ReactNode, use } from "react";
12
13
  import { csrLocation } from "./csrLocation";
14
+ import type { PathContextType } from "./csrTypes";
15
+ import { sharedContextKey } from "./sharedContextKey";
13
16
  import { Translator } from "./translator";
14
17
 
15
18
  type TransMessage<Locale extends Record<string, unknown>> = {
@@ -52,6 +55,23 @@ const escapeRichParams = (param: { [key: string]: string | number } | undefined)
52
55
  ]),
53
56
  );
54
57
 
58
+ const reactInternals = (
59
+ React as unknown as { __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE?: { H: unknown } }
60
+ ).__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
61
+
62
+ const entryPathname = (): string | undefined => {
63
+ const context = (globalThis as unknown as { [slot: symbol]: Context<PathContextType> | undefined })[
64
+ sharedContextKey("path")
65
+ ];
66
+ if (!context || !reactInternals?.H) return undefined;
67
+ try {
68
+ const { location, followsRouter } = use(context);
69
+ return followsRouter ? undefined : location?.pathname;
70
+ } catch {
71
+ return undefined;
72
+ }
73
+ };
74
+
55
75
  const getPageInfo = (): { locale: string; path: string } => {
56
76
  const { defaultLocale, locales } = parseAkanI18nEnv();
57
77
  const localeSet = new Set(locales);
@@ -59,7 +79,7 @@ const getPageInfo = (): { locale: string; path: string } => {
59
79
  const activePath = Translator.getActivePath();
60
80
  if (activePath) return { locale: activeLocale ?? defaultLocale, path: activePath };
61
81
  if (getEnv().side !== "server") {
62
- const pathname = Translator.getRenderPath() ?? csrLocation.pathname;
82
+ const pathname = Translator.getRenderPath() ?? entryPathname() ?? csrLocation.pathname;
63
83
  const [, firstSegment = "", ...rest] = pathname.split("/");
64
84
  const hasLocalePrefix = localeSet.has(firstSegment);
65
85
 
@@ -1,10 +1,12 @@
1
1
  "use client";
2
2
  import { type Context, createContext } from "react";
3
3
 
4
+ import { sharedContextKey } from "./sharedContextKey";
5
+
4
6
  /** Every framework context is made here: a build inlines modules into each client chunk, so a plain `createContext`
5
7
  * copy silently misses a Provider from another chunk. `use-agentic` keeps its own `useAgentic.`-prefixed twin. */
6
8
  export const sharedContext = <T>(name: string, initial: T): Context<T> => {
7
- const key = Symbol.for(`akanjs.context.${name}`);
9
+ const key = sharedContextKey(name);
8
10
  const holder = globalThis as typeof globalThis & { [slot: symbol]: Context<T> | undefined };
9
11
  const existing = holder[key];
10
12
  if (existing) return existing;
@@ -0,0 +1,2 @@
1
+ /** Where `sharedContext` keeps a framework context on `globalThis`, readable by a module the server loads too. */
2
+ export const sharedContextKey = (name: string) => Symbol.for(`akanjs.context.${name}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.22",
3
+ "version": "3.3.0-rc.24",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -1,5 +1,5 @@
1
1
  import { type ErrConstructor, type ErrPayload, type ErrRestoreOption } from "akanjs/common";
2
- import type { ReactNode } from "react";
2
+ import { type ReactNode } from "react";
3
3
  export type { ErrPayload } from "akanjs/common";
4
4
  /** @deprecated `Err` from `akanjs/common`, the class `makePageProto`'s `Err` is. */
5
5
  export interface PageErrInstance extends Error {
@@ -0,0 +1,2 @@
1
+ /** Where `sharedContext` keeps a framework context on `globalThis`, readable by a module the server loads too. */
2
+ export declare const sharedContextKey: (name: string) => symbol;
@@ -34,6 +34,8 @@ export declare class NativeNavigation {
34
34
  openDeepLink(url: string): void;
35
35
  openPushLink(url: unknown): void;
36
36
  back(): void;
37
+ /** The frame's word that a move was sent back to the page on screen before it showed, as a refusing guard does. */
38
+ static turnedBack(): void;
37
39
  static isMacBackKey({ key, metaKey, altKey, ctrlKey, shiftKey, }: Pick<KeyboardEvent, "key" | "metaKey" | "altKey" | "ctrlKey" | "shiftKey">): boolean;
38
40
  }
39
41
  export {};
@@ -27,6 +27,9 @@ interface NativeNavigationOptions {
27
27
 
28
28
  /** A native shell's deep links, push taps and Android back button, for the CSR frame that owns the history. */
29
29
  export class NativeNavigation {
30
+
31
+ static #fallingBack: { navigation: NativeNavigation; path: string; navigations: number; redirects: number } | null =
32
+ null;
30
33
  #mountedAt = Date.now();
31
34
  #handled: { href: string; handledAt: number } | null = null;
32
35
  #didResetStack = false;
@@ -95,6 +98,7 @@ export class NativeNavigation {
95
98
  * is history, or the index is still to come. Otherwise the system takes it and shows its own back-to-home animation.
96
99
  */
97
100
  syncBack(force = false) {
101
+ this.#leaveIfSentBack();
98
102
  if (!isNativeApp() || !app.isSupported("setBackEnabled")) return;
99
103
  const backState = this.options.backState();
100
104
  const enabled =
@@ -155,6 +159,31 @@ export class NativeNavigation {
155
159
  return;
156
160
  }
157
161
  clientRouter.backOrFallback(window.__AKAN_MOBILE_TARGET__?.indexPath ?? "/", { scrollToTop: false });
162
+ NativeNavigation.#fallingBack = {
163
+ navigation: this,
164
+ path: backState.path,
165
+ navigations: clientRouter.navigationCount(),
166
+ redirects: clientRouter.redirectCount(),
167
+ };
168
+ }
169
+
170
+ /** The frame's word that a move was sent back to the page on screen before it showed, as a refusing guard does. */
171
+ static turnedBack() {
172
+ const fallingBack = NativeNavigation.#fallingBack;
173
+ if (fallingBack) fallingBack.navigation.#leaveIfSentBack();
174
+ }
175
+
176
+ #leaveIfSentBack() {
177
+ const fallingBack = NativeNavigation.#fallingBack;
178
+ if (fallingBack?.navigation !== this) return;
179
+ const redirects = clientRouter.redirectCount() - fallingBack.redirects;
180
+ if (clientRouter.navigationCount() - fallingBack.navigations !== redirects) {
181
+ NativeNavigation.#fallingBack = null;
182
+ return;
183
+ }
184
+ if (!redirects || this.options.historyIdx() > 0 || this.options.backState().path !== fallingBack.path) return;
185
+ NativeNavigation.#fallingBack = null;
186
+ void app.exit().catch(() => undefined);
158
187
  }
159
188
 
160
189
  #leavesApp(path: string) {
@@ -18,6 +18,7 @@ import {
18
18
  import { isNativeApp, openExternalUrl } from "akanjs/client/native";
19
19
  import { useCallback, useMemo, useState } from "react";
20
20
  import { CsrRefresh } from "./csrRefresh";
21
+ import { NativeNavigation } from "./nativeNavigation";
21
22
  import { useCsrKeyboard } from "./useCsrKeyboard";
22
23
  import { type CsrLocationState, useCsrLocationState } from "./useCsrLocationState";
23
24
  import { type CsrNavigation, useCsrNavigation } from "./useCsrNavigation";
@@ -66,6 +67,7 @@ const useCsrRouter = ({
66
67
 
67
68
  const cancelled: NavigationIntent | null = cancelPendingNavigation();
68
69
  if (location.href === href) {
70
+ if (cancelled) NativeNavigation.turnedBack();
69
71
  if (!pageContentRef.current) return;
70
72
  pageContentRef.current.scrollTop = getScrollTop(location);
71
73
  return;