@7365admin1/layer-common 4.2.23 → 4.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @iservice365/layer-common
2
2
 
3
+ ## 4.2.24
4
+
5
+ ### Patch Changes
6
+
7
+ - ce5231a: Stop the app root waiting on two API round trips before it can paint, and start the third one alongside them.
8
+
9
+ `secure-member.client.ts` awaited the member lookup and then, serially, the role document inside `router.beforeEach`, so nothing painted on any route -- not even the neutral "signing you in" hold -- until both had come back. The app root renders no permission-gated surface, so it paid for a gate it does not have. On the root only (`to.name === "index"` and no `memberOnly`) the resolve is now started rather than awaited; every other route, and a root that does carry `memberOnly`, awaits exactly as before, and a route reached while the resolve is still in flight awaits that same promise.
10
+
11
+ `useLocalAuth.getCurrentUser` was the third round trip and it started last, because page setup only runs after the guard returns. The guard now primes it once per page load in parallel with the member lookup, and `getCurrentUser` collapses concurrent callers onto one request so the page asking again costs nothing. `authenticate()` goes through the same request instead of its own `useLazyAsyncData` copy; with no `user` cookie it takes the redirect-to-index branch directly rather than after a request that could only fail.
12
+
3
13
  ## 4.2.23
4
14
 
5
15
  ### Patch Changes
