Before you read this article, I want you to know that it is a bit flawed, and I apologize for the mistakes I made previously, while authoring this ...
For further actions, you may consider blocking this person and/or reporting abuse
I feel like it doesn't really work, does it ? You're only waiting 5 arbitrary seconds and since the content of the page is not in the DOM it doesn't even fetch the images etc I believe ?
The way I did it was by using a loader that goes on top of the content instead of replacing it and listening to events from imagesloaded.desandro.com/ to know when the loader could be removed
Yeah Martin...
That was kinda like the idea around it... Some DOM contents (mostly images) takes time before they're displayed on a webpage.
The idea behind the usage of the
setTimeoutfunction is to show the leader for small amount of time instead of having a blank section that holds the image or content you're expecting to see on a webpage.I think, adding the preloading screen for a short amount of time helps to keep the users engaged on your website, before the contents of the page are fully ready.
One can also go ahead to increase or reduce the milliseconds. It all depends on your preferences, and the demography of the type of users your building for.
Yes I understood that, but the way you do it won't work I think, since NextJS won't load images that are not in the DOM, so you're making your users wait for nothing I believe ? The content to load should be in the DOM but you can show a loader OVER it while you're loading.
Example :
No Martin,
Nextjs already has it's own image optimization feature when they introduced the
<Image />component.You can decide to pass properties like
onBlurdataURLto display either a loader or masked image before the realy content is shown.But the aim of this article isn't related to that. Take for example, in some countries where the access to internet service is very poor. The time that it'll take toblaod some DOM elements will be high. A preloading component comes in handy in situations like this.
Yes, I know about the Image component but that doesn't have much to do with what I'm saying.
I don't think you understand what I'm trying to say but that's ok, my comments will be there to help people having trouble with it !
What will your own approach be like.
Can you share it with me? If you don't mind.
Yes, Ivan. I'm setting the loader to be visible on every page render. That's why it is inside the
_app.jsfile.And, yes... there's no logic that set's the loader back to
falsesince we're making use of it in theuseEffecthook, which runs only once, i.e. when the page is loaded for the first time or when it is refreshed.Hardcoding the loading time isn't the best way, it's false loading
Because In nextjs production things are very fast, user will be waiting unnecessarily for each page change
Instead you can use router events in nextjs
nextjs.org/docs/api-reference/next...
Hi @arshkkk ,
I'm not making the preloading screen show on each page change. Just when the page is first mounted to the DOM.
But, I'll definitely check the link you shared here. Thank you for sharing it 😎
Here's how I did this, rather than being an arbitrary load it actually does detect images being loaded.
Even this new solution not working. Your useEffect function should listen to router event changes and should look like this:
Thanks Ilir! Been learning about the super power of the
useRouterhook lately. I'll have this modified to reflect the change as soon as possible.It's not actually a preloader. Preloader should have to wait till API will load, and some API's will take less time, some will take more, and using setTimeOut is actually making it static, but we need dynamic timing, which is not possible with your code.
I have problem in meta tags is not rendered when page is loading. Meta Tags will be should load in the request
Hi @abdulrahmanelheyb,
Kindly take a look at this article that explains a possible fix.