Skip to content

Static APIRoutes can't receive props from getStaticPaths #4920

@Jutanium

Description

@Jutanium

What version of astro are you using?

1.3.0

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

pnpm

What operating system are you using?

Mac

Describe the Bug

Custom endpoints/API Routes support getStaticPaths for dynamic routing in static mode. The params in each route can be accessed using the params property of APIContext, but there is no way to access the props returned from getStaticPaths.

The fix would expose props on APIContext.

// src/pages/[id].json.ts

import type { APIRoute } from 'astro';

export const get: APIRoute = async ({ params, request, props }) => {
  const id = params.id;
  console.log(props); //undefined
  return ...
};

export function getStaticPaths() {
  return [
    { params: { id: '0' }, props: { name: 'Sarah' } },
    { params: { id: '1' }, props: { name: 'Chris' } },
    { params: { id: '2' }, props: { name: 'Fuzzy' } },
  ];
}

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-e6xf7j?file=src%2Fpages%2F[id].json.ts%3AL8&on=stackblitz

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: nice to haveNot breaking anything but nice to have (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions