From 1aeabe417077505bc0cdb8d2e47366ddbc616072 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 8 Dec 2022 08:08:27 -0500 Subject: [PATCH] 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 --- .changeset/fast-carpets-float.md | 5 ++++ packages/astro/src/core/app/index.ts | 7 +++-- .../test/fixtures/ssr-params/package.json | 8 +++++ .../ssr-params/src/pages/[category].astro | 12 ++++++++ packages/astro/test/ssr-params.test.js | 30 +++++++++++++++++++ pnpm-lock.yaml | 8 ++++- 6 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 .changeset/fast-carpets-float.md create mode 100644 packages/astro/test/fixtures/ssr-params/package.json create mode 100644 packages/astro/test/fixtures/ssr-params/src/pages/[category].astro create mode 100644 packages/astro/test/ssr-params.test.js diff --git a/.changeset/fast-carpets-float.md b/.changeset/fast-carpets-float.md new file mode 100644 index 000000000000..0d565296de40 --- /dev/null +++ b/.changeset/fast-carpets-float.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix Astro.params not having values when using base in SSR diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 188562e9d775..c7b8616ef05c 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -168,7 +168,7 @@ export class App { status = 200 ): Promise { 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 { 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, }); diff --git a/packages/astro/test/fixtures/ssr-params/package.json b/packages/astro/test/fixtures/ssr-params/package.json new file mode 100644 index 000000000000..4fbafaafa9fb --- /dev/null +++ b/packages/astro/test/fixtures/ssr-params/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-params", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro b/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro new file mode 100644 index 000000000000..bdaa1f965d22 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro @@ -0,0 +1,12 @@ +--- +const { category } = Astro.params +--- + + + Testing + + +

Testing

+

{ category }

+ + diff --git a/packages/astro/test/ssr-params.test.js b/packages/astro/test/ssr-params.test.js new file mode 100644 index 000000000000..93e5f956ba29 --- /dev/null +++ b/packages/astro/test/ssr-params.test.js @@ -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'); + }); + +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f41745f3f6dd..b3f2bf3448bb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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