So, I’ve been hosting on Netlify with no particular issues for some time now. For a small experiment with an adjacent project that wants to share data from various collections, I created an API route that emanates a JSON file of the collection:
export const prerender = false;
import type { APIRoute } from "astro";
import { getCollection } from 'astro:content';
export const GET: APIRoute = async () => {
try {
// Get the collection
const collection = await getCollection('blog');
// Simple sanitization without complex types
const sanitizedData = collection.map(entry => {
// Convert to plain object and handle Date objects
const plainData = JSON.parse(JSON.stringify({
id: entry.id,
slug: entry.body,
collection: entry.collection,
...entry.data
}));
return plainData;
});
// Return JSON response
return new Response(
JSON.stringify(sanitizedData, null, 2),
{
headers: {
"Content-Type": "application/json",
},
}
);
} catch (error) {
console.error("Error generating blog JSON:", error);
return new Response(
JSON.stringify({ error: "Failed to generate collection JSON" }),
{
status: 500,
headers: {
"Content-Type": "application/json",
},
}
);
}
};
This uses a collection helper that simply cobbles the JSON together.
import type { CollectionEntry } from 'astro:content';
/**
* Sanitizes collection entries to prevent circular references in JSON
* @param entries Collection entries from any Astro collection
* @returns Array of sanitized entries safe for JSON serialization
*/
export function sanitizeCollectionForJSON(entries: any[]) {
return entries.map(entry => {
const { id, body, collection } = entry;
// Get data and sanitize Date objects
const data = JSON.parse(JSON.stringify(entry.data));
return {
id,
body,
collection,
...data
};
});
}
The endpoint works as expected in dev locally. But when I push to Netlify, I get 404s. I’ve seen one other thread here in Support that seems to indicate, well — that I’m doing what is meant to be done. At the moment I see no glaring issues and wonder if this is a Netlify issue (I have the latest @astro/netlify adapter 6.2.5) or if I am overlooking something else. My understanding of the endpoints is that I simply place the function/handler in src/pages/api, which I’ve done. Any thoughts on what else I might nudge? I’m running output: ‘static’, with my understanding that this is okay so long as I add export const prerender = false
to my endpoint file.
Finally, when I push this to deploy a branch, Netlify is stuck in the uploading phase, well — seems like it’ll just stay there forever. (I’ve left these alone, and they are still trying to upload, stuck at 90%)