Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

html-webpack-jsdom-prerender-plugin

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-webpack-jsdom-prerender-plugin

Webpack plugin to prerender JS apps in a JSDOM context

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
2
Weekly downloads
 
Created
Source

html-webpack-jsdom-prerender-plugin

A plugin to prerender and inject JavaScript apps into the static markup generated by html-webpack-plugin at build time. This was heavily inspired by html-webpack-prerender-plugin by Reuters Graphics.

Getting Started

  1. Install this package and html-webpack-plugin as dev dependencies

    yarn add --dev html-webpack-plugin html-webpack-jsdom-prerender-plugin
    
  2. Add the plugins to your webpack configuration

    // webpack.config.js
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    const HtmlWebpackJsdomPrerenderPlugin = require('html-webpack-jsdom-prerender-plugin')
    
    module.exports = {
      entry: './src/js/index.js',
      output: {
        path: './dist',
        filename: '[name].js',
      },
      // ...
      plugins: [
        new HtmlWebpackPlugin({
          filename: 'index.html',
          template: './src/templates/index.html',
        }),
        new HtmlWebpackJsdomPrerenderPlugin({
          'index.html': {
            chunks: ['main'],
          },
        }),
      ],
    }
    
  3. Create an HTML template for your app

    <!-- src/templates/index.html -->
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head></head>
      <body>
        <div id='root'></div>
      </body>
    </html>
    
  4. Make sure your app will change the DOM when it runs. If you're using a framework like React you can use the flag window.__HTML_WEBPACK_JSDOM_PRERENDER_PLUGIN__ to see if your app is running in a prerender environment rather than on the client.

    // src/js/index.js
    import React from 'react'
    import { render } from 'react-dom'
    import { renderToString } from 'react-dom/server'
    import Component from './my-component'
    
    const node = document.getElementById('root')
    if (window.__HTML_WEBPACK_JSDOM_PRERENDER_PLUGIN__) {
      node.innerHTML = renderToString(<Component />)
    } else {
      render(<Component />, node)
    }
    

    Note The plugin only currently supports two types of publicPath: "auto" (the same behavior as if the publicPath is excluded) and a path relative to the root, for example "/assets".

FAQs

Package last updated on 24 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc