React Suspense is a mechanism that allows components to "wait" for something before rendering, typically used for data fetching or waiting on other asynchronous processes. This feature enables developers to define a fallback component (like a loading indicator) that is displayed while waiting for the component’s content to be ready.
loading.js
, Next.js applications can show immediate feedback upon navigation, such as skeleton screens or spinners. This feedback is crucial for improving user perception of application responsiveness.In a typical implementation, you would wrap components that perform asynchronous actions within <Suspense>
tags, specifying a fallback UI to show while the data is being loaded.
Next.js enhances server-side rendering (SSR) by incorporating streaming, which breaks down the page's HTML into smaller chunks that can be sent to the client as soon as they are ready, rather than waiting for the entire page to load.
error.js
FileNext.js provides a structured way to handle errors using the error.js
file. This file acts as a React Error Boundary, catching errors in its component subtree and displaying a fallback UI.
error.js
can include functionality to attempt recovery from errors without needing a full page reload, such as retry buttons.