How to use preact-render-to-string - 10 common examples

To help you get started, we’ve selected a few preact-render-to-string examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Download / preact-layout / test / Layout.spec.jsx View on Github external
<div>
					<header>
						<h1>my page</h1>
					</header>
					<div>
						<article>main article</article>
					</div>
					<footer>
						goodbye
					</footer>
				</div>
			`.replace(/\s+\s+/g, '&gt;')

			expect(result).toBe(expectedResult)

			result = render(
				
					
				
			)

			// work around preact-render-to-string generating comment
			// nodes for empty (null/undefined) vdom elements. It's
			// a bug according to Jason
			result = result.replace(//g, '')

			const reversedExpected = `
				<div>
					<footer>
						goodbye
					</footer>
					<div></div></div>
github Download / preact-layout / test / Layout.spec.jsx View on Github external
}

			// 2 levels
			// nested children above tested 1 level deep, this tests 2 levels deep
			let result = render(
				
					
						
					
				
			)

			expect(result.indexOf('my page')).not.toBe(-1)

			// 3 levels
			result = render(
				
					
						
							
						
					
				
			)

			expect(result.indexOf('my page')).not.toBe(-1)

			// 4 levels
			result = render(
github reshape / preact-components / test / index.js View on Github external
path.join(__dirname, '../node_modules/preact/dist/preact.js')
          )}
          // require reshapePreact
          ${fs.readFileSync(path.join(__dirname, '../lib/browser.js'))}
          // our custom component
          ${component}
          // implementation
          var el = document.querySelector('p')
          window.out = reshapePreact.hydrateInitialState(el.getAttribute('data-state'), {
            'my-component': MyComponent
          })
        
      `,
        { runScripts: 'dangerously' }
      )
      t.is(render(dom.window.out), '<p>the value of foo is "bar"</p>')
    })
})
github johnhaitas / preact-cli-express-ssr / src / server / render.js View on Github external
import { h } from 'preact';
import render from 'preact-render-to-string';

export const AppPlaceholder = () =&gt; (<div id="app">);

const appPlaceholder = render(h(AppPlaceholder)),
	appRegex = /<div id="app">]*&gt;.*?(?= {
	const headTag = '',
		templateApp = template.includes(appPlaceholder) ? appPlaceholder : appRegex;
	return props =&gt; {
		const app = render(h(rootComponentClass, props)),
			head = '', // you could use something like Helmet here
			renderedTemplate = template
				.replace(headTag, headTag + head)
				.replace(templateApp, app);

		return renderedTemplate;
	};
};
</div></div>
github Ariel-Rodriguez / react-amp-template / src / StyleManager / StyledComponents.jsx View on Github external
static render(element, opts) {
    // eslint-disable-next-line global-require
    const { ServerStyleSheet, StyleSheetManager } = require('styled-components')

    const sheet = new ServerStyleSheet()
    const html = preactToString(
      
        {element}
      , null, opts,
    )
    const styles = sheet.getStyleElement()[0]
    const css = (styles)
      // eslint-disable-next-line no-underscore-dangle
      ? styles.attributes.dangerouslySetInnerHTML.__html : ''

    return { html, css }
  }
}
github kristoferbaxter / preact-hn / src / server / routes / default.tsx View on Github external
<title>Preact Hacker News</title>
      
      ${supportsManifest ? '' : ''}
      <style>${resources.inline}</style>
      ${resources.inline === null &amp;&amp; resources.css !== null
        ? ''
        : ''}
      ${supportsManifest ? '' : ''}
      
      
      
    
    
      <div id="mount">`);

  const RoutedViewComponent: string = render();

  res.write(`
        ${RoutedViewComponent}
        </div>
      
    `);

  // TODO: More research on why these scripts cannot be loaded async.

  res.end();

  next();
}
github reshape / preact-components / lib / index.js View on Github external
node => {
        // encode/compress the original html structure
        // this can be rehydrated later to reduce client/server duplication
        const originalHtml = encode(node)
        return parse(render(toVdom(components, node, originalHtml)))
      }
    )
github preactjs / enzyme-adapter-preact-pure / src / StringRenderer.ts View on Github external
render(el: ReactElement, context?: any) {
    // FIXME - The behavior here is different across different Preact versions.
    // Historically this was because preact-render-to-string v4.x did not support
    // Preact 10. In future we should unify them. This will be a breaking change
    // as it will affect the output for either Preact v8 or Preact v10.
    if (isPreact10()) {
      const tempContainer = document.createElement('div');
      render(el as any, tempContainer);
      const html = tempContainer.innerHTML;
      render(h('unmount-me', {}), tempContainer);
      return html;
    } else {
      return renderToString(el as any, context);
    }
  }
github SaraVieira / awesome-talks / src / index.js View on Github external
import App from './App'
import { render } from 'preact'
import { default as renderToString } from 'preact-render-to-string'
import { ServerStyleSheet } from 'styled-components'
import * as OfflinePluginRuntime from 'offline-plugin/runtime'

if (typeof window === 'undefined') {
  /* eslint-disable */
  const sheet = new ServerStyleSheet()
  const html = renderToString(sheet.collectStyles())
  const styleTags = sheet.getStyleTags()
  /* eslint-enable */
}
if (typeof window !== 'undefined') {
  render(, document.getElementById('root'))
  OfflinePluginRuntime.install()
}
github developit / preact-photon / test / index.js View on Github external
it('should render icon &amp; text', () =&gt; {
			expect(render(<button>bar</button>)).to.equal('<button class="btn btn-default"><span class="icon icon-home icon-text"></span>bar</button>');
		});
	});

preact-render-to-string

Render JSX to an HTML string, with support for Preact components.

MIT
Latest version published 10 days ago

Package Health Score

89 / 100
Full package analysis