Next.js 13 introduced a new file convention, loading.js
, to help you create meaningful Loading UI with React Suspense. With this convention, you can show an instant loading state from the server while the content of a route segment loads, the new content is automatically swapped in once rendering is complete.
An instant loading state is fallback UI that is shown immediately upon navigation. You can pre-render loading indicators such as skeletons and spinners, or a small but meaningful part of future screens such as a cover photo, title, etc. This helps users understand the app is responding and provides a better user experience.
Create a loading state by adding a loading.js
file inside a folder.
export default function Loading() {
// You can add any UI inside Loading, including a Skeleton.
return <LoadingSkeleton />
}
In the same folder, loading.js
will be nested inside layout.js
. It'll wrap the page.js
file and any children below in a <Suspense>
boundary.
Good to know:
In addition to loading.js
, you can also manually create Suspense Boundaries for your own UI components.
Recommendation: Use the
loading.js
convention for route segments (layouts and pages) as Next.js optimizes this functionality.
To learn how to use Suspense Boundaries, see the React Documentation.