stereobooster / react-snap

👻 Zero-configuration framework-agnostic static prerendering for SPAs
MIT License
5.06k stars 392 forks source link

Optimize performance of crawler #105

Open stereobooster opened 6 years ago

stereobooster commented 6 years ago

Something like minimalcss doing. This, theoretically, should decrease memory usage.

await page.setRequestInterception(true)
page.on('request', req => {
  if (
    req.resourceType === 'stylesheet' ||
    req.resourceType === 'font' ||
    req.resourceType === 'image'
  ) {
    req.abort()
  } else {
    req.continue()
  }
})

Use something else instead of networkidle, because networkidle waits 0,5 second after last network activity finished. Instead we can use page.waitFor. This, theoretically, should speed up things.

See: https://github.com/peterbe/minimalcss/pull/118

stereobooster commented 6 years ago

this is not the best way to do this:

  page.on("response", async response => {
   ....
        } else {
          await page.evaluate(route => {
            const linkTag = document.createElement("link");
            linkTag.setAttribute("rel", "preload");
            linkTag.setAttribute("as", "image");
            linkTag.setAttribute("href", route);
            document.body.appendChild(linkTag);
          }, route);
        }
stereobooster commented 6 years ago

https://github.com/stereobooster/react-snap/issues/139

} else if (preconnectThirdParty) {
  const urlObj = url.parse(responseUrl);
  const domain = `${urlObj.protocol}//${urlObj.host}`;
  if (uniqueResources.has(domain)) return;
  await page.evaluate(route => {
    const linkTag = document.createElement("link");
    linkTag.setAttribute("rel", "preconnect");
    linkTag.setAttribute("href", route);
    document.head.appendChild(linkTag);
  }, domain);
  uniqueResources.add(domain);
}