Skip to content

Commit e1cd1ae

Browse files
fix: renderers types (#13720)
1 parent 2103991 commit e1cd1ae

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

.changeset/tangy-eagles-train.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astrojs/svelte': patch
3+
'@astrojs/react': patch
4+
'@astrojs/vue': patch
5+
---
6+
7+
Fixes SSR renderer type

packages/integrations/react/src/server-v17.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AstroComponentMetadata } from 'astro';
1+
import type { AstroComponentMetadata, NamedSSRLoadedRendererValue } from 'astro';
22
import React from 'react';
33
import ReactDOM from 'react-dom/server';
44
import StaticHtml from './static-html.js';
@@ -36,11 +36,11 @@ function check(Component: any, props: Record<string, any>, children: any) {
3636
return isReactComponent;
3737
}
3838

39-
function renderToStaticMarkup(
39+
async function renderToStaticMarkup(
4040
Component: any,
4141
props: Record<string, any>,
4242
{ default: children, ...slotted }: Record<string, any>,
43-
metadata: AstroComponentMetadata,
43+
metadata?: AstroComponentMetadata,
4444
) {
4545
delete props['class'];
4646
const slots: Record<string, any> = {};
@@ -57,12 +57,12 @@ function renderToStaticMarkup(
5757
if (newChildren != null) {
5858
newProps.children = React.createElement(StaticHtml, {
5959
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
60-
hydrate: metadata.astroStaticSlot ? !!metadata.hydrate : true,
60+
hydrate: metadata?.astroStaticSlot ? !!metadata.hydrate : true,
6161
value: newChildren,
6262
});
6363
}
6464
const vnode = React.createElement(Component, newProps);
65-
let html;
65+
let html: string;
6666
if (metadata?.hydrate) {
6767
html = ReactDOM.renderToString(vnode);
6868
} else {
@@ -71,9 +71,11 @@ function renderToStaticMarkup(
7171
return { html };
7272
}
7373

74-
export default {
74+
const renderer: NamedSSRLoadedRendererValue = {
7575
name: '@astrojs/react',
7676
check,
7777
renderToStaticMarkup,
7878
supportsAstroStaticSlot: true,
7979
};
80+
81+
export default renderer;

packages/integrations/react/src/server.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import opts from 'astro:react:opts';
2-
import type { AstroComponentMetadata } from 'astro';
2+
import type { AstroComponentMetadata, NamedSSRLoadedRendererValue } from 'astro';
33
import React from 'react';
44
import ReactDOM from 'react-dom/server';
55
import { incrementId } from './context.js';
@@ -47,7 +47,7 @@ async function check(
4747
return React.createElement('div');
4848
}
4949

50-
await renderToStaticMarkup.call(this, Tester, props, children, {} as any);
50+
await renderToStaticMarkup.call(this, Tester, props, children);
5151

5252
return isReactComponent;
5353
}
@@ -58,17 +58,17 @@ async function getNodeWritable(): Promise<typeof import('node:stream').Writable>
5858
return Writable;
5959
}
6060

61-
function needsHydration(metadata: AstroComponentMetadata) {
61+
function needsHydration(metadata?: AstroComponentMetadata) {
6262
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
63-
return metadata.astroStaticSlot ? !!metadata.hydrate : true;
63+
return metadata?.astroStaticSlot ? !!metadata.hydrate : true;
6464
}
6565

6666
async function renderToStaticMarkup(
6767
this: RendererContext,
6868
Component: any,
6969
props: Record<string, any>,
7070
{ default: children, ...slotted }: Record<string, any>,
71-
metadata: AstroComponentMetadata,
71+
metadata?: AstroComponentMetadata,
7272
) {
7373
let prefix;
7474
if (this && this.result) {
@@ -113,7 +113,7 @@ async function renderToStaticMarkup(
113113
identifierPrefix: prefix,
114114
formState,
115115
};
116-
let html;
116+
let html: string;
117117
if (opts.experimentalDisableStreaming) {
118118
html = ReactDOM.renderToString(vnode);
119119
} else if ('renderToReadableStream' in ReactDOM) {
@@ -156,7 +156,7 @@ async function getFormState({
156156
async function renderToPipeableStreamAsync(vnode: any, options: Record<string, any>) {
157157
const Writable = await getNodeWritable();
158158
let html = '';
159-
return new Promise((resolve, reject) => {
159+
return new Promise<string>((resolve, reject) => {
160160
let error = undefined;
161161
let stream = ReactDOM.renderToPipeableStream(vnode, {
162162
...options,
@@ -219,9 +219,11 @@ function isFormRequest(contentType: string | null) {
219219
return formContentTypes.some((t) => type === t);
220220
}
221221

222-
export default {
222+
const renderer: NamedSSRLoadedRendererValue = {
223223
name: '@astrojs/react',
224224
check,
225225
renderToStaticMarkup,
226226
supportsAstroStaticSlot: true,
227227
};
228+
229+
export default renderer;

packages/integrations/svelte/src/server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AstroComponentMetadata } from 'astro';
1+
import type { AstroComponentMetadata, NamedSSRLoadedRendererValue } from 'astro';
22
import { createRawSnippet } from 'svelte';
33
import { render } from 'svelte/server';
44
import { incrementId } from './context.js';
@@ -13,17 +13,17 @@ function check(Component: any) {
1313
return Component.toString().includes('$$payload');
1414
}
1515

16-
function needsHydration(metadata: AstroComponentMetadata) {
16+
function needsHydration(metadata?: AstroComponentMetadata) {
1717
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
18-
return metadata.astroStaticSlot ? !!metadata.hydrate : true;
18+
return metadata?.astroStaticSlot ? !!metadata.hydrate : true;
1919
}
2020

2121
async function renderToStaticMarkup(
2222
this: RendererContext,
2323
Component: any,
2424
props: Record<string, any>,
2525
slotted: Record<string, any>,
26-
metadata: AstroComponentMetadata,
26+
metadata?: AstroComponentMetadata,
2727
) {
2828
const tagName = needsHydration(metadata) ? 'astro-slot' : 'astro-static-slot';
2929

@@ -66,9 +66,11 @@ async function renderToStaticMarkup(
6666
return { html: result.body };
6767
}
6868

69-
export default {
69+
const renderer: NamedSSRLoadedRendererValue = {
7070
name: '@astrojs/svelte',
7171
check,
7272
renderToStaticMarkup,
7373
supportsAstroStaticSlot: true,
7474
};
75+
76+
export default renderer;
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { setup } from 'virtual:@astrojs/vue/app';
2-
import type { AstroComponentMetadata } from 'astro';
2+
import type { AstroComponentMetadata, NamedSSRLoadedRendererValue } from 'astro';
33
import { createSSRApp, h } from 'vue';
44
import { renderToString } from 'vue/server-renderer';
55
import { incrementId } from './context.js';
66
import StaticHtml from './static-html.js';
77
import type { RendererContext } from './types.js';
88

9-
function check(Component: any) {
9+
async function check(Component: any) {
1010
return !!Component['ssrRender'] || !!Component['__ssrInlineRender'];
1111
}
1212

@@ -15,13 +15,13 @@ async function renderToStaticMarkup(
1515
Component: any,
1616
inputProps: Record<string, any>,
1717
slotted: Record<string, any>,
18-
metadata: AstroComponentMetadata,
18+
metadata?: AstroComponentMetadata,
1919
) {
2020
let prefix;
2121
if (this && this.result) {
2222
prefix = incrementId(this.result);
2323
}
24-
const attrs = { prefix };
24+
const attrs: Record<string, any> = { prefix };
2525

2626
const slots: Record<string, any> = {};
2727
const props = { ...inputProps };
@@ -32,7 +32,7 @@ async function renderToStaticMarkup(
3232
value,
3333
name: key === 'default' ? undefined : key,
3434
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
35-
hydrate: metadata.astroStaticSlot ? !!metadata.hydrate : true,
35+
hydrate: metadata?.astroStaticSlot ? !!metadata.hydrate : true,
3636
});
3737
}
3838
const app = createSSRApp({ render: () => h(Component, props, slots) });
@@ -42,9 +42,11 @@ async function renderToStaticMarkup(
4242
return { html, attrs };
4343
}
4444

45-
export default {
45+
const renderer: NamedSSRLoadedRendererValue = {
4646
name: '@astrojs/vue',
4747
check,
4848
renderToStaticMarkup,
4949
supportsAstroStaticSlot: true,
5050
};
51+
52+
export default renderer;

0 commit comments

Comments
 (0)