From 6fd00c4941441663247fedbf322ef9bd75108045 Mon Sep 17 00:00:00 2001 From: Steven Yung Date: Mon, 26 Sep 2022 20:13:25 +0200 Subject: [PATCH] add double check on astro file return type to display more human readable error (#4857) --- examples/ssr/src/pages/index.astro | 2 ++ .../astro/src/runtime/server/render/page.ts | 9 +++-- .../astro/test/astro-not-response.test.js | 35 +++++++++++++++++++ .../fixtures/astro-not-response/package.json | 8 +++++ .../src/pages/not-response.astro | 3 ++ pnpm-lock.yaml | 6 ++++ 6 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 packages/astro/test/astro-not-response.test.js create mode 100644 packages/astro/test/fixtures/astro-not-response/package.json create mode 100644 packages/astro/test/fixtures/astro-not-response/src/pages/not-response.astro diff --git a/examples/ssr/src/pages/index.astro b/examples/ssr/src/pages/index.astro index 23a4c2721..a8f95588b 100644 --- a/examples/ssr/src/pages/index.astro +++ b/examples/ssr/src/pages/index.astro @@ -6,6 +6,8 @@ import { getProducts } from '../api'; import '../styles/common.css'; const products = await getProducts(Astro.request); + +return; --- diff --git a/packages/astro/src/runtime/server/render/page.ts b/packages/astro/src/runtime/server/render/page.ts index 9648a94bd..f8ab70f4c 100644 --- a/packages/astro/src/runtime/server/render/page.ts +++ b/packages/astro/src/runtime/server/render/page.ts @@ -112,7 +112,12 @@ export async function renderPage( let response = createResponse(body, { ...init, headers }); return response; - } else { - return factoryReturnValue; } + + // We double check if the file return a Response + if (!(factoryReturnValue instanceof Response)) { + throw new Error('Only instance of Response can be returned from an Astro file'); + } + + return factoryReturnValue; } diff --git a/packages/astro/test/astro-not-response.test.js b/packages/astro/test/astro-not-response.test.js new file mode 100644 index 000000000..76c7e5c35 --- /dev/null +++ b/packages/astro/test/astro-not-response.test.js @@ -0,0 +1,35 @@ +import { expect, assert } from 'chai'; +import { loadFixture } from './test-utils.js'; + +// Asset bundling +describe('Not returning responses', () => { + let fixture; + /** @type {import('./test-utils').DevServer} */ + let devServer; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/astro-not-response/', + }); + + devServer = await fixture.startDevServer(); + }); + + after(async () => { + await devServer.stop(); + }); + + it('Does not work from a page', async () => { + try { + await fixture.build(); + } catch (e) { + expect(e).to.be.instanceOf( + Error, + 'Only instance of Response can be returned from an Astro file' + ); + return null; + } + + assert.fail('Should have thrown an error'); + }); +}); diff --git a/packages/astro/test/fixtures/astro-not-response/package.json b/packages/astro/test/fixtures/astro-not-response/package.json new file mode 100644 index 000000000..31eeb4e68 --- /dev/null +++ b/packages/astro/test/fixtures/astro-not-response/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/astro-not-response", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/astro-not-response/src/pages/not-response.astro b/packages/astro/test/fixtures/astro-not-response/src/pages/not-response.astro new file mode 100644 index 000000000..cd7aef969 --- /dev/null +++ b/packages/astro/test/fixtures/astro-not-response/src/pages/not-response.astro @@ -0,0 +1,3 @@ +--- +return null; +--- diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e4d170183..3a3859003 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1335,6 +1335,12 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/astro-not-response: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/astro-page-directory-url: specifiers: astro: workspace:*