Does Google execute JavaScript?
August 6, 2016
I’m told: Yes, it’s 2016; of course Google executes JavaScript.
But I’m also told: Server-side rendering is necessary for SEO.
If Google can run JavaScript and thereby render client-side views, why is server-side rendering necessary for SEO? Okay, Google isn’t the only search engine. But what if it’s the only one I care about?
Recently I ran a simple experiment to see to what extent the Google crawler understands dynamic content. I set up a web page at doesgoogleexecutejavascript.com which does the following:
- The HTML from the server contains text which says “Google does not execute JavaScript.”
- There is some inline JavaScript on the page that changes the text to “Google executes JavaScript, but only if it is embedded in the document.”
- The HTML also links to a script which, when loaded, changes the text to “Google executes JavaScript, even if the script is fetched from the network. However, Google does not make AJAX requests.”
- That script makes an AJAX and updates the text with the response from the server. The server returns the message “Google executes JavaScript and even makes AJAX requests.”
After I launched this page, I linked to it from its GitHub repository and waited for Google to discover it. Within a day, I saw the following in Google search:
I waited a week to see if Google would re-crawl the page and run the JavaScript—no dice. Then I manually submitted the page to the index from the Google Search Console. Shortly thereafter, I saw this:
It seems Google is not guaranteed to run your JavaScript automatically. You may have to manually trigger a crawl. And, even then, Google apparently won’t do any AJAX requests your page may depend on, or at least it didn’t in my case.
This is by no means a comprehensive treatise on Google’s propensity or lack thereof to execute JavaScript. Conclude what you want! If your client-rendered page has all the data it needs bootstrapped onto the page (i.e., it doesn’t need to make AJAX requests to do the initial render), then maybe you can have good SEO even without server-side rendering. But my conclusion is: do server-side rendering anyway just to be safe.
New Years Update (1/1/2017): Apparently some time after I did this experiment Google re-indexed the page and did the AJAX call this time:
Thanks pul for pointing that out.