lambda-packages
lambda-packages copied to clipboard
Unable to `return` from SSR dynamic page
What version of astro are you using?
1.1.5
Are you using an SSR adapter? If so, which one?
Vercel
What package manager are you using?
yarn
What operating system are you using?
Mac
Describe the Bug
I have code like this inside my SSR (vercel adapter) page:
const { slug } = Astro.params;
const found = data.find((o) => o.slug === slug);
if (!found) {
Astro.response.status = 404;
Astro.response.statusText = "Not found";
return;
}
const obj = found;
When I run it (astro dev) I get:
ERROR: Top-level return cannot be used inside an ECMAScript module
The error seems to stem from esbuild. If I remove return from the if clause it works, but I need for two reasons:
- In TypeScript
foundisObj | undefinedbutobjcan't beundefinedafter theifcheck. So it can be safely used and type-checked inside the template. - I want to early-return with 404 if the object is not found and not execute any further SSR code that might depend on the data object available.
Maybe I missed anything and there is another way to handle SSR. I also couldn't use getStaticPaths's props because they are ignored in server mode, which I understand is required for Vercel adapter.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-1ajmmy?file=src/pages/[slug].astro
Participation
- [ ] I am willing to submit a pull request for this issue.