{"id":22336520,"url":"https://github.com/googlechromelabs/prerender-loader","last_synced_at":"2025-05-15T00:09:29.424Z","repository":{"id":38848396,"uuid":"135479869","full_name":"GoogleChromeLabs/prerender-loader","owner":"GoogleChromeLabs","description":"📰 Painless universal pre-rendering for Webpack.","archived":false,"fork":false,"pushed_at":"2022-04-15T01:11:53.000Z","size":64,"stargazers_count":1903,"open_issues_count":29,"forks_count":50,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-03T03:29:07.780Z","etag":null,"topics":["html-webpack-plugin","pre-rendering","prerender","webpack"],"latest_commit_sha":null,"homepage":"https://npm.im/prerender-loader","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GoogleChromeLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-30T18:04:38.000Z","updated_at":"2025-03-28T00:25:05.000Z","dependencies_parsed_at":"2022-09-18T13:02:05.639Z","dependency_job_id":null,"html_url":"https://github.com/GoogleChromeLabs/prerender-loader","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fprerender-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fprerender-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fprerender-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fprerender-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoogleChromeLabs","download_url":"https://codeload.github.com/GoogleChromeLabs/prerender-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166857,"owners_count":21058481,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html-webpack-plugin","pre-rendering","prerender","webpack"],"created_at":"2024-12-04T06:07:00.639Z","updated_at":"2025-04-10T06:15:03.099Z","avatar_url":"https://github.com/GoogleChromeLabs.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://i.imgur.com/BL0lf5F.png\" width=\"240\" height=\"240\" alt=\"prerender-loader\"\u003e\n  \u003ch1 align=\"center\"\u003e\n    prerender-loader\n    \u003ca href=\"https://www.npmjs.org/package/prerender-loader\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/prerender-loader.svg?style=flat\" alt=\"npm\"\u003e\u003c/a\u003e\n  \u003c/h1\u003e\n\u003c/p\u003e\n\nPainless universal prerendering for Webpack. Works great with\n[html-webpack-plugin].\n\n\u003e 🧐 **What is Prerendering?**\n\u003e\n\u003ePre-rendering describes the process of rendering a client-side application at\n\u003ebuild time, producing useful static HTML that can be sent to the browser\n\u003einstead of an empty bootstrapping page.\n\u003e\n\u003ePre-rendering is like Server-Side Rendering, just done at build time to produce\n\u003estatic files. Both techniques help get meaningful content onto the user's\n\u003escreen faster.\n\n## Features\n\n-   Works entirely within Webpack\n-   Integrates with [html-webpack-plugin]\n-   Works with `webpack-dev-server` / `webpack serve`\n-   Supports both DOM and String prerendering\n-   Asynchronous rendering via async/await or Promises\n\n---\n\n\u003c!-- TOC depthFrom:2 --\u003e\n\n- [Features](#features)\n- [How does it work?](#how-does-it-work)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [DOM Prerendering](#dom-prerendering)\n  - [String Prerendering](#string-prerendering)\n  - [Injecting content into the HTML](#injecting-content-into-the-html)\n  - [Prerendering JavaScript Files](#prerendering-javascript-files)\n- [Options](#options)\n- [License](#license)\n\n\u003c!-- /TOC --\u003e\n\n## How does it work?\n\n`prerender-loader` renders your web application within Webpack during builds,\nproducing static HTML. When the loader is applied to an HTML file, it creates a\nDOM structure from that HTML, compiles the application, runs it within the DOM\nand serializes the result back to HTML.\n\n---\n\n## Installation\n\nFirst, install `prerender-loader` as a development dependency:\n\n```sh\nnpm i -D prerender-loader\n```\n\n---\n\n## Usage\n\nIn most cases, you'll want to apply the loader to your `html-webpack-plugin`\ntemplate option:\n\n```diff\n// webpack.config.js\nmodule.exports = {\n  plugins: [\n    new HtmlWebpackPlugin({\n-     template: 'index.html',\n+     template: '!!prerender-loader?string!index.html',\n\n      // any other options you'd normally set are still supported:\n      compile: false,\n      inject: true\n    })\n  ]\n}\n```\n\nWhat does all that punctuation mean? Let's break the whole loader string\ndown:\n\n\u003e In Webpack, a module identifier beginning with `!!` will bypass any configured\n\u003eloaders from `module.rules` - here we're saying \"don't do anything to\n\u003e`index.html` except what I've defined here\n\u003e\n\u003eThe `?string` parameter tells `prerender-loader` to output an ES module\n\u003eexporting the prerendered HTML string, rather than returning the HTML directly.\n\u003e\n\u003eFinally, everything up to the last `!` in a module identifier is the inline\n\u003eloader definition (the transforms to apply to a given module).  The filename of\n\u003ethe module to load comes after the `!`.\n\u003e\n\u003e**Note:** If you've already set up `html-loader` or `raw-loader` to handle\n\u003e`.html` files, you can skip both options and simply pass a `template` value of\n\u003e`\"prerender-loader!index.html\"`!\n\nAs with any loader, it is also possible to apply `prerender-loader` on-the-fly\n:\n\n```js\nconst html = require('prerender-loader?!./app.html');\n```\n\n... or in your Webpack configuration's `module.rules` section:\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: 'src/index.html',\n        loader: 'prerender-loader?string'\n      }\n    ]\n  }\n}\n```\n\n\nOnce you have `prerender-loader` in-place, prerendering is now turned on. During\nyour build, the app will be executed, with any modifications it makes to\n`index.html` will be saved to disk.  This is fine for the needs of many apps,\nbut you can also take more explicit control over your prerendering: either using\nthe DOM or by rendering to a String.\n\n### DOM Prerendering\n\nDuring prerendering, your application gets compiled and run directly under\nNodeJS, but within a [JSDOM] container so that you can use the familiar browser\nglobals like `document` and `window`.\n\nHere's an example `entry` module that uses DOM prerendering:\n\n```js\nimport { render } from 'fancy-dom-library';\nimport App from './app';\n\nexport default () =\u003e {\n  render(\u003cApp /\u003e, document.body);\n};\n```\n\nIn all cases, asynchronous functions and callbacks are supported:\n\n```js\nimport { mount } from 'other-fancy-library';\nimport app from './app';\n\nexport default async function prerender() {\n  let res = await fetch('https://example.com');\n  let data = await res.json();\n  mount(app(data), document.getElementById('app'));\n}\n```\n\n### String Prerendering\n\nIt's also possible to export a function from your Webpack entry module, which\ngives you full control over prerendering: `prerender-loader` will call the\nfunction and its return value will be used as the static HTML.  If the exported\nfunction returns a Promise, it will be awaited and the resolved value will be\nused.\n\n```js\nimport { renderToString } from 'react-dom';\nimport App from './app';\n\nexport default () =\u003e {\n  const html = renderToString(\u003cApp /\u003e);\n  // returned HTML will be injected into \u003cbody\u003e:\n  return html;\n};\n```\n\nIn addition to DOM and String prerendering, it's also possible to use a\ncombination of the two.  If an application's Webpack entry exports a prerender\nfunction that doesn't return a value, the default DOM serialization will kick\nin, just like in DOM prerendering. This means you can use your exported\nprerender function to trigger DOM manipulation (\"client-side\" rendering), and\nthen just let `prerender-loader` handle generating the static HTML for whatever\ngot rendered.\n\nHere's an example that renders a [Preact] application and waits for DOM\nrendering to settle down before allowing prerender-loader to serialize the\ndocument to static HTML:\n\n```js\nimport { h, options } from 'preact';\nimport { renderToString } from 'preact';\nimport App from './app';\n\n// we're done when there are no renders for 50ms:\nconst IDLE_TIMEOUT = 50;\n\nexport default () =\u003e new Promise(resolve =\u003e {\n  let timer;\n  // each time preact re-renders, reset our idle timer:\n  options.debounceRendering = commit =\u003e {\n    clearTimeout(timer);\n    timer = setTimeout(resolve, IDLE_TIMEOUT);\n    commit();\n  };\n\n  // render into \u003cbody\u003e using normal client-side rendering:\n  render(\u003cApp /\u003e, document.body);\n});\n```\n\n### Injecting content into the HTML\n\nWhen applied to a `.html` file, `prerender-loader` will inject prerendered\ncontent at the end of `\u003cbody\u003e` by default.  If you want to place the content\nsomewhere else, you can add a `{{prerender}}` field:\n\n```html\n\u003chtml\u003e\n  \u003cbody\u003e\n    \u003cdiv id=\"app_root\"\u003e\n      \u003c!-- Inject any pre-rendered HTML here: --\u003e\n      {{prerender}}\n    \u003c/div\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nThis works well if you intend to provide a prerender function that only returns\nyour application's HTML structure, not the full document's HTML.\n\n### Prerendering JavaScript Files\n\nIn addition to processing `.html` files, the loader can also directly pre-render\n`.js` scripts. The only difference is that the DOM used for prerender will be\ninitially empty:\n\n```js\nconst prerenderedHtml = require('!prerender-loader?string!./app.js');\n```\n\n---\n\n## Options\n\nAll options are ... optional.\n\n| Option        | Type    | Default            | Description                                                            |\n| ------------- | ------- | ------------------ | ---------------------------------------------------------------------- |\n| `string`      | boolean | false              | Output a JS module exporting an HTML String instead of the HTML itself |\n| `disabled`    | boolean | false              | Bypass the loader entirely (but still respect `options.string`)        |\n| `documentUrl` | string  | 'http://localhost' | Change the jsdom's URL (affects `window.location`, `document.URL`...)  |\n| `params`      | object  | null               | Options to pass to your prerender function                             |\n| `env`         | object  | {}                 | Environment variables to define when building JS for prerendering      |\n\n\n---\n\n## License\n\n[Apache 2.0](LICENSE)\n\nThis is not an official Google product.\n\n[html-webpack-plugin]: https://github.com/jantimon/html-webpack-plugin\n[JSDOM]: https://github.com/jsdom/jsdom\n[Preact]: https://preactjs.com\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglechromelabs%2Fprerender-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgooglechromelabs%2Fprerender-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglechromelabs%2Fprerender-loader/lists"}