Hi there, I’m having trouble getting netlify to deploy and run my functions. I created a cache warmer but netlify does seem to want to recognize it. I have the following code in a file called cacheWarmer.js in client/netlify/functions, to which it is mapped to in build configuraitons:
import axios from 'axios';
const handler = async (event, context) => {
const fetchDatabaseIds = async () => {
console.log("Starting to fetch database IDs...");
const queryId = "#####################";
let allDatabaseIds = [];
let hasNextPage = true;
let endCursor = null;
while (hasNextPage) {
try {
const response = await axios.get(
"https://########.com/graphql",
{
params: {
queryId: queryId,
variables: JSON.stringify({
first: 50,
after: endCursor,
}),
},
}
);
const products = response.data.data.products;
const productIds = products.nodes.map(product => product.databaseId);
allDatabaseIds = [...allDatabaseIds, ...productIds];
hasNextPage = products.pageInfo.hasNextPage;
endCursor = products.pageInfo.endCursor;
console.log(`Fetched ${productIds.length} databaseIds, total: ${allDatabaseIds.length}`);
} catch (error) {
console.error("Error fetching data:", error.message);
break;
}
}
return allDatabaseIds;
};
const fetchProductPages = async (databaseIds) => {
const queryId = "#################";
let productDetails = [];
for (let databaseId of databaseIds) {
try {
const productUrl = `https://##################.com/graphql?queryId=${queryId}&variables=${JSON.stringify({ id: databaseId })}`;
console.log(`Pinging product URL: ${productUrl}`);
const { data } = await axios.get(productUrl);
productDetails.push(data);
console.log(`Fetched product data for ${databaseId}`);
} catch (error) {
console.error(`Error fetching data for product ${databaseId}:`, error.message);
}
}
console.log("All product details fetched:", productDetails);
return productDetails;
};
try {
const databaseIds = await fetchDatabaseIds();
const products = await fetchProductPages(databaseIds);
return {
statusCode: 200,
body: JSON.stringify({ message: "Cachewarmer executed successfully", products }),
};
} catch (error) {
console.error("Error in cachewarmer function:", error.message);
return {
statusCode: 500,
body: JSON.stringify({ message: "Cachewarmer execution failed", error: error.message }),
};
}
};
export default handler;
I have a netlify toml file also in client that looks like this
[build]
command = “vite build”
publish = “dist”
base = “client”
[functions]
directory = “client/netlify/functions”
external_node_modules = [
“axios”
]
included_files = [“client/netlify/functions/**/*.js”]
[functions.“cacheWarmer”]
external_node_modules = [“axios”]
included_files = [“client/netlify/functions/cacheWarmer.js”]
Can anyone help? thanks!