Next 13 - Special files

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

A super cool new addition to this new routing/layout style of Next 13 is the fact that we get a couple of special files that make our application more robust.
The concept of these files already existed, but we had to wrap many elements with Suspense to achieve the same manually.
Let's take a look at which ones we get:
page.tsx: We already used this to create our pages.layout.tsx: Defined to create the layouts per folder.loading.tsx: Optional loading component can be used to show a loading state.error.tsx: Optional error state can be used only to show the error in a specific application part.template.tsx: Optional and very similar to layout, but this one will always remount on navigation, and the state is not shared.head.tsx: Optional to add a different <head> tag for a specific route.For this article, we'll look into loading and error components.
Let's say you have a component that can initially take quite some time to load. We will block the page until the load is done without any loading or handling of the load.
Since the loading only would happen in one of our components, we get the fantastic feature of loading components. By adding these to our folder, we can show the time it takes to load.
The big benefit being it won't block the rest of the page or our navigation.
To add these files, add a loading.tsx file. Some example content could be:
export default function Loading() {
return <p>Loading account details</p>;
}
I placed this in the app/dashboard/account/loading.tsx spot. We'll work on testing it out in the following article.
Like the loading component, it could be that only one element on the page gives us an error. By default, this could crash out the entire application, but with this new error component, we can only show that piece of the puzzle as an error.
You can add this file by creating an error.tsx file in your component folder.
Note: This has to be a client component!
An example error component would look like this.
'use client';
export default function Error({
error,
reset,
}: {
error: Error,
reset: () => void,
}) {
return (
<div>
<p>Something went wrong!</p>
<button onClick={() => reset()}>Reset error boundary</button>
</div>
);
}
Now, we show this snippet whenever an error happens in this component, and the user can retry the component render.
I added these components on GitHub with some sample code we'll dive into later, but you can already check it out in this branch.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter