jlarmstrongiv / astro-i18n-aut

The i18n integration for Astro 🧑‍🚀
https://www.npmjs.com/package/astro-i18n-aut
MIT License
132 stars 12 forks source link

500 Error on api POST route on Vercel #47

Closed Maximillion195 closed 9 months ago

Maximillion195 commented 10 months ago

Hey, was so happy to find your solution. Found it crazy that you couldn't use one template for all the translations.

Hoping you could help with an an issue i'm having when it's deployed to Vercel and I go to use an api POST endpoint that I created i'm getting a 500 error and it shows this in the logs.

Pretty new to Astro and exhausted everything I can think of trying. Let me know if you need anymore info

02:11:27 [ERROR] TypeError: Cannot read properties of undefined (reading 'at')
    at removeTrailingSlash (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:37:294)
    at file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:37:968
    at applyHandle (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:21:22)
    at file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:18:12
    at applyHandle (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:21:22)
    at file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:23:18
    at file:///var/task/vercel/path0/.vercel/output/_functions/entry.mjs:964:25
    at applyHandle (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:21:22)
    at file:///var/task/vercel/path0/.vercel/output/_functions/chunks/_astro-internal_middleware_QRNLHTyu.mjs:18:12
    at callMiddleware (file:///var/task/vercel/path0/.vercel/output/_functions/entry.mjs:234:27)

I set the trailing slash to what was specific in the docs

astro.config

trailingSlash: 'never',
build: {
    format: 'file',
},
output: 'hybrid',

API Endpoint - /api/upload

export const prerender = false;

import type { APIRoute } from 'astro';

export const POST: APIRoute = async ({ request }) => {

}
jlarmstrongiv commented 10 months ago

All /api routes are omitted from the translations by default

The middleware only really takes effect when it’s a page route with a default locale like /en/about redirects to /about

Are you using other middleware? Does it work when you temporarily remove the astro-i18n-aut package? You can also try disabling the astro-i18n-aut middleware by setting redirectDefaultLocale to false

I am using .at() in some of the helper functions, but they are all strings that should be defined.

If you have an example reproduction or public project on GitHub, I could take a closer look. If you do, I’ll gladly re-open this issue

Maximillion195 commented 10 months ago

I tried removing the astro-i18n-aut package and the api route starts working again. Seems to be an issue with the baseUrl some how.

I have an example reproduction. If you deploy this to Vercel and click the Test button you should be able to see the error in the Vercel logs.

Maximillion195 commented 10 months ago

I didn't get to the root of it but it seems like the baseUrl was coming up as undefined when deployed on Vercel. I made a really hacky fix on the removeTrailingSlash function to prevent it from erroring.

export const BASE_URL = virtualConfig.BASE_URL;

export function removeTrailingSlash(url: string) {
  if (!url) return '';
  return url?.at(-1) === "/" ? url.slice(0, -1) : url;
}
jlarmstrongiv commented 9 months ago

Thanks @Maximillion195 ! I have defaulted the BASE_URL to / in 0.7.0

Unfortunately, I don’t have Vercel, so if it doesn’t work with your example reproduction, please let me know and I’ll re-open the issue