From 12748e2f96619da6ea1fccbb1d54043c27d18e2f Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Sat, 5 Dec 2020 14:37:44 +0100 Subject: [PATCH 1/4] update deps, fix normalizeUrl issue --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index e862fc5..5c38f69 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const normalizeUrl = require('normalize-url') const isUrl = require('is-url') const LRU = require('lru-cache') const {extractCss} = require('./_chromium') @@ -9,7 +8,7 @@ const cssCache = new LRU({ }) module.exports = async (req, res) => { - const url = normalizeUrl(req.url.slice(1), {stripWWW: false}) + const url = req.url.slice(1) if (!isUrl(url)) { res.statusCode = 406 From b02d6eb13caed37b2c44c90da3988450bb30ce73 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Tue, 23 Feb 2021 22:59:20 +0100 Subject: [PATCH 2/4] Set custom User-Agent string for Puppeteer as workaround for websites that block Puppeteer when `HeadlessChrome` is present in the UA string. --- _chromium.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_chromium.js b/_chromium.js index 3fa88a7..0ae0aee 100644 --- a/_chromium.js +++ b/_chromium.js @@ -12,6 +12,10 @@ exports.extractCss = async url => { }) const page = await browser.newPage() + // Set an explicit UserAgent, because the default UserAgent string includes something like + // `HeadlessChrome/88.0.4298.0` and some websites/CDN's block that with a HTTP 403 + await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:85.0) Gecko/20100101 Firefox/85.0') + // Start CSS coverage. This is the meat and bones of this module await page.coverage.startCSSCoverage().catch(() => {}) From d3bad111c1f4a81b4a8c54a902288d07a0f7bfcf Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Tue, 23 Feb 2021 23:03:08 +0100 Subject: [PATCH 3/4] fix oopsie --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5c38f69..e862fc5 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const normalizeUrl = require('normalize-url') const isUrl = require('is-url') const LRU = require('lru-cache') const {extractCss} = require('./_chromium') @@ -8,7 +9,7 @@ const cssCache = new LRU({ }) module.exports = async (req, res) => { - const url = req.url.slice(1) + const url = normalizeUrl(req.url.slice(1), {stripWWW: false}) if (!isUrl(url)) { res.statusCode = 406 From 855ea98701163b5076185f754eb088a03effc225 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Tue, 23 Feb 2021 23:07:44 +0100 Subject: [PATCH 4/4] fix oopsie --- readme.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/readme.md b/readme.md index e8db696..f7fccbd 100644 --- a/readme.md +++ b/readme.md @@ -14,11 +14,6 @@ The folks from [CSS Stats](https://cssstats.com/) have created [get-css](https:/ This package uses an actual browser under the hood to get all the CSS and exposes an HTTP endpoint that accepts a url to get the CSS from. -## Local testing - -I have no idea how local testing for Now is supposed to work, so I created a tiny HTTP server in `dev.js` that calls the actual function that gets deployed. -Run `ENV=dev npm run dev` to run a local version of the function for local testing. - ## Credits - This repo is pretty much an exact copy of [this example from Zeit](https://github.com/zeit/now-examples/tree/master/puppeteer-screenshot).