@imtbl/auth-nextjs 2.12.5-alpha.1 → 2.12.5-alpha.2

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.
@@ -265,19 +265,38 @@ function useAccessToken() {
265
265
  return getAccessToken;
266
266
  }
267
267
  function useHydratedData(props, fetcher) {
268
- const { getAccessToken } = useImmutableAuth();
268
+ const { getAccessToken, auth } = useImmutableAuth();
269
269
  const {
270
270
  session,
271
271
  ssr,
272
272
  data: serverData,
273
273
  fetchError
274
274
  } = props;
275
- const needsClientFetch = !ssr || Boolean(fetchError) || serverData === null;
275
+ const needsClientFetch = !ssr || Boolean(fetchError);
276
276
  const [data, setData] = (0, import_react.useState)(serverData);
277
277
  const [isLoading, setIsLoading] = (0, import_react.useState)(needsClientFetch);
278
278
  const [error, setError] = (0, import_react.useState)(
279
279
  fetchError ? new Error(fetchError) : null
280
280
  );
281
+ const hasFetchedRef = (0, import_react.useRef)(false);
282
+ const prevPropsRef = (0, import_react.useRef)({ serverData, ssr, fetchError });
283
+ (0, import_react.useEffect)(() => {
284
+ const prevProps = prevPropsRef.current;
285
+ const propsChanged = prevProps.serverData !== serverData || prevProps.ssr !== ssr || prevProps.fetchError !== fetchError;
286
+ if (propsChanged) {
287
+ prevPropsRef.current = { serverData, ssr, fetchError };
288
+ hasFetchedRef.current = false;
289
+ if (ssr && !fetchError) {
290
+ setData(serverData);
291
+ setIsLoading(false);
292
+ setError(null);
293
+ } else {
294
+ setData(null);
295
+ setIsLoading(true);
296
+ setError(fetchError ? new Error(fetchError) : null);
297
+ }
298
+ }
299
+ }, [serverData, ssr, fetchError]);
281
300
  const fetchData = (0, import_react.useCallback)(async () => {
282
301
  setIsLoading(true);
283
302
  setError(null);
@@ -297,10 +316,12 @@ function useHydratedData(props, fetcher) {
297
316
  }
298
317
  }, [session, ssr, fetcher, getAccessToken]);
299
318
  (0, import_react.useEffect)(() => {
300
- if (needsClientFetch) {
301
- fetchData();
302
- }
303
- }, []);
319
+ if (hasFetchedRef.current) return;
320
+ if (!needsClientFetch) return;
321
+ if (!ssr && !auth) return;
322
+ hasFetchedRef.current = true;
323
+ fetchData();
324
+ }, [needsClientFetch, ssr, auth, fetchData]);
304
325
  return {
305
326
  data,
306
327
  isLoading,
@@ -251,19 +251,38 @@ function useAccessToken() {
251
251
  return getAccessToken;
252
252
  }
253
253
  function useHydratedData(props, fetcher) {
254
- const { getAccessToken } = useImmutableAuth();
254
+ const { getAccessToken, auth } = useImmutableAuth();
255
255
  const {
256
256
  session,
257
257
  ssr,
258
258
  data: serverData,
259
259
  fetchError
260
260
  } = props;
261
- const needsClientFetch = !ssr || Boolean(fetchError) || serverData === null;
261
+ const needsClientFetch = !ssr || Boolean(fetchError);
262
262
  const [data, setData] = useState(serverData);
263
263
  const [isLoading, setIsLoading] = useState(needsClientFetch);
264
264
  const [error, setError] = useState(
265
265
  fetchError ? new Error(fetchError) : null
266
266
  );
267
+ const hasFetchedRef = useRef(false);
268
+ const prevPropsRef = useRef({ serverData, ssr, fetchError });
269
+ useEffect(() => {
270
+ const prevProps = prevPropsRef.current;
271
+ const propsChanged = prevProps.serverData !== serverData || prevProps.ssr !== ssr || prevProps.fetchError !== fetchError;
272
+ if (propsChanged) {
273
+ prevPropsRef.current = { serverData, ssr, fetchError };
274
+ hasFetchedRef.current = false;
275
+ if (ssr && !fetchError) {
276
+ setData(serverData);
277
+ setIsLoading(false);
278
+ setError(null);
279
+ } else {
280
+ setData(null);
281
+ setIsLoading(true);
282
+ setError(fetchError ? new Error(fetchError) : null);
283
+ }
284
+ }
285
+ }, [serverData, ssr, fetchError]);
267
286
  const fetchData = useCallback(async () => {
268
287
  setIsLoading(true);
269
288
  setError(null);
@@ -283,10 +302,12 @@ function useHydratedData(props, fetcher) {
283
302
  }
284
303
  }, [session, ssr, fetcher, getAccessToken]);
285
304
  useEffect(() => {
286
- if (needsClientFetch) {
287
- fetchData();
288
- }
289
- }, []);
305
+ if (hasFetchedRef.current) return;
306
+ if (!needsClientFetch) return;
307
+ if (!ssr && !auth) return;
308
+ hasFetchedRef.current = true;
309
+ fetchData();
310
+ }, [needsClientFetch, ssr, auth, fetchData]);
290
311
  return {
291
312
  data,
292
313
  isLoading,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imtbl/auth-nextjs",
3
- "version": "2.12.5-alpha.1",
3
+ "version": "2.12.5-alpha.2",
4
4
  "description": "Next.js App Router authentication integration for Immutable SDK using Auth.js v5",
5
5
  "author": "Immutable",
6
6
  "bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
@@ -51,7 +51,7 @@
51
51
  "dist"
52
52
  ],
53
53
  "dependencies": {
54
- "@imtbl/auth": "2.12.5-alpha.1"
54
+ "@imtbl/auth": "2.12.5-alpha.2"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "next": "14.2.25",