summaryrefslogtreecommitdiff
path: root/packages/renderers/renderer-react/server.js
blob: d4d9f0cf8c01d9824653238dfe29d6b4acdcb8f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Component as BaseComponent, createElement as h } from 'react';
import { renderToStaticMarkup as renderToString } from 'react-dom/server.js';
import StaticHtml from './static-html.js';

const reactTypeof = Symbol.for('react.element');

function check(Component, props, children) {
  if (typeof Component !== 'function') return false;

  if (Component.prototype != null && typeof Component.prototype.render === 'function') {
    return BaseComponent.isPrototypeOf(Component);
  }

  let error = null;
  let isReactComponent = false;
  function Tester(...args) {
    try {
      const vnode = Component(...args);
      if (vnode && vnode['$$typeof'] === reactTypeof) {
        isReactComponent = true;
      }
    } catch (err) {
      error = err;
    }

    return h('div');
  }

  renderToStaticMarkup(Tester, props, children);

  if (error) {
    throw error;
  }
  return isReactComponent;
}

function renderToStaticMarkup(Component, props, children) {
  const html = renderToString(h(Component, { ...props, children: h(StaticHtml, { value: children }), innerHTML: children }));
  return { html };
}

export default {
  check,
  renderToStaticMarkup,
};
Gravatar wulinsheng123 2-1/+6 2022-12-08astro-rss: Generate feed with proper XML escaping (#5550)Gravatar Anders Kaseorg 5-36/+57 2022-12-08Fix Astro.params not having values when using base in SSR (#5553)Gravatar Matthew Phillips 6-4/+66 2022-12-07Allow all redirection status codes in redirect() (#5446)Gravatar Jeffrey Yasskin 2-1/+6 2022-12-06feat(astro-rss): generate post content in feed (#5366)Gravatar Ben Smith 4-1/+46 2022-12-06[ci] formatGravatar matthewp 2-7/+6 2022-12-06Use accumulated sort order when order production CSS (#5549)Gravatar Matthew Phillips 15-2/+180 2022-12-06[ci] formatGravatar matthewp 22-158/+171 2022-12-06Head propagation (#5511)Gravatar Matthew Phillips 36-279/+804 2022-12-06[ci] formatGravatar matthewp 1-7/+7 2022-12-06Removed premature optimization (#5548)Gravatar Ido S 2-37/+35 2022-12-06Exclude astro from Vite optimization (#5545)Gravatar Bjorn Lu 2-1/+6 2022-12-06bench workflow - use bash as shell (#5546)Gravatar Matthew Phillips 1-2/+4 2022-12-06bench workflow - pipe stderr (#5543)Gravatar Matthew Phillips 1-2/+4 2022-12-06Update esbuild dependency (#5534)Gravatar Bjorn Lu 7-18/+275 2022-12-06Fix tests with node 18 (#5528)Gravatar Bjorn Lu 1-9/+14