#database call

1 messages · Page 1 of 1 (latest)

valid yarrow
#

how to use an aws sdk call to the database and have that data be static at build time? in app router

timber ironBOT
#

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

#

To fetch data from an AWS SDK call and have it be static at build time in the app directory, you can use the generateStaticParams function. This function is used in combination with dynamic route segments to statically generate routes at build time instead of on-demand at request time.

Here's an example of how you might use it:

export async function generateStaticParams() {
 const data = await awsSdkCall() // replace this with your AWS SDK call
 
 return data.map((item) => ({
   id: item.id,
 }))
}

In this example, awsSdkCall() should be replaced with your actual AWS SDK call that fetches data from your database. The function maps over the data returned from the AWS SDK call and returns an array of objects, each containing an id property. This id is used as a parameter for your dynamic route.

The generateStaticParams function also has the benefit of smart retrieval of data. If content is fetched within the generateStaticParams function using a fetch request, the requests are automatically deduplicated. This means a fetch request with the same arguments across multiple generateStaticParams, Layouts, and Pages will only be made once, which decreases build times.

For more information and advanced use cases, see the generateStaticParams server function documentation (https://nextjs.org/docs/app/api-reference/functions/generate-static-params).

Please note that the actual AWS SDK call is not provided in the extracts, so you'll need to replace awsSdkCall() with your actual AWS SDK call. Also, ensure that your AWS SDK call is asynchronous and returns a Promise that resolves with the data you want to fetch.

#

@valid yarrow kapa.ai is still learning and improving, please let us know how it did by reacting below

valid yarrow
#

@timber iron what does fetching at "build time" mean?

timber ironBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Fetching at "build time" refers to the process of retrieving data from a source (like a database or an API) during the build process of your application, before it is deployed. This is a common practice in Static Site Generation (SSG), a method used in Next.js.

In Next.js, you can fetch data at build time using the getStaticProps function. This function runs at build time on the server-side and fetches the necessary data for your page. The data returned by getStaticProps is then passed as a prop to your page and can be used to pre-render the page with the fetched data.

Here's an example of how you might use getStaticProps:

export async function getStaticProps() {
  // Fetch data from an API
  const res = await fetch('<https://.../data'>)
  const data = await res.json()

  // Return the data as a prop to your page
  return { props: { data } }
}

export default function Page({ data }) {
  // Render your page with the fetched data
  // ...
}

In this example, getStaticProps fetches data from an API at build time. The fetched data is then passed as a prop to the Page component, which can use the data to pre-render the page.

The advantage of fetching data at build time is that all the necessary data for your page is fetched and ready before the page is requested by a user. This can lead to faster page load times since the browser doesn't need to wait for data to be fetched at the time of the request.

However, because the data is fetched at build time, any changes to the data after the build won't be reflected on the page until the next build. This makes fetching at build time ideal for data that doesn't change often.

For more information on fetching data at build time in Next.js, see the Data Fetching documentation (https://nextjs.org/docs/pages/building-your-application/data-fetching).

#

@valid yarrow kapa.ai is still learning and improving, please let us know how it did by reacting below

valid yarrow
#

@timber iron can you fetch at build time in an api in next.js app router? some parts of the api are static, others fetched at request time in the api

timber ironBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you can fetch data at build time in the Next.js App Router using the generateStaticParams function. This function is used to statically generate routes at build time. Here's an example:

export async function generateStaticParams() {
 const data = await fetch('<https://.../data'>).then((res) => res.json())
 
 return data.map((item) => ({
   id: item.id,
 }))
}

In this example, generateStaticParams fetches data from an API at build time. The fetched data is then used to generate static routes for each item in the data.

For parts of the API that need to be fetched at request time, you can use dynamic routes in Next.js. In a dynamic route, components are rendered on the server at request time. Here's an example of a dynamic route handler:

import { NextResponse } from 'next/server'
 
export async function GET() {
 const res = await fetch('<https://data.mongodb-api.com/...',> {
 headers: {
 'Content-Type': 'application/json',
 'API-Key': process.env.DATA_API_KEY,
 },
 })
 const data = await res.json()
 
 return NextResponse.json({ data })
}

In this example, the GET function fetches data from an API at request time and returns the data as a JSON response.

Sources:


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