71.8 F
Austin
Monday, April 19, 2021

SOLVED: How to fix Document.write errors on WordPress (Newspaper Theme)

HEADLINE developer Aaron Martinez finds a {code-fix} solution to the "Document.write" errors on WordPress.

Must Read

Microsoft Edge, Mozilla Firefox, and Google Chrome want to make one thing clear in 2020: The worst-practices intrusion of Document.write() HTML will no longer be tolerated. With the arrival of a revamped GTMetrix, now harnessing Lighthouse, Document. write() errors carry serious grading weight. But HEADLINE found a solution for the document.write() errors in Lighthouse/Gtmetrix site audits.

The {code-fix} solution has undergone basic cross-platform testing [Firefox, Chrome, Microsoft Edge] and it cuts the mustard. For HEADLINE’s index (which I’ve optimized through Td Composer, WP Rocket, Rank Math, and Asset CleanUp–I double-set the code:

  1. At the top of HEADLINE’s head tag [sitewide] in the plugin “Headers and Footers Scripts” [Position 1].
  2. At the top of the HTML tag in TagDiv’s theme panel [custom code][custom HTML].

I chose to double-set the {code-fix} because the Document.write() call is originating from Newspaper scripts and I don’t manage HEADLINE’s custom HTML there. I prefetch Google DNS Requests with WP Rocket and save custom HTML in “Headers and Footers Scripts.” The latter being hands-down the absolute best plugin to organize tags/add custom scripts and overwrite other plugins.

I tested a number of {code-fix} arrangements and found that when I only set the {code-fix} in “Headers and Footers Scripts” that some of the Document.write(s) can slip through the cracks. Alternatively, if I only set the {code-fix} in the TagDiv theme panel, Wp Rocket’s Google DNS Requests override the custom HTML. So hierarchically, “Headers and Footers Scripts” can reposition the TagDiv custom HTML to take first-bite at the Document.write(s) while still allowing WP Rocket to prefetch the Google DNS.

I re-audited HEADLINE (with Lighthouse and GTMetrix) after optimizing the {code-fix} placement and HEADLINE’s speed/optimization scores shot way up–which actually makes sense if you think about what document.write() is really doing. Code calls for document.write() are telling the parser to rewrite the page mid-read, which can lead to bottlenecks and pose security concerns, especially when compared to security-heavy iframes or other delayable embeds.

Also, it’s worth noting that the document.write() code fix I’m using on HEADLINE is not exactly best-practices. In a perfect world, digital ads would natively load asynchronously. But until Newspaper and other themes drop support for document.write() altogether, AdjacentHTML will have to do.


How To Fix Document.Write
beenhere black 48dp 1

Insert javascript in head tag.

{code-fix}

<script>
    document.write=function(s){
    var scripts = document.getElementsByTagName(‘script’);
    var lastScript = scripts[scripts.length-1];
    lastScript.insertAdjacentHTML(“beforebegin”, s);
</script>

inFAQ™
by HEADLINE

If you guys want to check out a heavily-customized Newspaper site, go checkout HEADLINE.dev. I’ve built a fully-responsive 4-screen Newspaper page, as well as a totally custom Amp site for mobile and desktop. Every single desktop page has a custom Amp version. I even built a one-of-a-kind Bootstrap form (Amp-compatible, Formidable Forms-based) called Funder by HEADLINE. I’ll be writing a series of articles, like this one, about how to optimize/extend the Newspaper theme.

I’d also like to reiterate that I am not a professional WordPress developer. There is a non-zero number of kinks with HEADLINE that I am still working out, but I’ve had an overwhelmingly positive experience building on the TagDiv platform thus far. The Newspaper development/support team also deserves recognition, a world-class outfit seriously committed to the advancement of journalism on WordPress!

Aaron Martinez

– Editor-in-Chief of HEADLINE

- Advertisement -
- Advertisement -
- Advertisement -

Latest News

SEC chair Gary Gensler was the Crypto Professor at MIT

Gary Gensler was confirmed as the new SEC chair by the U.S Senate on April 14, 2021. While Democrats,...
- Google Reviews -

HEADLINE IconHEADLINE

San Antonio, Texas. United States

5.0 7 reviews

More Articles Like This

- Advertisement -
- Advertisement -
- Advertisement -