DEV Community

Cover image for Next 13 - Special files
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

5

Next 13 - Special files

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.

Next 13 loading component

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>;
}
Enter fullscreen mode Exit fullscreen mode

I placed this in the app/dashboard/account/loading.tsx spot. We'll work on testing it out in the following article.

Next 13 error component

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>
  );
}
Enter fullscreen mode Exit fullscreen mode

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, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay