# Sporadic 502s during build

**URL:** <https://answers.netlify.com/t/sporadic-502s-during-build/85977>\
**Category:** Support\
**Tags:** nextjs\
**Created:** [February 20, 2023, 12:50pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977 "2023-02-20T12:50:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreas1](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andreas1/32/44847_2.png) [@andreas1](https://answers.netlify.com/u/andreas1)\
**Post date:** [February 20, 2023, 12:50pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/1 "2023-02-20T12:50:47Z")

</div>

Hi!

I got some problems while building my NextJS site. During the build process I _sometimes_ get 502 responses for API calls made in getStaticProps.

**Example response below:**

1:11:04 PM: response: {  
1:11:04 PM: status: 502,  
1:11:04 PM: statusText: ‘Bad Gateway’,  
1:11:04 PM: headers: {  
1:11:04 PM: age: ‘10’,  
1:11:04 PM: ‘cache-control’: ‘no-cache’,  
1:11:04 PM: ‘content-length’: ‘115’,  
1:11:04 PM: ‘content-type’: ‘text/plain; charset=utf-8’,  
1:11:04 PM: date: ‘Mon, 20 Feb 2023 12:10:54 GMT, Mon, 20 Feb 2023 12:11:04 GMT’,  
1:11:04 PM: server: ‘Netlify’,  
1:11:04 PM: ‘strict-transport-security’: ‘max-age=31536000’,  
1:11:04 PM: vary: ‘Accept-Encoding’,  
1:11:04 PM: ‘x-nf-request-id’: ‘01GSQBQTNG8KRAAZ3GSRRBSPAV’,  
1:11:04 PM: connection: ‘close’  
1:11:04 PM: },  
1:11:04 PM: config: {  
1:11:04 PM: transitional: [Object],  
1:11:04 PM: adapter: [Function: httpAdapter],  
1:11:04 PM: transformRequest: [Array],  
1:11:04 PM: transformResponse: [Array],  
1:11:04 PM: timeout: 0,  
1:11:04 PM: xsrfCookieName: ‘XSRF-TOKEN’,  
1:11:04 PM: xsrfHeaderName: ‘X-XSRF-TOKEN’,  
1:11:04 PM: maxContentLength: -1,  
1:11:04 PM: maxBodyLength: -1,  
1:11:04 PM: validateStatus: [Function: validateStatus],  
1:11:04 PM: headers: [Object],  
1:11:04 PM: method: ‘get’,  
1:11:04 PM: url: ‘[https://stage.finansvalp.se/api/pages/findUri/investeringar](https://stage.finansvalp.se/api/pages/findUri/investeringar)’,  
1:11:04 PM: data: undefined  
1:11:04 PM: },  
1:11:04 PM: request: \<ref \*1\> ClientRequest {  
1:11:04 PM: \_events: [Object: null prototype],  
1:11:04 PM: \_eventsCount: 7,  
1:11:04 PM: \_maxListeners: undefined,  
1:11:04 PM: outputData: ,  
1:11:04 PM: outputSize: 0,  
1:11:04 PM: writable: true,  
1:11:04 PM: destroyed: false,  
1:11:04 PM: \_last: true,  
1:11:04 PM: chunkedEncoding: false,  
1:11:04 PM: shouldKeepAlive: false,  
1:11:04 PM: maxRequestsOnConnectionReached: false,  
1:11:04 PM: \_defaultKeepAlive: true,  
1:11:04 PM: useChunkedEncodingByDefault: false,  
1:11:04 PM: sendDate: false,  
1:11:04 PM: \_removedConnection: false,  
1:11:04 PM: \_removedContLen: false,  
1:11:04 PM: \_removedTE: false,  
1:11:04 PM: strictContentLength: false,  
1:11:04 PM: \_contentLength: 0,  
1:11:04 PM: \_hasBody: true,  
1:11:04 PM: \_trailer: ‘’,  
1:11:04 PM: finished: true,  
1:11:04 PM: \_headerSent: true,  
1:11:04 PM: \_closed: false,  
1:11:04 PM: socket: [TLSSocket],  
1:11:04 PM: \_header: ‘GET /api/pages/findUri/investeringar HTTP/1.1\r\n’ +  
1:11:04 PM: ‘Accept: application/json, text/plain, _/_\r\n’ +  
1:11:04 PM: ‘User-Agent: axios/0.25.0\r\n’ +  
1:11:04 PM: ‘Host: stage.finansvalp.se\r\n’ +  
1:11:04 PM: ‘Connection: close\r\n’ +  
1:11:04 PM: ‘\r\n’,  
1:11:04 PM: \_keepAliveTimeout: 0,  
1:11:04 PM: \_onPendingData: [Function: nop],  
1:11:04 PM: agent: [Agent],  
1:11:04 PM: socketPath: undefined,  
1:11:04 PM: method: ‘GET’,  
1:11:04 PM: maxHeaderSize: undefined,  
1:11:04 PM: insecureHTTPParser: undefined,  
1:11:04 PM: path: ‘/api/pages/findUri/investeringar’,  
1:11:04 PM: \_ended: true,  
1:11:04 PM: res: [IncomingMessage],  
1:11:04 PM: aborted: false,  
1:11:04 PM: timeoutCb: null,  
1:11:04 PM: upgradeOrConnect: false,  
1:11:04 PM: parser: null,  
1:11:04 PM: maxHeadersCount: null,  
1:11:04 PM: reusedSocket: false,  
1:11:04 PM: host: ‘stage.finansvalp.se’,  
1:11:04 PM: protocol: ‘https:’,  
1:11:04 PM: \_redirectable: [Writable],  
1:11:04 PM: [Symbol(kCapture)]: false,  
1:11:04 PM: [Symbol(kBytesWritten)]: 0,  
1:11:04 PM: [Symbol(kEndCalled)]: true,  
1:11:04 PM: [Symbol(kNeedDrain)]: false,  
1:11:04 PM: [Symbol(corked)]: 0,  
1:11:04 PM: [Symbol(kOutHeaders)]: [Object: null prototype],  
1:11:04 PM: [Symbol(kUniqueHeaders)]: null  
1:11:04 PM: },  
1:11:04 PM: data: {  
1:11:04 PM: errorMessage: ‘2023-02-20T12:11:04.931Z 7e777685-d3b4-440d-87a5-d4cb28fec1f0 Task timed out after 10.01 seconds’  
1:11:04 PM: }  
1:11:04 PM: },

**Example x-nf-request-id ids below:**

01GSQBQTN5674FHH17F6MBWKVY  
01GSQBQTM0J1G20CQBR26CSXSQ  
01GSQBQTP270XH5EVAXRWC481D  
01GSQBQTNG8KRAAZ3GSRRBSPAV

**Site:**  
[https://stage.finansvalp.se](https://stage.finansvalp.se)

**Example API call:**  
[https://stage.finansvalp.se/api/pages/findUri/investeringar](https://stage.finansvalp.se/api/pages/findUri/investeringar)

Trying to figure out if this is Netlify related before I move on to checking my WP backend that I am querying for data.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![gualter](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/gualter/32/35068_2.png) [@gualter](https://answers.netlify.com/u/gualter)\
**Post date:** [February 20, 2023, 12:54pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/2 "2023-02-20T12:54:38Z")

</div>

hi @andreas1

welcome to our forums! that error relates to the timeout of functions execution which is 10 seconds by default. I’ve increased this to 26 seconds, our network max.

Can you try now, please?

---

<div class="post-metadata">

**Author:** ![andreas1](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andreas1/32/44847_2.png) [@andreas1](https://answers.netlify.com/u/andreas1)\
**Post date:** [February 20, 2023, 1:03pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/3 "2023-02-20T13:03:24Z")

</div>

Hi @gualter

Thanks. I did deploy again just now and it works. However, this issue has been sporadic, so it wasn’t always happening.

Are execution times usually longer during the build process? If I call the route myself it rarely takes longer than a second. Could this extended execution time be due to latency between the CDN and my server provider?

---

<div class="post-metadata">

**Author:** ![gualter](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/gualter/32/35068_2.png) [@gualter](https://answers.netlify.com/u/gualter)\
**Post date:** [February 20, 2023, 2:14pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/4 "2023-02-20T14:14:31Z")

</div>

> Are execution times usually longer during the build process?

Not necessarly. Can you trace this to a specific page(s) at build time? What are you doing on that getStaticProps call?

Perhaps you can add a few console.logs to trace the time it takes on each step as described on our article:

> [@\[Support Guide\] Why is my function taking long or timing out?](https://answers.netlify.com/t/support-guide-why-is-my-function-timing-out/71689):
>
> In this guide, we’ll be talking about synchronous functions. We won’t be covering Edge Functions or Background Functions since those are not bound by the same limits as standard synchronous functions or Scheduled Functions are. Netlify Functions are a core feature of our product. By delegating your server-side logic to them, you leverage serverless functions that are [version-controlled, built, and deployed along with the rest of your Netlify site](https://docs.netlify.com/functions/overview/). However, [there are some limitations on functio…](https://docs.netlify.com/functions/overview/#default-deployment-options)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![andreas1](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andreas1/32/44847_2.png) [@andreas1](https://answers.netlify.com/u/andreas1)\
**Post date:** [February 20, 2023, 2:54pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/5 "2023-02-20T14:54:40Z")

</div>

I can’t isolate it to a specific page / pages, it seemed to happen at random.

I’m calling my Wordpress API to get some page data to use as props for the pages. I notice now that I’m querying a lot more data than needed though. So I could improve that a bit. This project is still under heavy development.

Are you able to change location for the serverless functions? I’m located in Sweden and the site is intended to a Swedish audience. The WP server is also located in Sweden.

---

<div class="post-metadata">

**Author:** ![gualter](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/gualter/32/35068_2.png) [@gualter](https://answers.netlify.com/u/gualter)\
**Post date:** [February 20, 2023, 3:12pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/6 "2023-02-20T15:12:27Z")

</div>

Certainly. I’ve changed the region to `eu-central-01`. Please redeploy the site to make it effective.

As for the page queries, I definitely recommend only querying for the necessary to reduce the time it takes for the function to execute

Hope this helps!

---

<div class="post-metadata">

**Author:** ![andreas1](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andreas1/32/44847_2.png) [@andreas1](https://answers.netlify.com/u/andreas1)\
**Post date:** [February 20, 2023, 3:19pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/7 "2023-02-20T15:19:56Z")

</div>

Thanks! The fixes that have been implemented as well the improvements to the API call should hopefully fully resolve the issue.

I’ll get back to this if the issue would reappear.

---

<div class="post-metadata">

**Author:** ![gualter](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/gualter/32/35068_2.png) [@gualter](https://answers.netlify.com/u/gualter)\
**Post date:** [February 20, 2023, 3:34pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/8 "2023-02-20T15:34:59Z")

</div>

Great! we’ll be here if you need us

---

<div class="post-metadata">

**Author:** ![andreas1](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andreas1/32/44847_2.png) [@andreas1](https://answers.netlify.com/u/andreas1)\
**Post date:** [February 22, 2023, 1:49pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/9 "2023-02-22T13:49:01Z")

</div>

Hi again @gualter

Would you be able to increase the function exec time as well as set the location to eu-central for my site tmp.finansvalp.se as well?

Let me know if there’s a better way for me to make these types of requests.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![SamO](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/samo/32/41847_2.png) [@SamO](https://answers.netlify.com/u/SamO)\
**Post date:** [February 22, 2023, 4:44pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/10 "2023-02-22T16:44:45Z")

</div>

Hi @andreas1 👋🏿 ,

Welcome back, I increased your function timeout to the max and set the location to eu-central-1. Can you please re-deploy to activate?

In the future you can create a new topic OR find a thread where others have the same request and just respond on there. 👍🏾 I hope this helps! ( :

---

<div class="post-metadata">

**Author:** ![andreas1](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andreas1/32/44847_2.png) [@andreas1](https://answers.netlify.com/u/andreas1)\
**Post date:** [February 23, 2023, 8:23am UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/11 "2023-02-23T08:23:53Z")

</div>

Hi @SamO

Thanks a lot! Re-deployed just now and it looks good.

Cheers!

---

<div class="post-metadata">

**Author:** ![SamO](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/samo/32/41847_2.png) [@SamO](https://answers.netlify.com/u/SamO)\
**Post date:** [February 23, 2023, 3:10pm UTC](https://answers.netlify.com/t/sporadic-502s-during-build/85977/12 "2023-02-23T15:10:14Z")

</div>

Thanks for confirming! @andreas1 and happy building.
