diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 69cdc341a3d08..86577450b53d5 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -1,5 +1,5 @@ import type { APIContext, ComponentInstance, Params, Props, RouteData } from '../../@types/astro'; -import { renderPage as runtimeRenderPage } from '../../runtime/server/index.js'; +import { render, renderPage as runtimeRenderPage } from '../../runtime/server/index.js'; import { attachToResponse } from '../cookies/index.js'; import { AstroError, AstroErrorData } from '../errors/index.js'; import type { LogOptions } from '../logger/core.js'; diff --git a/packages/astro/test/fixtures/ssr-locals/src/pages/api.js b/packages/astro/test/fixtures/ssr-locals/src/pages/api.js index 8b209c5826c37..d4f7386fb85c4 100644 --- a/packages/astro/test/fixtures/ssr-locals/src/pages/api.js +++ b/packages/astro/test/fixtures/ssr-locals/src/pages/api.js @@ -1,5 +1,5 @@ -export async function post({ locals }) { +export async function get({ locals }) { let out = { ...locals }; return new Response(JSON.stringify(out), { diff --git a/packages/astro/test/fixtures/ssr-locals/src/pages/foo.astro b/packages/astro/test/fixtures/ssr-locals/src/pages/foo.astro index f864ac8f4690d..66b1f7a045b10 100644 --- a/packages/astro/test/fixtures/ssr-locals/src/pages/foo.astro +++ b/packages/astro/test/fixtures/ssr-locals/src/pages/foo.astro @@ -1,4 +1,4 @@ --- const { foo } = Astro.locals; --- -

{ foo }

+

{ foo }

diff --git a/packages/astro/test/ssr-locals.test.js b/packages/astro/test/ssr-locals.test.js index c0801cab70482..41e5710fbbb84 100644 --- a/packages/astro/test/ssr-locals.test.js +++ b/packages/astro/test/ssr-locals.test.js @@ -3,7 +3,7 @@ import * as cheerio from 'cheerio'; import { loadFixture } from './test-utils.js'; import testAdapter from './test-adapter.js'; -describe('SSR Environment Variables', () => { +describe('SSR Astro.locals from server', () => { /** @type {import('./test-utils').Fixture} */ let fixture; @@ -19,20 +19,20 @@ describe('SSR Environment Variables', () => { it('Can access Astro.locals in page', async () => { const app = await fixture.loadTestAdapterApp(); const request = new Request('http://example.com/foo'); - const route = match - const response = await app.render(request); + const locals = { foo: 'bar' }; + const response = await app.render(request, undefined, locals); const html = await response.text(); + const $ = cheerio.load(html); - expect($('#ssr').text()).to.equal('true'); + expect($('#foo').text()).to.equal('bar'); }); - it('Can access Astro.locals in API', async () => { + it('Can access Astro.locals in api context', async () => { const app = await fixture.loadTestAdapterApp(); const request = new Request('http://example.com/api'); - const response = await app.render(request); + const locals = { foo: 'bar' }; + const response = await app.render(request, undefined, locals); expect(response.status).to.equal(200); - expect(response.headers.get('Content-Type')).to.equal('application/json;charset=utf-8'); - expect(response.headers.get('Content-Length')).to.not.be.empty; const body = await response.json(); expect(body.foo).to.equal('bar'); diff --git a/packages/astro/test/test-adapter.js b/packages/astro/test/test-adapter.js index cc34e3c3373ce..d74cfaf81a71b 100644 --- a/packages/astro/test/test-adapter.js +++ b/packages/astro/test/test-adapter.js @@ -34,7 +34,7 @@ export default function ({ provideAddress = true, extendAdapter } = { provideAdd this.#manifest = manifest; } - async render(request, routeData) { + async render(request, routeData, locals) { const url = new URL(request.url); if(this.#manifest.assets.has(url.pathname)) { const filePath = new URL('../client/' + this.removeBase(url.pathname), import.meta.url); @@ -42,9 +42,8 @@ export default function ({ provideAddress = true, extendAdapter } = { provideAdd return new Response(data); } - Reflect.set(request, Symbol.for('astro.locals'), {}); ${provideAddress ? `request[Symbol.for('astro.clientAddress')] = '0.0.0.0';` : ''} - return super.render(request, routeData); + return super.render(request, routeData, locals); } }