Fix Astro.params not having values when using base in SSR (#5553)

* Fix Astro.params not having values when using base in SSR

* Adding a changeseet
This commit is contained in:
Matthew Phillips 2022-12-08 08:08:27 -05:00 committed by GitHub
parent 4f7f20616e
commit 1aeabe4170
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix Astro.params not having values when using base in SSR

View file

@ -168,7 +168,7 @@ export class App {
status = 200
): Promise<Response> {
const url = new URL(request.url);
const manifest = this.#manifest;
const pathname = '/' + this.removeBase(url.pathname);
const info = this.#routeDataToRouteInfo.get(routeData!)!;
const links = createLinkStylesheetElementSet(info.links);
@ -190,7 +190,7 @@ export class App {
const ctx = createRenderContext({
request,
origin: url.origin,
pathname: url.pathname,
pathname,
scripts,
links,
route: routeData,
@ -215,12 +215,13 @@ export class App {
status = 200
): Promise<Response> {
const url = new URL(request.url);
const pathname = '/' + this.removeBase(url.pathname);
const handler = mod as unknown as EndpointHandler;
const ctx = createRenderContext({
request,
origin: url.origin,
pathname: url.pathname,
pathname,
route: routeData,
status,
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/ssr-params",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,12 @@
---
const { category } = Astro.params
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<h2 class="category">{ category }</h2>
</body>
</html>

View file

@ -0,0 +1,30 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';
describe('Astro.params in SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-params/',
adapter: testAdapter(),
output: 'server',
base: '/users/houston/',
});
await fixture.build();
});
it('Params are passed to component', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/users/houston/food');
const response = await app.render(request);
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
expect($('.category').text()).to.equal('food');
});
});

View file

@ -2280,6 +2280,12 @@ importers:
dependencies:
astro: link:../../..
packages/astro/test/fixtures/ssr-params:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/ssr-partytown:
specifiers:
'@astrojs/partytown': workspace:*
@ -18434,7 +18440,7 @@ packages:
optional: true
dependencies:
'@types/node': 18.11.9
esbuild: 0.15.18
esbuild: 0.15.14
postcss: 8.4.19
resolve: 1.22.1
rollup: 2.79.1