Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextTopLoader stopped working on Nextjs 14.0.3 #56

Open
ajsystem opened this issue Nov 17, 2023 · 36 comments
Open

NextTopLoader stopped working on Nextjs 14.0.3 #56

ajsystem opened this issue Nov 17, 2023 · 36 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@ajsystem
Copy link

Hello, just to report that on a project with the last Nextjs 14.0.3, the toploader keeps loading and never finish or completes.

But there aren't errors on console server/client.

As a workaround to get it working install 14.0.2

@rrojan
Copy link

rrojan commented Nov 18, 2023

Same here. the progress bar is stuck at the end and never finishes loading since NextJS 14.0.3.

It also looks less snappy, if that makes sense. At the time where it previously completed loading, it now goes to around at 60-70% progress and slowly crawls its way to the end, but doesn't stop loading.

@SuhelMakkad
Copy link

+1

@jonathanwilke
Copy link

Same problem for me. Downgrading to 14.0.2 fixed it.

@TheSGJ
Copy link
Owner

TheSGJ commented Nov 18, 2023

Next js 14.0.3 added experimental support for history.pushState and history.replaceState. That's why it's messing with history.pushState() method in the code.
Here's the pr which introduced it:
vercel/next.js/pull/58335

@TheSGJ TheSGJ added help wanted Extra attention is needed bug Something isn't working labels Nov 18, 2023
@putramaghfirah
Copy link

same problem.

@croofec
Copy link

croofec commented Nov 18, 2023

+1

@sengawasenga
Copy link

I got the same problem, how can we solve it ?

@omarqra
Copy link

omarqra commented Nov 19, 2023

+1

@preeti-192
Copy link

preeti-192 commented Nov 20, 2023

import type { Metadata } from 'next';
import './globals.css';
import { ReactNode } from 'react';
import NextTopLoader from 'nextjs-toploader';
import AppWrapper from './AppWrapper';

export default function RootLayout({
  children,
}: Readonly<{
  children: ReactNode;
}>) {
  return (
    <html lang="en" className="antialiased">
      <body>
        <NextTopLoader
          color="hsl(var(--secondary-light))"
          initialPosition={0.08}
          crawlSpeed={200}
          height={3}
          crawl
          showSpinner
          easing="ease"
          speed={200}
          shadow="0 0 10px hsl(var(--secondary)),0 0 5px hsl(var(--secondary-light))"
          template='<div class="bar" role="bar"><div class="peg"></div></div> 
  <div class="spinner" style="top: 65px" role="spinner"><div class="spinner-icon"></div></div>'
          zIndex={999999}
        />
        <main>
          <AppWrapper>{children}</AppWrapper>
        </main>
      </body>
    </html>
  );
}

@PedroL22
Copy link

same here

@besufkadmenji
Copy link

besufkadmenji commented Nov 20, 2023

Possible fix: run NProgress.done() inside your top level client component, this will cleanup any stuck loader

import * as NProgress from "nprogress";

useEffect(() => {
    NProgress.done();
}, [pathname, router]);

@Jings
Copy link

Jings commented Nov 21, 2023

Possible fix: run NProgress.done() inside your top level client component, this will cleanup any stuck loader

import * as NProgress from "nprogress";

useEffect(() => {
    NProgress.done();
}, [pathname, router]);

This works for now.
Hopefully the proper fix will come soon :)

@joshke
Copy link

joshke commented Nov 22, 2023

+1

1 similar comment
@Super-Kenil
Copy link

+1

@monir6163
Copy link

Possible fix: run NProgress.done() inside your top level client component, this will cleanup any stuck loader

import * as NProgress from "nprogress";

useEffect(() => {
    NProgress.done();
}, [pathname, router]);

why root layout client-side rendering? I hope it is not good practiche

@Jings
Copy link

Jings commented Nov 23, 2023

Possible fix: run NProgress.done() inside your top level client component, this will cleanup any stuck loader

import * as NProgress from "nprogress";

useEffect(() => {
    NProgress.done();
}, [pathname, router]);

why root layout client-side rendering? I hope it is not good practiche

