Skip to content

Commit

Permalink
Only rethrow renderer error when no renderer found (#4131)
Browse files Browse the repository at this point in the history
* Only rethrow renderer error when no renderer found

* Adds a changeset
  • Loading branch information
matthewp committed Aug 3, 2022
1 parent acd0b6d commit 09eca9b
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lazy-cameras-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes use of multiple renderers when one throws
4 changes: 3 additions & 1 deletion packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ Did you mean to add ${formatList(probableRendererNames.map((r) => '`' + r + '`')
}
}

if (error) {
// If no renderer is found and there is an error, throw that error because
// it is likely a problem with the component code.
if (!renderer && error) {
throw error;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import one from '@astrojs/renderer-one';
import two from '@astrojs/renderer-two';

export default {
integrations: [one(), two()]
};
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/multiple-renderers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/multiple-renderers",
"dependencies": {
"astro": "workspace:*",
"@astrojs/renderer-one": "file:./renderers/one",
"@astrojs/renderer-two": "file:./renderers/two"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

export default function() {
return {
name: 'renderer-one',
hooks: {
'astro:config:setup': ({ addRenderer }) => {
addRenderer({
name: 'renderer-one',
clientEntrypoint: null,
serverEntrypoint: '@astrojs/renderer-one/server.mjs',
});
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@astrojs/renderer-one",
"version": "1.0.0",
"main": "index.mjs"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export default {
check() {
throw new Error(`Oops this did not work`);
},
renderToStaticMarkup(Component) {
return {
html: Component()
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

export default function() {
return {
name: 'renderer-two',
hooks: {
'astro:config:setup': ({ addRenderer }) => {
addRenderer({
name: 'renderer-two',
clientEntrypoint: null,
serverEntrypoint: '@astrojs/renderer-two/server.mjs',
});
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@astrojs/renderer-two",
"version": "1.0.0",
"main": "index.mjs"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export default {
check() {
return true;
},
renderToStaticMarkup(Component) {
return {
html: Component()
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
function Component() {
return `<div id="component">works</div>`;
}
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<Component />
</body>
</html>
21 changes: 21 additions & 0 deletions packages/astro/test/multiple-renderers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Multiple renderers', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/multiple-renderers/',
});
await fixture.build();
});

it('when the first throws but the second does not, use the second renderer', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#component')).to.have.a.lengthOf(1);
});
});
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 09eca9b

Please sign in to comment.