Aliaz
Aliaz

Reputation: 11

How can I set a timeout for requests to the Next.js server?

Suppose there’s a simple Next.js (version 14.2.5) app that calls a backend API using a server action triggered by a button press. When the button is clicked, the browser sends a request to the Next.js server (the first request), and then the server calls the API (the second request). While I can set a timeout for the second request, how can I set a timeout for the first request?

I tried using maxDuration from the route segment configs in the page.tsx where the button is located, but it didn’t work. It’s worth noting that my app is self-hosted and not deployed on Vercel or any other platform. Is it necessary to deploy the app on Vercel for this configuration to function properly?

Here’s my code:

actions.ts:

export const sendRequest = async () => {
  return await fetch(url, {body, method: "POST"})
}

component.tsx:

"use client"
export default function Component() {
  return (
    <button onClick={async () => {
        await sendRequest();
      }
    <button />
  )
}

page.tsx:

export const maxDuration = 5;

export default async funciton Page() {
  return (<Component />)
}

Upvotes: 0

Views: 212

Answers (0)

Related Questions