@besufkadmenji is only talking about your top most client component not root layout client side rendering. If this is your root layout, fine. For me it is not but instead two or three pages that are the top most client components.

@vnevermore
Copy link

same problem here

@gokulkrishh
Copy link

Possible fix: run NProgress.done() inside your top level client component, this will cleanup any stuck loader

import * as NProgress from "nprogress";

useEffect(() => {
    NProgress.done();
}, [pathname, router]);

Yep this worked for me. But we need proper fix. Thanks for suggesting.

@NestedGateway
Copy link

Also facing problem with nextjs 14. I am still using it because there is no such alternative to it. All other all manual.

@gustaveWPM
Copy link

gustaveWPM commented Nov 30, 2023

Possible fix: run NProgress.done() inside your top level client component, this will cleanup any stuck loader

import * as NProgress from "nprogress";

useEffect(() => {
    NProgress.done();
}, [pathname, router]);

Thank you so much!
I added it at the top of my <Providers> component.

My root layout looks like this:

<html lang={language} dir={dir}>
  <body>
    <div>
      <Providers locale={language}>
        <NextTopLoader {...} />
        {children}
// ...

So that's a really good workaround, imo.

@andrewmumblebee
Copy link

andrewmumblebee commented Dec 1, 2023

Potentially will be fixed by vercel/next.js#58861

@nunesunil
Copy link

Fixed by vercel/next.js#58861 and released in v14.0.4-canary.37.

@Super-Kenil
Copy link

@ajsystem , I checked the latest Nextjs v14.0.4. and nextjs-toploader works fine in that version. I think this issues should be closed now

@mamlzy
Copy link

mamlzy commented Dec 9, 2023

@ajsystem , I checked the latest Nextjs v14.0.4. and nextjs-toploader works fine in that version. I think this issues should be closed now

let see what other people opinions first, just to make sure before it's closed😉

@gokulkrishh
Copy link

@ImamAlfariziSyahputra @Super-Kenil Yep issue is fixed in v14.0.4 for my app expense.fyi

@Super-Kenil
Copy link

@ImamAlfariziSyahputra . I had already tested it in 3 of my projects, then I commented here.
@gokulkrishh thanks for confirming

@gustaveWPM
Copy link

Fixed!

@bschwartz10
Copy link

bschwartz10 commented Jan 4, 2024

Still experiencing the stuck loader when its 99% done. This is happening specifically when using the custom useRouter solution from this issue: #10 (comment)

"next": "^14.0.4"
"nextjs-toploader": "^1.6.4"
"nprogress": "^0.2.0"

@B33fb0n3
Copy link

B33fb0n3 commented Jan 30, 2024

