如何在NextJS的应用目录中创建加载指示器或进度条?

huangapple go评论79阅读模式
英文:

How to create a Loading Indicator or a Progress Bar in NextJS's App Directory?

问题

I have developed a large web application with NextJS 13, the first version was using the Pages Router.

After completing almost the entire website, I managed to migrate to the App Directory.

This migration was not essentially to upgrade for some new routing features, but I wanted to change my SASS's Large compiled file imported in the _app.tsx for better website loading time.

Since the loading was quite a big issue for this project, I started using MUI with the styling of each component, simply a CSS-in-JS solution.

But the problem I found with the new App Directory is the router events. I had a progress bar indicator from the next-n-progress package, but now it doesn't work. The user clicks on a link, and the app takes some small time to load the next page.

The problem is not an issue for SSR pages since I have put a loading.tsx file in the root of each page, but it persists for pages with client-side configuration.

Like the Home Page, Sign-in & Sign-up, etc.

I tried another package for the Progress Bar that supports the new app directory, but it is not showing up at all.

This is the component responsible for the Layout:
GitHub Component permalink

Is there any way to create a new progress bar with this new NextJS 13 Update?

英文:

I have developed a large web application with NextJS 13, the first version was using the Pages Router.

After completing almost the entire website, I managed to migrate to the App Directory.

This migration was not essentially to upgrade for some new routing features, but I wanted to change my SASS's Large compiled file imported in the _app.tsx for better website loading time.

Since the loading was quite a big issue for this project, I started using MUI with the styling of the each component, simply a CSS-in-JS solution.

But the problem I found with the new App Directory, is the router events, I had a progress bar indicator from the next-n-progress package, but now it doesn't work, the user clicks on a link and the app takes some small time to load the next page.

The problem is not an issue for SSR pages since I have put a loading.tsx file in the root of each page, but it persists for pages with client-side configuration.

Like the Home Page, Sign-in & Sign-up, etc.

I tried another package for the Progress Bar that supports the new app directory, but it is not showing up at all.

This is the component responsible for the Layout:
GitHub Component permalink

Is there any way to create a new progress bar with this new NextJS 13 Update?

答案1

得分: 1

更新

我发现在新的 Next.js 13 应用目录中,这是一个常见问题,并且有一些关于它的开放问题,包括在应用路由行为中遇到的多个问题。

因此,我成功使用了 next-n-progress,但我注意到它仅通过 使用 LINK 组件 来工作,因为它触发了加载以显示进度条,此外,它还具有预取视口上存在的所有链接的好处。

所以,在此期间,只需继续使用 Link 而不是 Router.push 用于简单的导航情况。

英文:

UPDATE

I found that it is a common issue with the new Next.js 13 App Directory, and there are some open issues about it, including multiple problems encountered in the App Router Behaviors.

So, I managed to use next-n-progress, but I noticed that it only works by USING LINK COMPONENT, since it triggers the loading for the progress bar to appear, plus it has a benefit of prefetching all Links present on the view-port.

So, meanwhile, just keep using Link instead of Router.push for simple navigation cases.

huangapple
  • 本文由 发表于 2023年5月25日 20:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定