Subscribe

Next 13 - Data fetching

✍️

A closer look at data fetching in Next 13

14 Nov, 2022 · 2 min read

Data fetching is nothing new in Next, but they simplified how this works.

You might remember getServerSideProps and getStaticProps from the previous React versions. Those are no longer needed. In return, we get one uniform way of fetching data!

Fetching data

The recommended way of fetching data is using server components, as discussed in the previous article.

According to the docs, there are a lot of benefits from fetching from server components:

  • Access to backend services like database and API that don’t run on the client
  • Keep your secure keys on the server so they don’t leak to the client
  • Fetch and render in the same environment
  • Caching your renders on the server
  • Send less JavaScript to bundle

One important note is that they recommend you refetch data over passing it down. Underwater, they deduplicate your requests and return cached results. So it’s more efficient to have the server do the heavy lifting, and the client requests the data.

How does the fetching work?

They changed to use the fetch Web API, which is fantastic. It’s a super powerful API, so happy to see this choice.

Fetch will always return a promise so we can await the result; by default, it’s a cached request/results.

Let’s show you an example.

In our component, we can create a data fetching function that would look like this.

async function getData() {
  const res = await fetch('https://jsonplaceholder.typicode.com/todos');

  return res.json();
}

Our component itself can then use this function like this.

export default async function AccountPage() {
  const table = await getData();

  return (
    <ul>
      {table.map((todo) => (
        <li key={todo.id}>{todo.title}</li>
      ))}
    </ul>
  );
}

And yep, that’s it, simple. I like the abstraction of thought here to simplify everything using native APIs.

It’s also a great abstraction to move towards a unique concern architecture by simply making the components as small and self-sufficient as possible.

We also get many options around the fetch API that we can leverage, but it might be best to review them once we need them.

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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Next portfolio - Filter by category

30 Nov, 2022 · 5 min read

Next portfolio - Filter by category

A glance at Turbopack

17 Nov, 2022 · 3 min read

A glance at Turbopack

Join 2097 devs and subscribe to my newsletter