Просмотр исходного кода

Fix detectNextjsRoutingType to handle undefined previousRoutingPage and add related tests

Yuki Takei 2 месяцев назад
Родитель
Сommit
0c18fa662c

+ 52 - 0
apps/app/src/pages/utils/nextjs-routing-utils.spec.ts

@@ -275,5 +275,57 @@ describe('nextjs-routing-utils', () => {
 
       expect(result).toBe('initial');
     });
+
+    describe('when previousRoutingPage is undefined', () => {
+      it('should return INITIAL when request is not CSR', () => {
+        const context = createMockContext(false);
+
+        const result = detectNextjsRoutingType(context);
+
+        expect(result).toBe('initial');
+      });
+
+      it('should return FROM_OUTSIDE when CSR and no cookie exists', () => {
+        const context = createMockContext(true); // No cookie value
+
+        const result = detectNextjsRoutingType(context);
+
+        expect(result).toBe('from-outside');
+      });
+
+      it('should return FROM_OUTSIDE when CSR and cookie exists', () => {
+        const context = createMockContext(true, '/some-page');
+
+        const result = detectNextjsRoutingType(context);
+
+        expect(result).toBe('from-outside');
+      });
+
+      it('should return FROM_OUTSIDE when CSR and cookie is empty string', () => {
+        const context = createMockContext(true, '');
+
+        const result = detectNextjsRoutingType(context);
+
+        expect(result).toBe('from-outside');
+      });
+    });
+
+    describe('when both cookie and previousRoutingPage are undefined/null', () => {
+      it('should return INITIAL when request is not CSR and both are missing', () => {
+        const context = createMockContext(false);
+
+        const result = detectNextjsRoutingType(context);
+
+        expect(result).toBe('initial');
+      });
+
+      it('should return FROM_OUTSIDE when CSR and both are missing', () => {
+        const context = createMockContext(true); // No cookie
+
+        const result = detectNextjsRoutingType(context);
+
+        expect(result).toBe('from-outside');
+      });
+    });
   });
 });

+ 6 - 2
apps/app/src/pages/utils/nextjs-routing-utils.ts

@@ -27,7 +27,7 @@ export type NextjsRoutingType =
 
 export const detectNextjsRoutingType = (
   context: GetServerSidePropsContext,
-  previousRoutingPage: string,
+  previousRoutingPage?: string,
 ): NextjsRoutingType => {
   const isCSR = !!context.req.headers['x-nextjs-data'];
 
@@ -38,7 +38,11 @@ export const detectNextjsRoutingType = (
   // Read cookie from server-side context
   const nextjsRoutingPage = context.req.cookies[COOKIE_NAME];
 
-  if (nextjsRoutingPage != null && nextjsRoutingPage === previousRoutingPage) {
+  if (
+    nextjsRoutingPage != null &&
+    previousRoutingPage != null &&
+    nextjsRoutingPage === previousRoutingPage
+  ) {
     return NextjsRoutingType.SAME_ROUTE;
   }