Does netlify support calling functions on server side?

Hi,

I made a small change in my netlify site (built on gatsby) by adding getServerData function to inject server side props. I am calling a netlify function here. For some reason, I get a 500 Internal server error. I am unable to check anything in the logs. Here is my function

export async function getServerData({ headers }) {
  try {
    const { data } = await axios.get('/api/get_country', { // <--- netlify function
      headers: Object.fromEntries(headers),
    });
    return { props: data, status: 200 };
  } catch (error) {
    console.log(error.toString());
    return {
      props: { country: '', status: 200 },
    };
  }
}

I am wondering, is there support for getServerData on netlify? OR am I calling the netlify function /api/get_country incorrectly?

Server Side functions cannot use relative URLs. You need to use absolute URLs like https://sitename.netlify.app/api/get_country.

Nope, doesn’t seem to work :frowning:

Here’s the change I made. I am guessing it has access to process.env environment variables? I’ve set the corresponding env value to the deploy preview env variable.

DOMAIN_HOST=https://deploy-preview-8--nvckenya.netlify.app

When you access it in the browser it seems to work https://deploy-preview-8--nvckenya.netlify.app/api/get_country but within getServerData, I get a 500 error :frowning:

The 500 is probably because of this error:

visible here: Function details | Functions | Logs | nvckenya | Netlify

not 100% sure, but seems likely. Feel free to provide a HAR file to investigate better.

With that being said, I don’t really understand why you’re doing it this way. Why not implement the functionality from the Netlify Function in your server-side code? I don’t understand the reasoning here for making 2 function invocations for a single request.

I even removed get_country function and moved the logic to getServerData, still doesn’t work! :frowning:

Is there any other way I can inject country information? I read in the redirect docs about netlify being able to redirect based on geoip.

Is it possible to somewhat do this? The user goes to / and then netlify automatically adds a query param with country code? or redirect to path based on the country of origin? /?country=nl or /nl. It could be any country.

You can use Edge Functions for that: Edge Functions API | Netlify Docs

Alright, what am I doing wrong here?

I have this edge function in a deploy preview

// /netlify/edge-functions/get_country.js
export default async (req, { geo }) => {
  return Response.json({ country: geo?.country?.code });
};

export const config = { path: '/api/get_country' };

And I am calling this edge function from ssr-engine (Please tell me if there’s some other way to use edge function in the place of ssr-engine so that I can get the geo location)

// src/pages/index.js
export async function getServerData() {
  try {
    const { data } = await axios.get(
      process.env.DOMAIN_HOST + '/api/get_country',
    );
    return { props: data.country, status: 200 };
  } catch (error) {
    console.log(error.toString());
    return {
      props: { country: '', status: 200 },
    };
  }
}

Although the edge function works, I am facing the same 500 error which I have no clue about. Can you please support me and tell me why this is happening?

https://deploy-preview-8--nvckenya.netlify.app/

@hrishikesh it looks like this is the issue and it’s probably something to do with Netlify, caching and cdn can you please have a look?

I see this in the SSR engine logs (related deploy)

Oct 8, 11:02:00 PM: INIT_START Runtime Version: nodejs:18.v13	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:0229ff5ced939264450549058d8f267110e92677c27063e6dcd781a280f2462b
Oct 8, 11:02:00 PM: 2023-10-08T21:02:00.999Z	undefined	INFO	Preparing Gatsby filesystem {
  from: '/var/task/.cache',
  to: '/tmp/gatsby/.cache',
  rewrites: [
    [ '/var/task/.cache/caches', '/tmp/gatsby/.cache/caches' ],
    [
      '/var/task/.cache/caches-lmdb',
      '/tmp/gatsby/.cache/caches-lmdb'
    ],
    [ '/var/task/.cache/data', '/tmp/gatsby/.cache/data' ]
  ]
}

Oct 8, 11:02:01 PM: 2023-10-08T21:02:01.002Z	undefined	INFO	Start copying data
Oct 8, 11:02:01 PM: 2023-10-08T21:02:01.009Z	undefined	INFO	End copying data
Oct 8, 11:02:02 PM: /bin/sh: lscpu: command not found
Oct 8, 11:02:02 PM: Downloaded datastore from CDN
Oct 8, 11:02:04 PM: error Engine failed to handle request Error: ENOENT: no such file or directory, stat '/opt/build/repo/content/retreats/nvckenya_hero_image2.jpg'
Oct 8, 11:02:04 PM: at Object.statSync (node:fs:1690:3)
Oct 8, 11:02:04 PM: at Object.statSync (/var/task/node_modules/graceful-fs/polyfills.js:318:34)
Oct 8, 11:02:04 PM: at Object.lfs.<computed> (/var/task/node_modules/linkfs/lib/index.js:144:25)
Oct 8, 11:02:04 PM: at Object.statSync (/var/task/.cache/query-engine/index.js:6772:34)
Oct 8, 11:02:04 PM: at opts.dereference.fs.lstatSync.bigint (/var/task/.cache/query-engine/index.js:7408:20)
Oct 8, 11:02:04 PM: at getStatsSync (/var/task/.cache/query-engine/index.js:7410:19)
Oct 8, 11:02:04 PM: at Object.checkPathsSync (/var/task/.cache/query-engine/index.js:7452:33)
Oct 8, 11:02:04 PM: at Object.copySync (/var/task/.cache/query-engine/index.js:7573:38)
Oct 8, 11:02:04 PM: at resolve (/var/task/.cache/query-engine/index.js:328363:14)
Oct 8, 11:02:04 PM: at wrappedTracingResolver (/var/task/.cache/query-engine/index.js:133158:20)
Oct 8, 11:02:04 PM: at executeField (/var/task/.cache/query-engine/index.js:105913:20)
Oct 8, 11:02:04 PM: at executeFields (/var/task/.cache/query-engine/index.js:105835:22)
Oct 8, 11:02:04 PM: at completeObjectValue (/var/task/.cache/query-engine/index.js:106335:10)
Oct 8, 11:02:04 PM: at completeValue (/var/task/.cache/query-engine/index.js:106065:12)
Oct 8, 11:02:04 PM: at /var/task/.cache/query-engine/index.js:105918:9
Oct 8, 11:02:04 PM: at processTicksAndRejections (node:internal/process/task_queues:95:5)
Oct 8, 11:02:04 PM: 34ed0007 Duration: 1499.85 ms	Memory Usage: 337 MB	Init Duration: 1973.89 ms	

Can anyone look at this from netlify side of things?

So nobody at netlify has any clue what the problem is?

Now I feel quite frustrated. What’s the point of having a platform which cannot render a server side api request?

There’s a PR to fix the error 500: fix(gatsby): open lmdb instances in writeable locations in generated ssr/dsg function by pieh · Pull Request #38631 · gatsbyjs/gatsby (github.com) which should be included with the next Gatsby release.