Only rethrow renderer error when no renderer found (#4131)

* Only rethrow renderer error when no renderer found

* Adds a changeset
This commit is contained in:
Matthew Phillips 2022-08-03 14:33:31 -04:00 committed by GitHub
parent acd0b6da3c
commit 09eca9be5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 147 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes use of multiple renderers when one throws

View file

@ -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;
}
}

View file

@ -0,0 +1,6 @@
import one from '@astrojs/renderer-one';
import two from '@astrojs/renderer-two';
export default {
integrations: [one(), two()]
};

View file

@ -0,0 +1,8 @@
{
"name": "@test/multiple-renderers",
"dependencies": {
"astro": "workspace:*",
"@astrojs/renderer-one": "file:./renderers/one",
"@astrojs/renderer-two": "file:./renderers/two"
}
}

View file

@ -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',
});
}
}
};
}

View file

@ -0,0 +1,5 @@
{
"name": "@astrojs/renderer-one",
"version": "1.0.0",
"main": "index.mjs"
}

View file

@ -0,0 +1,11 @@
export default {
check() {
throw new Error(`Oops this did not work`);
},
renderToStaticMarkup(Component) {
return {
html: Component()
};
},
};

View file

@ -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',
});
}
}
};
}

View file

@ -0,0 +1,5 @@
{
"name": "@astrojs/renderer-two",
"version": "1.0.0",
"main": "index.mjs"
}

View file

@ -0,0 +1,11 @@
export default {
check() {
return true;
},
renderToStaticMarkup(Component) {
return {
html: Component()
};
},
};

View file

@ -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>

View file

@ -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);
});
});

View file

@ -1634,6 +1634,22 @@ importers:
'@astrojs/preact': link:../../../../integrations/preact
astro: link:../../..
packages/astro/test/fixtures/multiple-renderers:
specifiers:
'@astrojs/renderer-one': file:./renderers/one
'@astrojs/renderer-two': file:./renderers/two
astro: workspace:*
dependencies:
'@astrojs/renderer-one': file:packages/astro/test/fixtures/multiple-renderers/renderers/one
'@astrojs/renderer-two': file:packages/astro/test/fixtures/multiple-renderers/renderers/two
astro: link:../../..
packages/astro/test/fixtures/multiple-renderers/renderers/one:
specifiers: {}
packages/astro/test/fixtures/multiple-renderers/renderers/two:
specifiers: {}
packages/astro/test/fixtures/page-level-styles:
specifiers:
astro: workspace:*
@ -17079,3 +17095,15 @@ packages:
name: '@astrojs/test-font-awesome-package'
version: 0.0.1
dev: false
file:packages/astro/test/fixtures/multiple-renderers/renderers/one:
resolution: {directory: packages/astro/test/fixtures/multiple-renderers/renderers/one, type: directory}
name: '@astrojs/renderer-one'
version: 1.0.0
dev: false
file:packages/astro/test/fixtures/multiple-renderers/renderers/two:
resolution: {directory: packages/astro/test/fixtures/multiple-renderers/renderers/two, type: directory}
name: '@astrojs/renderer-two'
version: 1.0.0
dev: false