Delay with dynamic in Next.js affecting UX – Looking for Solutions-
Unanswered
Florida White posted this in #help-forum
Florida WhiteOP
Hi everyone,
I’m facing an issue with using dynamic imports in Next.js. When I load a component dynamically, there’s a noticeable delay before it appears. While I understand this behavior is expected due to how dynamic works, it negatively impacts the user experience.
Here’s a simplified example of my code:
As you can see, I’m loading the SearchSideBar component dynamically using dynamic. The problem is that there’s a delay between when the user interacts with the button and when the component fully renders.
I’d like to know if there are ways to improve this:
- How can I preload the dynamically imported component so the delay is reduced or eliminated?
- Are there best practices for dynamically importing components that rely on animations or user-triggered actions?
Any guidance or suggestions would be greatly appreciated! 🙏
Thanks!
I’m facing an issue with using dynamic imports in Next.js. When I load a component dynamically, there’s a noticeable delay before it appears. While I understand this behavior is expected due to how dynamic works, it negatively impacts the user experience.
Here’s a simplified example of my code:
"use client";
import dynamic from "next/dynamic";
import { useState } from "react";
const SearchSideBar = dynamic(() => import('@/app/components/home/Navbar/SearchSideBar'));
const MobileMenu = () => {
const [isSearchBarOpen, setIsSearchBarOpen] = useState(false);
const [isSearchBarVisible, setIsSearchBarVisible] = useState(false);
const [isAnimating, setIsAnimating] = useState(false);
const toggleSearchSidebar = () => {
if (isAnimating || isSearchBarVisible) return;
setIsAnimating(true);
setIsSearchBarVisible(true);
setIsSearchBarOpen(true);
};
const handleSearchSidebarClose = () => {
setIsSearchBarOpen(false);
setTimeout(() => {
setIsSearchBarVisible(false);
setIsAnimating(false);
}, 300);
};
return (
<div>
<button onClick={toggleSearchSidebar}>Open Search Sidebar</button>
{isSearchBarVisible && (
<SearchSideBar isOpen={isSearchBarOpen} toggleSidebar={handleSearchSidebarClose} />
)}
</div>
);
};
export default MobileMenu;As you can see, I’m loading the SearchSideBar component dynamically using dynamic. The problem is that there’s a delay between when the user interacts with the button and when the component fully renders.
I’d like to know if there are ways to improve this:
- How can I preload the dynamically imported component so the delay is reduced or eliminated?
- Are there best practices for dynamically importing components that rely on animations or user-triggered actions?
Any guidance or suggestions would be greatly appreciated! 🙏
Thanks!
15 Replies
@Yi Lon Ma you can use the `loading` prop to show a loader
Florida WhiteOP
Thanks for the quick reply. So this is the only solution. Is the short delay normal?
yes, that's what dynamic does
@Yi Lon Ma yes, that's what dynamic does
Florida WhiteOP
Ok thanks, and in what situation would you advise me to use it?
@Florida White Ok thanks, and in what situation would you advise me to use it?
when something explicitly uses client side apis
window, document etc
@Yi Lon Ma when something explicitly uses client side apis
Florida WhiteOP
So when the file is marked "use client", Its better to use it ?
And also, is it possible to have it triggered by a scroll, like as soon as I get to the scroll area, well, it triggers it. I think it's possible, but is it a good practice?
@Florida White So when the file is marked "use client", Its better to use it ?
I think you are misunderstanding
use client with client side apis@Florida White And also, is it possible to have it triggered by a scroll, like as soon as I get to the scroll area, well, it triggers it. I think it's possible, but is it a good practice?
this sounds like job of
IntersectionObserver for which you need dynamic import and can't do much about it@Yi Lon Ma I think you are misunderstanding `use client` with `client side apis`
Florida WhiteOP
In fact, I see dynamic as a way to avoid loading everything at once. For example, if the person doesn't need a sidebar, I'll put it in dynamic.
yea but that can also be done with
? :Florida WhiteOP
Yes, except that if I don't use dynmaic I'll have to import the component normally (import components from "path"). And you lose the whole purpose of dynamic with ? :
yes
Florida WhiteOP
Ok thanks a lot for your help and anwsers