Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes dynamic API routes in SSR #3006

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});