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

@asphalt-react/ssr

v2.0.0-rc.11

Published

server side rendering for asphalt-react components

Downloads

105

Readme

⚠️ Styles for Asphalt React components are absent on the client on initial request and are loaded when script executes which may cause FOUC or Flash Of Unstyled Content for some users. This package is designed to add styles for components and solve FOUC.

Asphalt React SSR

npm

Provides a wrapper React component called SSR and a function called extractCSS to add styles when server rendering asphalt-react components.

  • <SSR> (default): Wrap your App with SSR component for server side rendering to work.

Only wrapped subtree is considered for styling

Following is a pseudocode of a hypothetical client implementation

app.js

import { Button } from "@asphalt-react/button"
import { SSR } from "@asphalt-react/ssr"

const App = () => (
  <SSR>
    <Button>Click Me</Button>
  </SSR>
)

export default App
  • extractCSS(): Wrap SSR with extractCSS function on server side implementation. Function extractCSS returns the following:
const { html,css,id } = extractCSS(...)
  • html: html of all wrapped components
  • css: combined css as string of asphalt-react components
  • id: string to uniquely identify style node.

Inject the css to the html on the server and apply the id to the style node.

When the script runs on the browser, <style> tag with above id is removed from the document.

Following is a pseudocode of a hypothetical server implementation

server.js

const ReactDOMServer = require("react-dom/server")
const React = require("react")

const App = require("./app.js").default
const { extractCSS } = require("@asphalt-react/asphalt-ssr")

const { html,css,id } = extractCSS(ReactDOMServer.renderToString(React.createElement(App)))

const serverHtml = `<!DOCTYPE html>
  <html lang="en">
    <head>
      <title>My page</title>
      <style id=`${id}`>${css}</style>
      <meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
    </head>
    <body>
      <div id="root">${html}</div>
    </body>
  </html>
`

Certain changes may be required to the above code according to implementation.

Installation

# yarn
$ yarn add @asphalt-react/ssr

# npm
$ npm install @asphalt-react/ssr

Local Development

How it works ?

Uses StyleContext from @asphalt-react/context for communication between server side and client side. Components receive a function as context's value and pass the respective stylesheet to the function in server environment. A dock then collects, de-duplicates and ships the styles. extractCSS returns final styles which are injected to server html.

How to build?

This package is built using @asphalt-react/build using a custom rollup.config.js.

# yarn
$ yarn rollup -c

# npx
$ npx rollup -c