Complete the code to fetch data inside a Next.js server component.
const data = await fetch([1]);
const json = await data.json();In Next.js server components, fetch takes a URL string as argument. So you provide the URL inside fetch.
Complete the code to fetch JSON data with caching disabled in a server component.
const res = await fetch('/api/data', { cache: [1] }); const data = await res.json();
Using cache: 'no-store' disables caching and fetches fresh data every time in Next.js server components.
Fix the error in the fetch call to correctly handle a POST request in a server component.
const res = await fetch('/api/data', { method: [1] });
To send data to the server, the HTTP method should be 'POST'.
Fill both blanks to fetch data and convert the response to JSON in a server component.
const response = await fetch([1]); const data = await response.[2]();
The fetch function takes a URL string. To parse JSON from the response, call the json() method without quotes.
Fill all three blanks to fetch data with POST method, send JSON body, and parse JSON response in a server component.
const res = await fetch('/api/data', { method: [1], headers: { 'Content-Type': [2] }, body: JSON.stringify({ name: 'John' }) }); const result = await res.[3]();
Use method 'POST' to send data, set header 'Content-Type' to 'application/json' to indicate JSON body, and parse response with json() method.