@@ -755,11 +765,11 @@
755
765
  `utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
756
766
  own rule from two endpoints the console already calls, unprojected:
757
767
 
758
- GET /api/members/user/:user/app/admin the Seven365 staff membership
759
- GET /api/roles/id/:role that membership's role document
768
+ GET /api/members/user/:user/app/admin the Seven365 staff membership
769
+ GET /api/roles/id/:role that membership's role document
760
770
 
761
- owner = member.type === "admin" && role.type === "admin" && role.default === true
762
- staff = member.type === "admin" && role.type === "admin"
771
+ owner = member.type === "admin" && role.type === "admin" && role.default === true
772
+ staff = member.type === "admin" && role.type === "admin"
763
773
 
764
774
  `role.default` is the marker because it is the only property of a platform
765
775
  staff role no API caller can set - `role.controller.ts` validates create and
@@ -1,25 +1,36 @@
1
+ // One in-flight `GET /api/users/id/:id` per page load, shared by every caller.
2
+ // These apps are `ssr: false`, so module scope is exactly one browser session.
3
+ // The route guard starts this request in parallel with the member lookup and
4
+ // the first screen asks for it again on setup; without this that is two
5
+ // identical requests instead of one.
6
+ let currentUserRequest: Promise<any> | null = null;
7
+
1
8
  export default function useLocalAuth() {
2
9
  const { cookieConfig } = useRuntimeConfig().public;
3
10
 
4
11
  const currentUser = useState<TUser | null>("currentUser", () => null);
5
12
 
13
+ // Goes through the same request `getCurrentUser` makes, so it joins the one
14
+ // the route guard already started instead of issuing a second.
15
+ //
16
+ // DELIBERATELY NOT AWAITED INSIDE. 19 pages call this with a top-level
17
+ // `await`, and the `useLazyAsyncData` it replaces returned before its request
18
+ // did; awaiting the request here would newly suspend every one of them.
6
19
  async function authenticate() {
7
20
  const user = useCookie("user", cookieConfig).value;
8
21
 
9
- const { data: getCurrentUserReq, error: getCurrentUserErr } =
10
- await useLazyAsyncData("getCurrentUser", () =>
11
- useNuxtApp().$api<TUser>(`/api/users/id/${user}`)
12
- );
13
-
14
- watchEffect(() => {
15
- currentUser.value = (getCurrentUserReq.value as TUser | null) ?? null;
16
- });
22
+ // No cookie used to mean a request for `/api/users/id/undefined` that could
23
+ // only fail. Same destination, one round trip earlier.
24
+ if (!user) {
25
+ currentUser.value = null;
26
+ navigateTo({ name: "index" });
27
+ return;
28
+ }
17
29
 
18
- watchEffect(() => {
19
- if (getCurrentUserErr.value) {
20
- // Redirect to login page if user authentication fails
21
- navigateTo({ name: "index" });
22
- }
30
+ getCurrentUser().catch(() => {
31
+ currentUser.value = null;
32
+ // Redirect to login page if user authentication fails
33
+ navigateTo({ name: "index" });
23
34
  });
24
35
  }
25
36
 
@@ -63,9 +74,16 @@ export default function useLocalAuth() {
63
74
  currentUser.value = null;
64
75
  return null;
65
76
  }
66
- const _user = await useNuxtApp().$api<TUser>(`/api/users/id/${user}`, {
67
- method: "GET",
68
- });
77
+ if (!currentUserRequest) {
78
+ currentUserRequest = useNuxtApp()
79
+ .$api<TUser>(`/api/users/id/${user}`, { method: "GET" })
80
+ .finally(() => {
81
+ // Cleared on success AND on failure: this collapses callers that
82
+ // overlap, it does not cache the answer.
83
+ currentUserRequest = null;
84
+ });
85
+ }
86
+ const _user = (await currentUserRequest) as TUser;
69
87
  currentUser.value = _user;
70
88
  return _user;
71
89
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.2.23",
5
+ "version": "4.2.24",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
@@ -13,6 +13,7 @@ export default defineNuxtPlugin(() => {
13
13
  const { getByUserType } = useMember();
14
14
  const { getRoleById } = useRole();
15
15
  const { getById } = useOrg();
16
+ const { getCurrentUser } = useLocalAuth();
16
17
 
17
18
  const { userAppRole, userAppRoleStatus, id, orgNature } = useLocalSetup();
18
19
 
@@ -21,6 +22,7 @@ export default defineNuxtPlugin(() => {
21
22
  let resolvedFor: string | null = null;
22
23
  let inFlight: Promise<void> | null = null;
23
24
  let hasMemberDocument = false;
25
+ let primedUser = false;
24
26
 
25
27
  // Deliberately never redirects and never throws.
26
28
  //
@@ -68,13 +70,40 @@ export default defineNuxtPlugin(() => {
68
70
  router.beforeEach(async (to) => {
69
71
  const userId = useCookie("user").value ?? "";
70
72
 
73
+ // The signed-in user's own record is a THIRD round trip and almost every
74
+ // first screen wants it, but page setup only runs once the guard below has
75
+ // returned -- so it used to start after both lookups had finished. Started
76
+ // here it runs alongside them. `getCurrentUser` collapses concurrent
77
+ // callers onto one request, so the page asking for it again costs nothing.
78
+ if (userId && !primedUser) {
79
+ primedUser = true;
80
+ void getCurrentUser().catch(() => {
81
+ // A failed prime is not this guard's business: every caller that needs
82
+ // the user asks again and handles its own failure.
83
+ });
84
+ }
85
+
71
86
  // Resolve for EVERY authenticated route, not only `memberOnly` ones. Gates
72
87
  // are read on pages that carry no such flag -- the staff console cannot
73
88
  // carry it at all, it has no organisation in the route -- and until this
74
89
  // ran there the role stayed null for the life of the page: gates that
75
90
  // default open granted everything, gates that fail closed denied everything.
76
91
  // Awaited, so no gate is ever evaluated against an unresolved role.
77
- if (userId) await resolveAppRole(userId);
92
+ //
93
+ // The ONE exception is the app root. It renders no permission-gated
94
+ // surface -- it reads the signed-in email, and leaves -- so waiting two
95
+ // serial round trips there delays the first paint of every app by the
96
+ // length of both and buys nothing. Started, not awaited. Every other
97
+ // route, root-or-not, still awaits, and a route reached while this is
98
+ // still in flight awaits THIS promise: `resolveAppRole` returns the
99
+ // in-flight one rather than starting a second.
100
+ if (userId) {
101
+ if (to.name === "index" && !to.meta?.memberOnly) {
102
+ void resolveAppRole(userId);
103
+ } else {
104
+ await resolveAppRole(userId);
105
+ }
106
+ }
78
107
 
79
108
  const isMember = to.meta?.memberOnly;
80
109