Best Practice for Astro Endpoints

Published:

Astro endpoints are files with a .js or .ts extension inside the pages folder.

# Where an endpoint lives
root/
  |- src/
    |- pages/
      |- index.astro
      |- endpoint.js # This is an endpoint

Here’s the thing: Astro strips away query parameters from endpoints if you’re in a static rendering mode.

So, unless you’re using SSR for all your pages, you’ll need to ensure your endpoints are not pre-rendered.

export const prerender = false 

// All your endpoints go here
export function GET () {/* ... */}
export function POST () {/* ... */}
export function PATCH () {/* ... */}
export function DELETE () {/* ... */}

This will save you lots of headaches when debugging Astro endpoints.

Want to become a better Frontend Developer?

Don’t worry about where to start. I’ll send you a library of articles frontend developers have found useful!

  • 60+ CSS articles
  • 90+ JavaScript articles

I’ll also send you one article every week to help you improve your FED skills crazy fast!