Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed component rerendering if there's only one renderer (framework components) #12131

Open
wants to merge 12 commits into
base: next
Choose a base branch
from
33 changes: 20 additions & 13 deletions packages/astro/src/runtime/server/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,29 @@ async function renderFrameworkComponent(
}

if (!renderer) {
let error;
for (const r of renderers) {
try {
if (await r.ssr.check.call({ result }, Component, props, children)) {
renderer = r;
break;
// If there's only one renderer in the project
// we can skip the `check` calls and use that renderer
if (renderers.length === 1) {
renderer = renderers[0];
} else {
let error;

for (const r of renderers) {
try {
if (await r.ssr.check.call({ result }, Component, props, children)) {
renderer = r;
break;
}
} catch (e) {
error ??= e;
}
} catch (e) {
error ??= e;
}
}

// 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;
// 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,9 @@
let count = 1;

export default function RenderCount() {
return (
<>
<div id="render-count">{count++}</div>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Counter as DepCounter } from '@test/solid-jsx-component';
import Hello from '../components/Hello.jsx';
import ProxyComponent from '../components/ProxyComponent.jsx';
import WithNewlines from '../components/WithNewlines.jsx';
import RenderCount from '../components/RenderCount.jsx';
---
<html>
<head><title>Solid</title></head>
Expand All @@ -14,6 +15,7 @@ import WithNewlines from '../components/WithNewlines.jsx';
<Router />
<ProxyComponent client:load />
<DepCounter client:load />
<RenderCount client:load />
</div>
</body>
</html>
7 changes: 7 additions & 0 deletions packages/astro/test/solid-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ describe.skip('Solid component build', { todo: 'Check why an error is thrown.' }
assert.equal($('#proxy-component').text(), 'Hello world');
});

it('Renders the component only once on the server', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

assert.equal($('#render-count').text(), '1');
});

// ssr-client-none.astro
it('Supports server only components', async () => {
const html = await fixture.readFile('ssr-client-none/index.html');
Expand Down
Loading