1

I am maintaining a single page app and I am trying to make it friendlier to search engines. The way the page works is that when a user clicks on an internal link, an AJAX request is made to fetch new content and then the DOM is updated to display the new content. My job is to add a history.pushState somewhere so that search engines know when the state of the page has changed. Right now, I am experimenting with the following order or operations:

await fetchNewContent(...);
updateDOM(...);
history.pushState(...);

My thinking is that history.pushState should be called after the page has been made because search engines index the snapshot of the page at the time this function is called. The problem is that I am not sure that search engines work that way, and putting the asynchronous fetchNewContent first, complicates the logic of my code. It would be nice if I could write my code in the following way:

history.pushState(...);
await fetchNewContent(...);
updateDOM(...);

The thinking here is that the search engine will see that some js code is still running after the call to history.pushState and wait for that code to complete before indexing the page.

What are there the best practices here? Does Google have any public documentation about this?

  • Yes, I saw that. That question and its answers are old and it doesn't explain in detail how google currently handles pushState. – safsaf32 Nov 8 '17 at 11:55
  • pushState is for users as they perform actions on your site. Googlebot isn't going to perform actions, so there is no need to worry about how it handles push state. To be search engine friendly, your links need to have a normal a href for Googlebot. Googlebot doesn't execute the pushState, it only records the URLs it finds. Your site should change the content based on the URL that Googlebot requests before the onload event. Googlebot will fetch different URLs for different pages. – Stephen Ostermiller Nov 8 '17 at 12:03
  • I do the href think already and I use a unique url for each state of my app. The reason I am posting this question is that in my initial implementation, I accidentally put the update of the document title after pushState. The end result was that google indexed some pages with the wrong title. Specifically, some pages were shown in Google having as their title the title of an unrelated page. That makes me thing that pushState does have an effect on Google's indexing. – safsaf32 Nov 8 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.