Sporadic 502s during build

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’,
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

Example API call:
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.

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?

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?

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:

Hope this helps!

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.

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!

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.

Great! we’ll be here if you need us

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.

Hi @andreas1 :wave:t6: ,

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. :+1:t5: I hope this helps! ( :

1 Like

Hi @SamO

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

Cheers!

Thanks for confirming! @andreas1 and happy building.