Skip to content

Commit

Permalink
Fixes dynamic API routes in SSR (#3006)
Browse files Browse the repository at this point in the history
* Fixes dynamic API routes in SSR

* Adds a changeset
  • Loading branch information
matthewp authored Apr 6, 2022
1 parent 3bf5d84 commit 68e1e2d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/three-donkeys-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes dynamic API routes in SSR
3 changes: 2 additions & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class App {

async #callEndpoint(
request: Request,
_routeData: RouteData,
routeData: RouteData,
mod: ComponentInstance
): Promise<Response> {
const url = new URL(request.url);
Expand All @@ -129,6 +129,7 @@ export class App {
origin: url.origin,
pathname: url.pathname,
request,
route: routeData,
routeCache: this.#routeCache,
ssr: true,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export function get(params) {
return {
body: JSON.stringify(params)
};
}
13 changes: 13 additions & 0 deletions packages/astro/test/ssr-dynamic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
});
});

0 comments on commit 68e1e2d

Please sign in to comment.