Serveless functions with relative url are broken

Hello,

Since a few days we have issues with relative url’s in vercels serverless functions. This problem only happpens on the vercel environment. The setup works locally with vercel dev.

Explanation:

I have serverless function X.js

FILE: /api/x.js

There is some code, in a specific condition it makes a call to another serverless function called y.js.

const { data: testData } = await axios.post('/api/y')

FILE: /api/y.js

Both files exists, and it works locally, but since a few days it doesn’t work on the vercel environment.
When I replace the url to a full url, then it works again:
const { data: testData } = await axios.post('https://some-vercel-domain.com/api/y')

Error log in Vercel:

Uncaught Exception: ReferenceError: DOMException is not defined
    at Dt (/opt/rust/nodejs.js:8:12511)
    at ClientRequest.<anonymous> (/opt/rust/nodejs.js:8:10713)
    at Object.onceWrapper (node:events:628:26)
    at ClientRequest.emit (node:events:525:35)
    at ClientRequest.emit (node:domain:489:12)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
Node.js process exited with exit status: 129. The logs above can help with debugging the issue.

Hi, @martijn788!

It’s good to hear that you have somewhat of a solution for now by using absolute URLs instead of relative ones. :raised_hands:

However, to maintain flexibility across environments, it’s recommended to use environment variables or Vercel’s runtime configuration to dynamically construct the full URL.

For example, you can set a BASE_URL environment variable in your Vercel project settings and use it in your code like this: const { data: testData } = await axios.post($process.env.BASE_URL/api/y).

Your serverless functions should work correctly both in the Vercel environment and locally, solving the problem of relative URL resolution that’s causing your current errors in production.

Could you give that a go?

Hi @pawlean

This used to work like a charm. So can you tell me what changed this flow and why it is broken now? And did we miss an update on that?

Changing to absolute URL’s is a big impact for us now, since we use it in a variety of ways.

I’ll ask internally for you – I missed the part of your post that it used to work. Thanks for your patience! :smiling_face:

In the meantime, did you change anything in your project from the last time this worked?

Thanks in advance!

No, it broke in projects/logic we haven’t touched in weeks.

This cased is solved by upgrading from node 16 to node 20.

1 Like

Thanks for getting back to us with your solution, @martijn788! :smile:

Let us know if we can help with anything else!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.