Fixes dynamic API routes in SSR (#3006)
* Fixes dynamic API routes in SSR * Adds a changeset
This commit is contained in:
parent
3bf5d84016
commit
68e1e2dd31
4 changed files with 26 additions and 1 deletions
5
.changeset/three-donkeys-train.md
Normal file
5
.changeset/three-donkeys-train.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes dynamic API routes in SSR
|
|
@ -119,7 +119,7 @@ export class App {
|
||||||
|
|
||||||
async #callEndpoint(
|
async #callEndpoint(
|
||||||
request: Request,
|
request: Request,
|
||||||
_routeData: RouteData,
|
routeData: RouteData,
|
||||||
mod: ComponentInstance
|
mod: ComponentInstance
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
@ -129,6 +129,7 @@ export class App {
|
||||||
origin: url.origin,
|
origin: url.origin,
|
||||||
pathname: url.pathname,
|
pathname: url.pathname,
|
||||||
request,
|
request,
|
||||||
|
route: routeData,
|
||||||
routeCache: this.#routeCache,
|
routeCache: this.#routeCache,
|
||||||
ssr: true,
|
ssr: true,
|
||||||
});
|
});
|
||||||
|
|
6
packages/astro/test/fixtures/ssr-dynamic/src/pages/api/products/[id].js
vendored
Normal file
6
packages/astro/test/fixtures/ssr-dynamic/src/pages/api/products/[id].js
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
export function get(params) {
|
||||||
|
return {
|
||||||
|
body: JSON.stringify(params)
|
||||||
|
};
|
||||||
|
}
|
|
@ -27,6 +27,14 @@ describe('Dynamic pages in SSR', () => {
|
||||||
return html;
|
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 () => {
|
it('Do not have to implement getStaticPaths', async () => {
|
||||||
const html = await fetchHTML('/123');
|
const html = await fetchHTML('/123');
|
||||||
const $ = cheerioLoad(html);
|
const $ = cheerioLoad(html);
|
||||||
|
@ -38,4 +46,9 @@ describe('Dynamic pages in SSR', () => {
|
||||||
const $ = cheerioLoad(html);
|
const $ = cheerioLoad(html);
|
||||||
expect($('link').length).to.equal(1);
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue