HACKER Q&A
📣 progval

Why does YouTube have 200kB of JavaScript before the title tag?


I maintain a bot that listens for URLs in a chat, fetches the first kB of the page to extract their and post it in the chat.<p>A few years ago, it stopped working on Youtube. I increased the limit to 32kB, as I figured it would give a safe margin.<p>Well, it broke again some time this month:<p><pre><code> >>> import urllib.request >>> urllib.request.urlopen('https://www.youtube.com/watch?v=dQw4w9WgXcQ').read().index(b'<title') 192194 </code></pre> This seems to be mostly inlined minified Javascript. Why does Youtube do this instead of including scripts that could be cached?</h4> </div> </div> <hr> <div class="card"> <div class="card-header"> <span alt="capableweb - Accepted Answer" title="capableweb - Accepted Answer" class="ps-1 btn btn-success text-white">   👤 capableweb</span> <span class="text-success"> Accepted Answer ✓</span> </div> <div class="card-body text-start"> <h5>Why don't try to actually read the HTML within the <head> tags instead, and parse the title from there? Should be clean data and survive for much longer.<p>As for why someone would embed JavaScript directly on page instead of via a script, usually you want that piece of code to run as soon as possible when the user opens the code. Including scripts that gets loaded over network can get loaded much later compared to when you inline it.</h4> </div> </div> </div> <hr> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script> <!-- Default Statcounter code for hackerqa https://hackerqa.net --> <script type="text/javascript"> var sc_project=13078448; var sc_invisible=1; var sc_security="656d49db"; </script> <script type="text/javascript" src="https://www.statcounter.com/counter/counter.js" async></script> <noscript><div class="statcounter"><a title="Web Analytics Made Easy - Statcounter" href="https://statcounter.com/" target="_blank"><img class="statcounter" src="https://c.statcounter.com/13078448/0/656d49db/1/" alt="Web Analytics Made Easy - Statcounter" referrerPolicy="no-referrer-when-downgrade"></a></div></noscript> <!-- End of Statcounter Code --> </body> </html>