#How do I enable top level await in next.js v13.5.4?

1 messages · Page 1 of 1 (latest)

ivory talon
#

I'm working on a little experimental app and I need to use top level await.

Is there a way to enable it in next.js v13.5.4?

worthy haloBOT
#

Hi @ivory talon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use top-level await in Next.js version 13.5.4. This feature is particularly useful when you are fetching data in your application. Here's an example of how you can use it:

export default async function Page() {
 const res = await fetch('<https://api.example.com/...'>);
 // The return value is *not* serialized
 // You can use Date, Map, Set, etc.
 const data = res.json();
 
 return '...';
}

This code is part of the new App Router feature in Next.js 13.4, which allows you to fetch data using the familiar async and await syntax. By default, all components are React Server Components, so data fetching happens securely on the server.

You can find more details about this in the Next.js 13.4 announcement blog post (https://nextjs.org/blog/next-13-4).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: