diff --git a/.changeset/three-donkeys-train.md b/.changeset/three-donkeys-train.md new file mode 100644 index 000000000..2ecda8dd3 --- /dev/null +++ b/.changeset/three-donkeys-train.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes dynamic API routes in SSR diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 2175b8e9f..81e19f914 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -119,7 +119,7 @@ export class App { async #callEndpoint( request: Request, - _routeData: RouteData, + routeData: RouteData, mod: ComponentInstance ): Promise { const url = new URL(request.url); @@ -129,6 +129,7 @@ export class App { origin: url.origin, pathname: url.pathname, request, + route: routeData, routeCache: this.#routeCache, ssr: true, }); diff --git a/packages/astro/test/fixtures/ssr-dynamic/src/pages/api/products/[id].js b/packages/astro/test/fixtures/ssr-dynamic/src/pages/api/products/[id].js new file mode 100644 index 000000000..2d619ee74 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-dynamic/src/pages/api/products/[id].js @@ -0,0 +1,6 @@ + +export function get(params) { + return { + body: JSON.stringify(params) + }; +} diff --git a/packages/astro/test/ssr-dynamic.test.js b/packages/astro/test/ssr-dynamic.test.js index 01ff3d2be..47157e10b 100644 --- a/packages/astro/test/ssr-dynamic.test.js +++ b/packages/astro/test/ssr-dynamic.test.js @@ -27,6 +27,14 @@ describe('Dynamic pages in SSR', () => { return html; } + async function fetchJSON(path) { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com' + path); + const response = await app.render(request); + const json = await response.json(); + return json; + } + it('Do not have to implement getStaticPaths', async () => { const html = await fetchHTML('/123'); const $ = cheerioLoad(html); @@ -38,4 +46,9 @@ describe('Dynamic pages in SSR', () => { const $ = cheerioLoad(html); expect($('link').length).to.equal(1); }); + + it('Dynamic API routes work', async () => { + const json = await fetchJSON('/api/products/33'); + expect(json.id).to.equal('33'); + }); });