i was working on a project where i have nuxt ./server/api/mdx.ts which is responsible for loading a file when requested i tried loading using fs which resulted in errors because the server functions run in a separate enviroment on vercel. then after searching around a bit i decided to have a nitro asset setup like this:
nuxt.config.ts:
// the rest of nuxt setup
nitro: {
storage: {
mdxassets: {
driver: 'fs',
base: "./documentation"
},
},
compressPublicAssets: {
brotli: true,
gzip: true
}
},
// the rest of nuxt setup
mdx.ts:
export default defineEventHandler(async (event) => {
try {
// Ensure the request method is POST
if (event.req.method !== 'POST') {
console.warn('Mdx Ignoring non-POST request:', {
method: event.req.method,
url: event.req.url,
});
return {
statusCode: 405,
body: 'Method Not Allowed',
};
}
// Read the file path from the request body
const { filePath } = await readBody(event);
if (!filePath) {
console.error('No file path provided');
return {
statusCode: 400,
body: 'No file path provided',
};
}
// Use the storage API to fetch the file
const storage = useStorage('mdxassets');
let file = await storage.getItem(filePath).then((x) => x?.toString());
if (!file) {
console.log('What was found: ', storage);
console.error('File not found or could not be read:', filePath);
return {
statusCode: 404,
body: 'File not found or could not be read',
};
}
console.log('File Content:', file);
this setup works fine on my pc bot in dev mode and build but doesn't work on vercel and with useSthorage i don't know the errors because it doesn't give me one but it silently isn't able to load it just on vercel. with my file i am trying to load is in the root dir. of my project inside a folder called docs.