A
A
amorphine2016-06-16 16:45:07
HTML
amorphine, 2016-06-16 16:45:07

How to get an HTML stream in Gulp given a known URL?

The project uses critical CSS rules obtained using the Critical module. At the moment, it reads html from pre-saved web pages, the names of the routes, which then form part of the name of the generated critical css, are entered manually, which is very inconvenient if there are many routes (and therefore linked standard pages). And if the CSS changes, then you need to generate the critical one again, manually saving the pages again.
Therefore, there was a need for gulp to take a route from a pre-prepared object, download typical pages and generate CSS.
As it was before:

gulp.task("desktopCriticalCss", function() {
    var critical = require('critical'),
        object = "ProductProductCommon";
    critical.generate({
        base: 'uncss_ignore/',
        src: object + '.html',
        dest: 'css/critical/mobile' + object + '.css',
        minify: true,
        dimensions: [{
            width: 1920,
            height: 1080
        }],
    });
});

As you want now:
1. Store routes and standard pages in a separate file
var url2filenames =
    {
        "ProductProduct":
            [
            "https://***.ru/samsung-sm-a300f-galaxy-a3-white-133955.html",
            "https://***.ru/huawei-g620s-lte-black-136150.html"
            ],
    };
module.exports.url2filenames = url2filenames;

2. In a separate module, we generate critical css
var gulp = require("gulp"),
    critical = require('critical').stream,
    download = require("gulp-download-stream"),
    request = require('request'),
    url2filenames = require("./url_to_critical_filenames.js").url2filenames;

gulp.task("mobileCriticalCss", function() {
    for (route in url2filenames) {
        var urls = url2filenames[route];
        urls.forEach(function(url){
            request(url)
                .pipe(critical({
                    dest: 'css/critical/mobile/mobile' + route + '.css',
                    minify: true,
                    dimensions: [{
                        width: 360,
                        height: 640
                    }]
                }));
        });
    }
});

The last one is non-working code, just attempts. How can i get html from url?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2016-06-16
@amorphine

https://www.npmjs.com/package/request

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
  }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question