Accessing functions.included_files in Astro SSR

Hi,

I’m running an Astro SSR function which calls a function that looks for icons in the /public/icon directory:

import { promises as fs } from 'node:fs';

export const getFavicons = async () => {

	let files = [];
	try {
		files = await fs.readdir('./public/icon');
	} catch (error) {
		console.error("Error reading icon directory:", error);
	}

	return files.filter((file) => {
		return file.endsWith('.svg') ||
			file.endsWith('.png') ||
			file.endsWith('.ico');
	});
};

This works locally, but fails when running on Netlify. Having reviewed the documentation and numerous posts in the forum, I’ve updated my netlify.toml file to:

[functions]
  node_bundler = "esbuild"
  included_files = ["public/*/*"]

Unfortunately this still returns Error: ENOENT: no such file or directory. I thought perhaps the issue is that the /public assets are moved to / by the astro build, so I also tried included_files = ["icon/*"]but this didn’t work either.

Any ideas where I’m going wrong?

Which site/deploy? Most likely your path is wrong.

Hi @hrishikesh,

The site is https://www.firstfootsoldiers.com/.

The path does appear to be wrong yes, but I can’t spot why/how. By using included_files = ["public/*/*"]my assumption is that all files/directories inside /public should be included and readable. My expectation is that this is maybe a quirk of the Astro build and files in /public locally aren’t actually inside /public when the site is built. I did try included_files = ["icon/*"]but this didn’t work either.

Looks like Astro provides a built-in config for this: astro/packages/integrations/netlify/src/index.ts at main · withastro/astro. You should use an absolute directory like path.join(process.cwd(), ‘public’).

Excellent thanks @hrishikesh that appears to have solved the issue!

import path from 'node:path';
import { promises as fs } from 'node:fs';

export const getFavicons = async () => {

	let files = [];

	try {
		files = await fs.readdir(path.join(process.cwd(), 'public/icon'));
	} catch (error) {
		console.error("Error reading icon directory:", error);
	}

	return files.filter((file) => {
		return file.endsWith('.svg') ||
			file.endsWith('.png') ||
			file.endsWith('.ico');
	});
};


1 Like