npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mo36924/preact-ssr-prepass

v1.2.2

Published

react-ssr-prepass drop-in replacement for preact-x

Downloads

3

Readme

preact-ssr-prepass

npm Coverage Status OpenCollective Backers OpenCollective Sponsors travis

Drop-in replacement for react-ssr-prepass.

Neither Preact nor React support Suspense on the server as of now. Heavily inspired by react-ssr-prepass, preact-ssr-prepass provides a two-pass approach with which Suspense can be used on the server. In the first pass, preact-ssr-prepass will create a VNode tree and await all suspensions, in the second pass preact-render-to-string can be used to render a vnode to a string.

Even if preact-ssr-prepass is designed to do as little as possible, it still adds a slight overhead since the VNode tree is created twice.

⚠️ Note that this is neither an official Preact nor React API and that the way Suspense is handled on the server might/will change in the future!

Usage / API

Awaiting suspensions

preact-ssr-prepass needs to be called just before rendering a vnode to a string. See the following example:

lazy.js:

export default function LazyLoaded() {
    return <div>I shall be loaded and rendered on the server</div>
}

index.js:

import { createElement as h } from 'preact';
import { Suspense, lazy } from 'preact/compat';
import renderToString from 'preact-render-to-string';
import prepass from 'preact-ssr-prepass';

const LazyComponent = lazy(() => import('./lazy'));

const vnode = (
    <Suspense fallback={<div>I shall not be rendered on the server</div>}>
        <LazyComponent />
    </Suspense>
);

prepass(vnode)
    .then(() => {
        // <div>I shall be loaded and rendered on the server</div>
        console.log(renderToString(vnode));
    });

Custom suspensions/data fetching using the visitor

preact-ssr-prepass accepts a second argument that allows you to suspend on arbitrary elements:

ssrPrepass(<App />, (element, instance) => {
  if (instance !== undefined && typeof instance.fetchData === 'function') {
    return instance.fetchData()
  }
});

API

/**
 * Visitor function to suspend on certain elements.
 * 
 * When this function returns a Promise it is awaited before the vnode will be rendered.
 */
type Visitor = (element: preact.VNode, instance: ?preact.Component) => ?Promise<any>;

/**
 * The default export of preact-ssr-prepass
 *
 * @param{vnode} preact.VNode The vnode to traverse
 * @param{visitor} ?Visitor A function that is called for each vnode and might return a Promise to suspend.
 * @param{context} ?Object Initial context to be used when traversing the vnode tree
 * @return Promise<any> Promise that will complete once the complete vnode tree is traversed. Note that even if
 *         a Suspension throws the returned promise will resolve.
 */
export default function prepass(vnode: preact.VNode, visitor?: Visitor, context:? Object): Promise<any>;

Replace react-ssr-prepass (e.g. next.js)

react-ssr-prepass is usually used on the server only and not bundled into your bundles but rather required through Node.js. To alias react-ssr-prepass to preact-ssr-prepass we recommend to use module-alias:

Create a file named alias.js:

const moduleAlias = require('module-alias')

module.exports = () => {
  moduleAlias.addAlias('react-ssr-prepass', 'preact-ssr-prepass')
}

Require and execute the exported function in your applications entrypoint (before require'ing react-ssr-prepass):

require('./alias')();

Differences to react-ssr-prepass

The visitor passed to preact-ssr-prepass gets a Preact element instead of a React one. When you use preact/compat's createElement it will make the element/vnode look as similar to a React element as possible.