Skip to content

Commit

Permalink
Fix Astro.params not having values when using base in SSR (#5553)
Browse files Browse the repository at this point in the history
* Fix Astro.params not having values when using base in SSR

* Adding a changeseet
  • Loading branch information
matthewp committed Dec 8, 2022
1 parent 4f7f206 commit 1aeabe4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-carpets-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Astro.params not having values when using base in SSR
7 changes: 4 additions & 3 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -190,7 +190,7 @@ export class App {
const ctx = createRenderContext({
request,
origin: url.origin,
pathname: url.pathname,
pathname,
scripts,
links,
route: routeData,
Expand All @@ -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,
});
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-params/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-params",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/ssr-params/src/pages/[category].astro
Original file line number Diff line number Diff line change
@@ -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>
30 changes: 30 additions & 0 deletions packages/astro/test/ssr-params.test.js
Original file line number Diff line number Diff line change
@@ -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');
});

});
8 changes: 7 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1aeabe4

Please sign in to comment.