Next.js Discord

Discord Forum

Routing and Loading.js Issue

Answered
Oriental chestnut gall wasp posted this in #help-forum
Open in Discord
Oriental chestnut gall waspOP
I've made a few posts over the past couple of days trying to nail down the issue I'm experiencing and I hope someone can help me get to the bottom of this.

Relevant Directory in Repo: https://github.com/shroommu/pokemon-battler/tree/master/src/app/pokedex
Live Production URL: https://pokemon.alexakruckenberg.com/pokedex

The Problem:

I'm creating a Pokedex for a portfolio project. The page looks like the first attachment when loaded. The buttons on the left each route to a different pokedex entry for each Pokemon (ie the URL for the Pikachu page looks like https://pokemon.alexakruckenberg.com/pokedex/pikachu)

When navigating from Pikachu to Charizard by clicking one of the buttons, I expect the following to happen:
1. The URL should update to /pokedex/charizard
2. While pokedex/charizard is loading, a skeleton for the right side of the page should be displayed (see second attachment)
3. Once pokedex/charizard has loaded, the skeleton should disappear, and the page should update with Charizard's data.

As I have my project set up now, this works in the local environment. As soon as I push to production, the skeleton in my loading.js never renders. In production, upon clicking a button to navigate to another Pokemon, the routing can take up to 5 seconds with no indication to the user that anything is loading.

To reproduce this, just click between 3-4 buttons on the left side and I pretty much guarantee you'll see my issue. I can most often reproduce it if I scroll to and then click on Mew at the bottom of the list. (Probably because Mew is never pre-fetched by next/link as it's never visible in the viewport on page refresh).

What am I doing wrong here?
Answered by Standard Chinchilla
Try putting this in page.tsx. export const dynamic = 'force-dynamic'
View full answer

21 Replies

Oriental chestnut gall waspOP
Bump
Standard Chinchilla
the loading.js file should be in the [pokemon] folder
since the pokedex/page.js is laoded instantly, even though you made it an async component, the laoding.js file is never triggered. Cause all it is is some text that says to select a pokemon. But when you move that loading.js file to pokedex/[pokemon]/ folder which has a page component that is actually async, you will get a suspense trigger and hopfully that should fix your problem
I'm not at my computer so i can't check, but I don't think the [pokemon]/page.js has an async component right now. I'll look as soon as I'm able.
Okay i just checked my repo on my phone and that page.js has an async component. I just don't know - I'm 100% certain i've tried putting the loading.js there and it didn't work.
Is there anything else that i need to consider?
Standard Chinchilla
Can you try again
Loading.js should be in [pokemon]/ in any case
@Standard Chinchilla Can you try again
Oriental chestnut gall waspOP
Just pushed the change, still no loading page.
Standard Chinchilla
Try putting this in page.tsx. export const dynamic = 'force-dynamic'
Answer
Oriental chestnut gall waspOP
holy shit
That did the trick.
Can you explain what that line does?
Standard Chinchilla
Yes in development mode all routes are treated as dynamic. In production, routes are static, meaning it’s rendered 1 time and then cached. That means once it get the Pokémon it will no longer hit your database again which is why there is no loading time therefore no loading stage.
Force dynamic tells next that you don’t want to do caching
Standard Chinchilla
Alo a note that if you force dynamic there is no point of generateStaticParams because you are always going to fetch from the database at request time. This will make your website slower so there is a trade off
Oriental chestnut gall waspOP
Ah yeah i can see that.
It's definitely a trade-off.
But i'll take this over what I had before, it's definitely a better user experience.
Thanks so much!