问题原文
原帖链接 · BlackStar1991 · CC BY-SA 4.0
Is it possible to disable Google indexing of some web page if this is placed to head tag
<meta name="googlebot" content="noindex" />
using only JS? Or is it all a hoax and only the finished page is taken into account when indexing?
已采纳解答
原帖链接 · Stephen Ostermiller · CC BY-SA 4.0
Google supports inserting meta tags with JavaScript, including noindex. You can have JavaScript insert <meta name="googlebot" content="noindex" /> into the <head> of the page and Google will stop indexing the page once it renders it.
Google recommends inserting noindex with JavaScript in its documentation: Understand JavaScript SEO Basics | Google Search Central | Google Developers
Avoid soft 404 errors in single-page apps
In client-side rendered single-page apps, routing is often implemented as client-side routing. In this case, using meaningful HTTP status codes can be impossible or impractical. To avoid soft 404 errors when using client-side rendering and routing, use one of the following strategies:
- Use a JavaScript redirect to a URL for which the server responds with a 404 HTTP status code (for example
/not-found).- Add a
<meta name="robots" content="noindex">to error pages using JavaScript....
Here is sample code for the noindex tag approach:
fetch(`/api/products/${productId}`) .then(response => response.json()) .then(product => { if(product.exists) { showProductDetails(product); // shows the product information on the page } else { // this product does not exist, so this is an error page. // Note: This example assumes there is no other meta robots tag present in the HTML. const metaRobots = document.createElement('meta'); metaRobots.name = 'robots'; metaRobots.content = 'noindex'; document.head.appendChild(metaRobots); } })
The usual caveats about JavaScript rendering and SEO apply: It often takes Google extra time (weeks or even a couple months) to decide that it needs to render the page, render it, and react to the results. In other words inserting noindex with JavaScript is not likely to be as fast as inserting it into the HTML code on the back end.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