Still not working :(

"next": "14.1.0",
"nextjs-toploader": "^1.6.4",

@sho-pb
Copy link

sho-pb commented Jan 31, 2024

my solutions

// NextTopLoader.tsx
'use client';

import Loader from 'nextjs-toploader';
import { usePathname } from 'next/navigation';

const NextTopLoader = () => {
  const pathname = usePathname();

  useEffect(() => {
    NProgress.done();
  }, [pathname]);

  return (
    <Loader />
  )
}
// useRouter.ts
import { NavigateOptions } from 'next/dist/shared/lib/app-router-context.shared-runtime';
import { useRouter as useNextRouter, usePathname } from 'next/navigation';
import { useCallback } from 'react';
import NProgress from 'nprogress';

export const useRouter = () => {
  const router = useNextRouter();
  const pathname = usePathname();

  const replace = useCallback(
    (href: string, options?: NavigateOptions) => {
      href !== pathname && NProgress.start();
      router.replace(href, options);
    },
    [router, pathname],
  );

  const push = useCallback(
    (href: string, options?: NavigateOptions) => {
      href !== pathname && NProgress.start();
      router.push(href, options);
    },
    [router, pathname],
  );

  return {
    ...router,
    replace,
    push,
  };
};

@Super-Kenil
Copy link

@B33fb0n3

Still not working :(

"next": "14.1.0",
"nextjs-toploader": "^1.6.4",

I am using nextjs-toploader, With same version you're using, Both Next and nextjs-toploader and it seems to be working perfectly on my end.

For fixing it, make sure you are not using it multiple times unnecessarily in your App.
If it doesn't get fixed, please create a Reproduction so that we could take a look at it

@moon2850088
Copy link

my solutions

// NextTopLoader.tsx
'use client';

import Loader from 'nextjs-toploader';
import { usePathname } from 'next/navigation';

const NextTopLoader = () => {
  const pathname = usePathname();

  useEffect(() => {
    NProgress.done();
  }, [pathname]);

  return (
    <Loader />
  )
}
// useRouter.ts
import { NavigateOptions } from 'next/dist/shared/lib/app-router-context.shared-runtime';
import { useRouter as useNextRouter, usePathname } from 'next/navigation';
import { useCallback } from 'react';
import NProgress from 'nprogress';

export const useRouter = () => {
  const router = useNextRouter();
  const pathname = usePathname();

  const replace = useCallback(
    (href: string, options?: NavigateOptions) => {
      href !== pathname && NProgress.start();
      router.replace(href, options);
    },
    [router, pathname],
  );

  const push = useCallback(
    (href: string, options?: NavigateOptions) => {
      href !== pathname && NProgress.start();
      router.push(href, options);
    },
    [router, pathname],
  );

  return {
    ...router,
    replace,
    push,
  };
};

It works for me. Thanks for your solution !

@rohankm
Copy link

rohankm commented Mar 22, 2024

my solutions

// NextTopLoader.tsx
'use client';

import Loader from 'nextjs-toploader';
import { usePathname } from 'next/navigation';

const NextTopLoader = () => {
  const pathname = usePathname();

  useEffect(() => {
    NProgress.done();
  }, [pathname]);

  return (
    <Loader />
  )
}
// useRouter.ts
import { NavigateOptions } from 'next/dist/shared/lib/app-router-context.shared-runtime';
import { useRouter as useNextRouter, usePathname } from 'next/navigation';
import { useCallback } from 'react';
import NProgress from 'nprogress';

export const useRouter = () => {
 const router = useNextRouter();
 const pathname = usePathname();

 const replace = useCallback(
   (href: string, options?: NavigateOptions) => {
     href !== pathname && NProgress.start();
     router.replace(href, options);
   },
   [router, pathname],
 );

 const push = useCallback(
   (href: string, options?: NavigateOptions) => {
     href !== pathname && NProgress.start();
     router.push(href, options);
   },
   [router, pathname],
 );

 return {
   ...router,
   replace,
   push,
 };
};

this works for the latest nextjs

@Kamleshpaul
Copy link

my solutions

// NextTopLoader.tsx
'use client';

import Loader from 'nextjs-toploader';
import { usePathname } from 'next/navigation';

const NextTopLoader = () => {
  const pathname = usePathname();

  useEffect(() => {
    NProgress.done();
  }, [pathname]);

  return (
    <Loader />
  )
}
// useRouter.ts
import { NavigateOptions } from 'next/dist/shared/lib/app-router-context.shared-runtime';
import { useRouter as useNextRouter, usePathname } from 'next/navigation';
import { useCallback } from 'react';
import NProgress from 'nprogress';

export const useRouter = () => {
  const router = useNextRouter();
  const pathname = usePathname();

  const replace = useCallback(
    (href: string, options?: NavigateOptions) => {
      href !== pathname && NProgress.start();
      router.replace(href, options);
    },
    [router, pathname],
  );

  const push = useCallback(
    (href: string, options?: NavigateOptions) => {
      href !== pathname && NProgress.start();
      router.push(href, options);
    },
    [router, pathname],
  );

  return {
    ...router,
    replace,
    push,
  };
};

this works

@Himture
Copy link

Himture commented Aug 5, 2024

Solution given by @sho-pb is perfect and works well and you can also do all the customization to the loader in the NextTopLoader.tsx file by adding the props there in the return.
Kudos!

@Emiliano-Bucci
Copy link

Emiliano-Bucci commented Nov 14, 2024

@sho-pb Hi! Is your solution also working with the browser back button? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests