Slow Navigation and Delayed Backend Request in Next.js Using `router.push(href)`

I’m working on a Next.js project and have implemented a SideNav component that dynamically generates navigation items based on the modules returned from an API. Each nav item has an href that is encoded using a custom function, and when a nav item is clicked, router.push(href) is called to navigate to the corresponding page.

However, I’ve noticed that when I click on any nav item, the URL in the browser takes some time to change. During this period, a network fetch call is initiated, which seems to delay the URL change. Only after this fetch call resolves does the request get sent to the backend, causing a noticeable delay in navigation.

Here’s how I’m generating the NavItem objects and their hrefs

const encodePathComponent = (component: string): string => {
  return encodeURIComponent(component.toLowerCase().replace(/[s/.]/g, '_'));
};

const generateNavItems = (modules: any[]): NavItem[] => {
  return modules.map((module) => {
    const { parent_module_name, children } = module;
    const IconComponent = moduleIconMap[parent_module_name as IconKey] || null;

    const subNav = children.length > 0
      ? children.map((child: any) => {
          const childHref = `/${encodePathComponent(parent_module_name)}/${encodePathComponent(child.child_module_name)}`;
          return {
            href: childHref,
            label: child.child_module_name,
            subNav: child.sub_children?.map((subChild: any) => {
              const subChildHref = `/${encodePathComponent(parent_module_name)}/${encodePathComponent(child.child_module_name)}/${encodePathComponent(subChild.sub_child_module_name)}`;
              return {
                href: subChildHref,
                label: subChild.sub_child_module_name,
              };
            }),
          };
        })
      : [];

    const href = children.length === 0 ? `/${encodePathComponent(parent_module_name)}` : undefined;

    return {
      label: parent_module_name,
      icon: IconComponent ? <IconComponent className="w-4 h-4" /> : null,
      href,
      subNav,
    };
  });
};

And this is how I’m handling the navigation:

const handleNavigation = useCallback(
  (label: string, href?: string) => {
    if (href) {
      router.push(href);
      setTitle(label);
    }
  },
  [router]
);

The Problem:

The issue arises at the router.push(href) line. The navigation is noticeably slow, with the URL change and the request to the backend both taking longer than expected. This delay is affecting the user experience, and I’m looking for ways to optimize this process.

Additional Details:Network Tab Image.
The delay occurs between the initiation of router.push(href) and the actual URL change in the browser.
This delay is compounded by the time it takes for the backend request to complete.
Has anyone experienced a similar issue or have any suggestions for improving the performance of router.push in this context? Any insights or recommendations would be greatly appreciated!

I tried these:

  1. Prefetching: I attempted to use router.prefetch(href) to prefetch the URLs when a user hovers over the navigation items. However, this didn’t significantly improve the navigation speed or reduce the delay in sending requests to the backend.

  2. Use of useMemo and useCallback: I optimized the SideNav component using useMemo and useCallback to minimize unnecessary re-renders and improve performance, but the delay persisted.

  3. Analyzing Network Requests: I checked the Network tab in the browser’s developer tools and noticed that the xhr requests to the backend are delayed, which seems to be contributing to the slow navigation.

I expected the URL to change immediately after calling router.push(href), with minimal delay before the backend request is sent.
I also expected that using router.prefetch(href) would reduce the delay, making the navigation smoother and more responsive.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật