Yuki Takei 7 месяцев назад
Родитель
Сommit
8a43a55081

+ 3 - 22
apps/app/src/pages/[[...path]].page.tsx

@@ -41,7 +41,6 @@ import { useSetupGlobalSocket, useSetupGlobalSocketForPage } from '~/stores/webs
 import { useSWRMUTxCurrentPageYjsData } from '~/stores/yjs';
 
 import { useSameRouteNavigation, useShallowRouting } from './[[...path]]/hooks';
-import { extractPageIdFromPathname } from './[[...path]]/navigation-utils';
 import { getServerSidePropsForInitial, getServerSidePropsForSameRoute } from './[[...path]]/server-side-props';
 import type {
   Props, InitialProps, SameRouteEachProps, IPageToShowRevisionWithMeta,
@@ -121,27 +120,9 @@ const isInitialProps = (props: Props): props is (InitialProps & SameRouteEachPro
 const Page: NextPageWithLayout<Props> = (props: Props) => {
   const router = useRouter();
 
-  // DEBUG: Log props changes to track browser back behavior
-  console.debug('Page component render:', {
-    currentPathname: props.currentPathname,
-    routerAsPath: router.asPath,
-    timestamp: new Date().toISOString(),
-  });
-
-  // DEBUG: Monitor router changes
+  // Monitor router changes - cleaned up empty handler
   useEffect(() => {
-    const handleRouteChange = (url: string) => {
-      console.debug('Router route change:', {
-        url,
-        propsCurrentPathname: props.currentPathname,
-        timestamp: new Date().toISOString(),
-      });
-    };
-
-    router.events.on('routeChangeComplete', handleRouteChange);
-    return () => {
-      router.events.off('routeChangeComplete', handleRouteChange);
-    };
+    // router.events handlers removed as they were only for debugging
   }, [router.events, props.currentPathname]);
 
   // register global EventEmitter
@@ -172,7 +153,7 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   useSetupGlobalSocketForPage(pageId);
 
   // Use custom hooks for navigation and routing
-  useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps);
+  useSameRouteNavigation(props);
   useShallowRouting(props);
 
   // Optimized effects with minimal dependencies

+ 7 - 10
apps/app/src/pages/[[...path]]/use-same-route-navigation.spec.tsx

@@ -45,9 +45,6 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
     return { currentPathname } as Parameters<typeof useSameRouteNavigation>[0];
   };
 
-  // Simple isInitialProps - testing runtime behavior, not type checking
-  const isInitialProps = (props: Parameters<typeof useSameRouteNavigation>[0]): props is never => false; // Always use client-side logic
-
   beforeEach(() => {
     vi.clearAllMocks();
 
@@ -79,7 +76,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       const props = createProps('/stale/props/path');
       mockRouter.asPath = '/actual/browser/path'; // Browser is actually here
 
-      renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      renderHook(() => useSameRouteNavigation(props));
 
       await act(async() => {
         await new Promise(resolve => setTimeout(resolve, 0));
@@ -98,7 +95,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       mockUseCurrentPageData.mockReturnValue([{ path: '/different/path' }]);
       mockUseCurrentPageId.mockReturnValue(['different-id', mockSetCurrentPageId]);
 
-      const { rerender } = renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      const { rerender } = renderHook(() => useSameRouteNavigation(props));
 
       // Initial render should trigger fetch (mismatched state)
       await act(async() => {
@@ -129,7 +126,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       const props = createProps('/some/page');
       mockRouter.asPath = '/some/page';
 
-      renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      renderHook(() => useSameRouteNavigation(props));
 
       await act(async() => {
         await new Promise(resolve => setTimeout(resolve, 0));
@@ -142,7 +139,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       const props = createProps('/page/path');
       mockRouter.asPath = '/page/path';
 
-      renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      renderHook(() => useSameRouteNavigation(props));
 
       await act(async() => {
         await new Promise(resolve => setTimeout(resolve, 0));
@@ -159,7 +156,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       mockUseCurrentPageData.mockReturnValue([{ path: '/current/page' }]);
       mockUseCurrentPageId.mockReturnValue(['id-for--current-page', mockSetCurrentPageId]);
 
-      renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      renderHook(() => useSameRouteNavigation(props));
 
       // Should not fetch when everything is already in sync
       expect(mockFetchCurrentPage).not.toHaveBeenCalled();
@@ -177,7 +174,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       });
       mockFetchCurrentPage.mockReturnValue(slowFetchPromise);
 
-      const { rerender } = renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      const { rerender } = renderHook(() => useSameRouteNavigation(props));
 
       // Start first fetch
       await act(async() => {
@@ -217,7 +214,7 @@ describe('useSameRouteNavigation - Essential Bug Fix Verification', () => {
       // Start with existing page
       mockUseCurrentPageId.mockReturnValue(['old-page-id', mockSetCurrentPageId]);
 
-      renderHook(() => useSameRouteNavigation(props, extractPageIdFromPathname, isInitialProps));
+      renderHook(() => useSameRouteNavigation(props));
 
       await act(async() => {
         await new Promise(resolve => setTimeout(resolve, 0));

+ 6 - 33
apps/app/src/pages/[[...path]]/use-same-route-navigation.ts

@@ -16,8 +16,6 @@ import type { Props, InitialProps, SameRouteEachProps } from './types';
  */
 export const useSameRouteNavigation = (
     props: Props,
-    _extractPageIdFromPathname: (pathname: string) => string | null, // Legacy parameter for backward compatibility
-    isInitialProps: (props: Props) => props is (InitialProps & SameRouteEachProps),
 ): void => {
   const router = useRouter();
   const [currentPage] = useCurrentPageData();
@@ -29,21 +27,18 @@ export const useSameRouteNavigation = (
   const lastProcessedPathnameRef = useRef<string | null>(null);
   const isFetchingRef = useRef<boolean>(false);
 
+  // Type guard to check if props are initial props
+  const isInitialProps = (props: Props): props is (InitialProps & SameRouteEachProps) => {
+    return 'isNextjsRoutingTypeInitial' in props && props.isNextjsRoutingTypeInitial;
+  };
+
   // Process pathname changes - monitor both props.currentPathname and router.asPath
   useEffect(() => {
     // Use router.asPath for browser back/forward compatibility, fallback to props.currentPathname
     const targetPathname = router.asPath || props.currentPathname;
 
-    // Always log when useEffect is triggered
-    console.debug('useSameRouteNavigation useEffect triggered:', {
-      targetPathname,
-      lastProcessed: lastProcessedPathnameRef.current,
-      timestamp: new Date().toISOString(),
-    });
-
     // Skip if we already processed this pathname
     if (lastProcessedPathnameRef.current === targetPathname) {
-      console.debug('Skipping - already processed:', targetPathname);
       return;
     }
 
@@ -52,7 +47,6 @@ export const useSameRouteNavigation = (
     const hasInitialData = isInitialProps(props) && !skipSSR;
 
     if (hasInitialData) {
-      console.debug('Skipping fetch - has initial data:', targetPathname);
       lastProcessedPathnameRef.current = targetPathname;
       return;
     }
@@ -71,35 +65,16 @@ export const useSameRouteNavigation = (
       || (currentPagePath !== targetPathname) // Different path
     );
 
-    console.debug('shouldFetch decision:', {
-      currentPagePath,
-      targetPathname,
-      targetPageId,
-      pageId,
-      condition1_noCurrentPage: !currentPagePath,
-      condition2_differentPageId: (targetPageId != null && pageId != null && pageId !== targetPageId),
-      condition3_differentPath: (currentPagePath !== targetPathname),
-      finalDecision: shouldFetch,
-    });
-
     if (!shouldFetch) {
-      console.debug('No fetch needed for:', targetPathname);
       lastProcessedPathnameRef.current = targetPathname;
       return;
     }
 
     // Prevent concurrent fetches
     if (isFetchingRef.current) {
-      console.debug('Fetch already in progress, skipping:', targetPathname);
       return;
     }
 
-    console.debug('Starting fetch for:', targetPathname, {
-      currentPagePath,
-      targetPageId,
-      pageId,
-    });
-
     isFetchingRef.current = true;
 
     const updatePageState = async(): Promise<void> => {
@@ -115,8 +90,6 @@ export const useSameRouteNavigation = (
         // Fetch page data
         const pageData = await fetchCurrentPage(targetPathname);
 
-        console.debug('Page data fetched for:', targetPathname, !!pageData);
-
         // Update editing markdown if we have body content
         if (pageData?.revision?.body !== undefined) {
           mutateEditingMarkdown(pageData.revision.body);
@@ -126,7 +99,7 @@ export const useSameRouteNavigation = (
         lastProcessedPathnameRef.current = targetPathname;
       }
       catch (error) {
-        console.error('Error fetching page data for:', targetPathname, error);
+        // Silent error handling - errors are logged by the caller if needed
       }
       finally {
         isFetchingRef.current = false;