skip to Main Content

i want to prevent loading the rest of the page and just load the -head-, in case the page be too heavy and i just need the -title- so thats a total waste. any idea if this is possible or not?
thank you

2

Answers


  1. The closest you can come is to use the content-range request header to ask the server for a specific number of bytes.

    The specifics of how to do this depends on the HTTP API (node:http, fetch, axios, etc.) you are using.

    Limitations:

    • You have to guess how many bytes will be enough to get the entire <head>. This is hard. For example http://facebook.com gives me 27K of data before the end of </head> while http://example.com only has 940B.
    • Not all servers support Content-Range
    Login or Signup to reply.
  2. Another not-so-clean solution (in addition to Quentin‘s answer) is to "stream" the site to a buffer in chunks and abort once </head> is reached. The problem is, the last chunk could still contain more than just </head>. Should work nonetheless:

    const https = require("https");
    
    const options = {
      hostname: "stackoverflow.com",
      path: "/",
      method: "GET",
    };
    
    const req = https.request(options, res => {
      let buf = Buffer.alloc(0);
    
      res.on("data", chunk => {
        buf = Buffer.concat([buf, chunk]);
        const headEndIndex = buf.indexOf("</head>");
        if (headEndIndex !== -1){
          buf = buf.slice(0, headEndIndex + "</head>".length);
    
          console.log(buf.toString());
    
          res.destroy();
        }
      });
    });
    
    req.end();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search