Fixes dynamic API routes in SSR (#3006)

* Fixes dynamic API routes in SSR

* Adds a changeset
This commit is contained in:
Matthew Phillips 2022-04-06 13:01:22 -04:00 committed by GitHub
parent 3bf5d84016
commit 68e1e2dd31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes dynamic API routes in SSR

View file

@ -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);
@ -129,6 +129,7 @@ export class App {
origin: url.origin,
pathname: url.pathname,
request,
route: routeData,
routeCache: this.#routeCache,
ssr: true,
});

View file

@ -0,0 +1,6 @@
export function get(params) {
return {
body: JSON.stringify(params)
};
}

View file

@ -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');
});
});