I have 30+ loading.tsx files... Is there a better way?
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
I have a ton of
And I put this file EVERYWHERE.... Like this:
- /products/loading.tsx
- /products/[id]/loading.tsx
- /products/[id]/edit/loading.tsx
This adds up very quickly, and I'm wondering if there's a way to do this once at a higher-level route, like
Thanks!
loading.tsx files that all render the same component. It looks something like this: import PageIsLoading from "@/ui/page-is-loading";
export default function () {
return <PageIsLoading />;
} And I put this file EVERYWHERE.... Like this:
- /products/loading.tsx
- /products/[id]/loading.tsx
- /products/[id]/edit/loading.tsx
This adds up very quickly, and I'm wondering if there's a way to do this once at a higher-level route, like
/products and have it apply to children routes as well, like /products/[id] and /products/[id]/edit ?Thanks!
32 Replies
@Masai Lion I have a ton of `loading.tsx` files that all render the same component. It looks something like this:
javascript
import PageIsLoading from "@/ui/page-is-loading";
export default function () {
return <PageIsLoading />;
}
And I put this file EVERYWHERE.... Like this:
- /products/loading.tsx
- /products/[id]/loading.tsx
- /products/[id]/edit/loading.tsx
This adds up very quickly, and I'm wondering if there's a way to do this once at a higher-level route, like `/products` and have it apply to children routes as well, like `/products/[id]` and `/products/[id]/edit` ?
Thanks!
you can create on top layer loading.tsx. When there is somthing fetching it will find the nearest suspense boundary and render it. Normally you want to have a suspense boundady only there, where you fetch stuff (on layer on top)
Masai LionOP
Are you saying to just have a
loading.tsx file at the top level route /products/ and page below that will use it? I feel like I tried that and it wasn't showing up...I don't fetch-on-render from the client side very often, I mostly use RSCs to collect data and render markup on the server side.
@Masai Lion Are you saying to just have a `loading.tsx` file at the top level route `/products/` and page below that will use it? I feel like I tried that and it wasn't showing up...
The loading.tsx is just a suspense wrapper for the page.tsx. And when you fetch something (for example) it will render the nearest suspense boundary. So yea.. should work
Masai LionOP
Ok, I'll try again....
And if it does not work like expected create a repro and send it here in this thread (https://codesandbox.io/)
Masai LionOP
Here's a github repo with a fresh nextjs project: https://github.com/TJBlackman/nextjs-loading-file-test
It works on the first page change, but not subsequent.
Parent -> Loader -> Child -> Grandchild (no loader) -> Great GrandChild (no loader)
And if you go straight to a grand child, or great grandchild page, it shows the loader on the first page change, but not subsequent.
Parent -> loader -> Grandchild -> great grandchild (no loader)
It works on the first page change, but not subsequent.
Parent -> Loader -> Child -> Grandchild (no loader) -> Great GrandChild (no loader)
And if you go straight to a grand child, or great grandchild page, it shows the loader on the first page change, but not subsequent.
Parent -> loader -> Grandchild -> great grandchild (no loader)
Masai LionOP
So, it works on any page, but only one time, and it doesn't work on any sequent links.
@Masai Lion So, it works on any page, but only one time, and it doesn't work on any sequent links.
change the loading.tsx to a template.tsx
Masai LionOP
Then the loading indicator is always showing and doesnt go away, and I have to render
props.children to get the actual content.If you really need this structure and it’s predictable
Just write a script that runs before next build
And creates them all
Masai LionOP
Can you point me to docs or an example?
No that’s basic JavaScript
ChatGPT what I said if you need more information
Masai LionOP
IS there a hook for running a script before nextjs build step? Like in next.config.js or something?
To run a script
Before next build you can modify the package json build script to run whatever you need
Before next build you can modify the package json build script to run whatever you need
build: “sh script.sh && next build”
Alternatively you can make a webpack plugin and add it to nextjs config but that’s significantly more involved
Masai LionOP
Thanks for your input, but I'm not in love with this suggestion. For now, I'll just leave all my loading.tsx files all throughout the code base.
That's the best decision btw!
I'd do the same thing, it's repetitive, but it's simple with 0 overhead
Btw you can simplify the loading file even more,
export default PageIsLoadingno need for the jsx, if you care at all
Masai LionOP
I made a tutorial video (3min) https://youtu.be/z24qIds0kIM, if anyone else comes along and is interested in what exactly is the problem.
@Masai Lion Thanks for your input, but I'm not in love with this suggestion. For now, I'll just leave all my loading.tsx files all throughout the code base.
why dont u put your loading.tsx in your root layout
idk if its possible but as far as i know you should only have loading.tsx where your layout is defined aswell
@Masai Lion i faced the same problem and I ended up adding the loading.tsx in all
Golden northern bumble bee
@Masai Lion I was literally running into this same problem today. the behavior is unintuitive. I just ended up putting the loading tsx alongside every page to make it work, pretty bad dx
asking Lee rob on x
asking Lee rob on x
@B33fb0n3 you can create on top layer loading.tsx. When there is somthing fetching it will find the nearest suspense boundary and render it. Normally you want to have a suspense boundady only there, where you fetch stuff (on layer on top)
Golden northern bumble bee
unfortunately this does not work after inital page load
Golden northern bumble bee
Any updates on this