透過集合功能整理內容 你可以依據偏好儲存及分類內容。
修正會影響搜尋體驗的 JavaScript 問題
本指南可協助辨識和修正特定 JavaScript 問題,避免您的網頁 (或使用 JavaScript 語言的特定網頁內容) 因為這類問題而無法顯示在 Google 搜尋中。雖然 Google 搜尋能夠執行 JavaScript,但您在設計網頁和應用程式時還須考量一些不同的情況和限制,讓檢索器能順利存取並轉譯您的內容。您也可以參考我們的 JavaScript 搜尋引擎最佳化 (SEO) 基礎知識指南,進一步瞭解如何讓 JavaScript 網站在 Google 搜尋中獲得最佳成效。
Googlebot 有如一位良善的網路公民,主要任務是檢索網站,同時確保檢索作業不會破壞網站的使用體驗。Googlebot 與其網路轉譯服務 (WRS) 元件會持續分析並辨別哪些資源對基本網頁內容沒有幫助,從而避免擷取這類資源。舉例來說,對基本網頁內容沒有幫助的報表和錯誤要求,以及對於擷取基本網頁內容沒有利用價值的類似要求類型,都算是上述所說的資源。用戶端分析結果可能無法完整或準確地呈現出 Googlebot 和 WRS 在您網站上的活動,您可以使用 Google Search Console 中的檢索統計資料報告,監控 Googlebot 和 WRS 在網站上的活動和意見回饋。
如果您懷疑網頁 (或使用 JavaScript 語言的特定網頁內容) 可能因為 JavaScript 問題而遭到封鎖,導致無法顯示在 Google 搜尋中,請按照下列步驟操作:如果不確定 JavaScript 問題是否為主因,請按照我們的通用偵錯指南來判斷問題為何。
- 如要測試 Google 檢索及轉譯網址的情況,請使用 Search Console 中的複合式搜尋結果測試或網址檢查工具。您可以查看載入的資源、JavaScript 控制台輸出結果和例外狀況、已轉譯的 DOM 以及其他資訊。
此外,我們也建議收集使用者 (包括 Googlebot) 在您的網站上遇到的 JavaScript 錯誤並進行稽核,以便找出可能影響內容轉譯的潛在問題。以下範例展現了如何記錄通用 onerror 處理常式中記錄的 JavaScript 錯誤。注意:這種方法不能記錄某些類型的 JavaScript 錯誤,例如剖析錯誤。
window.addEventListener('error', function(e) { var errorText = [ e.message, 'URL: ' + e.filename, 'Line: ' + e.lineno + ', Column: ' + e.colno, 'Stack: ' + (e.error && e.error.stack || '(no stack trace)') ].join('\n'); // Example: log errors as visual output into the host page. // Note: you probably don't want to show such errors to users, or // have the errors get indexed by Googlebot; however, it may // be a useful feature while actively debugging the page. var DOM_ID = 'rendering-debug-pre'; if (!document.getElementById(DOM_ID)) { var log = document.createElement('pre'); log.id = DOM_ID; log.style.whiteSpace = 'pre-wrap'; log.textContent = errorText; if (!document.body) document.body = document.createElement('body'); document.body.insertBefore(log, document.body.firstChild); } else { document.getElementById(DOM_ID).textContent += '\n\n' + errorText; } // Example: log the error to remote service. // Note: you can log errors to a remote service, to understand // and monitor the types of errors encountered by regular users, // Googlebot, and other crawlers. var client = new XMLHttpRequest(); client.open('POST', 'https://example.com/logError'); client.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); client.send(errorText); });
- 請務必避免
soft 404
錯誤。 這是單頁應用程式 (SPA) 特別難以避免的問題。您可以採用下列一或兩種做法,防止系統為錯誤網頁建立索引: - 重新導向至伺服器傳回
404
狀態碼的網址。 fetch(`https://api.kitten.club/cats/${id}`) .then(res => res.json()) .then((cat) => { if (!cat.exists) { // redirect to page that gives a 404 window.location.href = '/not-found'; } });
- 新增 robots
meta
標記,或將其變更為 noindex
。 fetch(`https://api.kitten.club/cats/${id}`) .then(res => res.json()) .then((cat) => { if (!cat.exists) { const metaRobots = document.createElement('meta'); metaRobots.name = 'robots'; metaRobots.content = 'noindex'; document.head.appendChild(metaRobots); } });
當 SPA 以用戶端 JavaScript 處理錯誤時,通常回報的 HTTP 狀態碼是 200
,而非其他適當的狀態碼。這可能導致系統為錯誤網頁建立索引,並可能使該網頁出現在搜尋結果中。
- Googlebot 可能會拒絕使用者授權要求。
需要使用者授權的功能不適用於 Googlebot 或所有使用者。舉例來說,假設您要求使用 Camera API
,但 Googlebot 並沒有相機可供您使用。相反地,請在不強制使用者授予相機存取權的情況下,讓使用者能存取內容。 - 請勿使用網址片段載入不同的內容。
SPA 可能會使用網址片段 (例如 https://example.com/#/products) 載入不同的檢視畫面。我們已於 2015 年停止支援 AJAX 檢索配置,因此您無法仰賴網址片段供 Googlebot 進行檢索。我們建議使用 History API,以便根據 SPA 中的網址載入不同內容。 - 請勿依賴資料持續性來提供內容。
WRS 會載入個別網址 (請參閱Google 搜尋的運作方式,瞭解 Google 探索內容的大致方式),並執行伺服器和用戶端重新導向,運作方式與一般瀏覽器相同。不過,WRS 在各網頁載入時並不會保留狀態: - 本機儲存空間和工作階段儲存空間資料在各網頁載入時會遭到清除。
- HTTP Cookie 在各網頁載入時會遭到清除。
- 使用內容指紋以避免 Googlebot 的快取問題。
為了減少網路要求與資源使用,Googlebot 會主動進行快取。在此情況下,WRS 可能會因為忽略快取標頭而使用過時的 JavaScript 或 CSS 資源。如要避免這個問題,您可以建立內容指紋,使其成為檔案名稱的一部分 (例如 main.2bb85551.js
)。由於指紋會反映檔案內容,因此只要檔案內容有所更新,系統就會產生一個新檔名。如要瞭解詳情,請參閱 web.dev 長效快取策略指南。 - 務必讓您的應用程式對所需的任何重要 API 使用功能偵測,並視情況提供備援行為或 Polyfill。
有些使用者代理程式可能尚未採用所有網路功能,也可能刻意停用特定功能。舉例來說,如果您使用 WebGL 在瀏覽器中算繪相片特效,功能偵測會顯示 Googlebot 不支援 WebGL。如要修正這個問題,您可以去掉相片特效,或是選擇使用伺服器端算繪功能來預先算繪相片特效,這樣就能讓所有人 (包括 Googlebot) 都能存取您的內容。 - 確認您的內容支援 HTTP 連線。
Googlebot 會使用 HTTP 要求從您的伺服器擷取內容,並不支援 WebSockets
或 WebRTC
等其他類型的連線。為了避免發生這類連線的支援問題,請務必提供備用 HTTP 以供擷取內容,並使用完善的錯誤處理程序和功能偵測功能。 - 確保您的網頁元件能正常轉譯。 運用複合式搜尋結果測試或網址檢查工具,檢查轉譯後的 HTML 是否包含預期中的所有內容。
WRS 會壓縮 light DOM 和 shadow DOM。如果您使用的網頁元件並未採用 light DOM 內容的 <slot>
機制,請參閱網頁元件的說明文件以進一步瞭解更多資訊,或改用其他網頁元件。如需詳細資訊,請參閱網頁元件最佳做法。 - 當您修正這份檢查清單中的問題後,請再次使用 Search Console 中的複合式搜尋結果測試或網址檢查工具對網頁進行測試。
如果已修正問題,就只會看到綠色勾號。如果仍有錯誤項目,請前往 搜尋中心產品討論社群發文提問。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-04 (世界標準時間)。
[null,null,["上次更新時間:2025-08-04 (世界標準時間)。"],[[["\u003cp\u003eThis guide helps you identify and fix JavaScript issues that may be preventing your website content from appearing correctly in Google Search.\u003c/p\u003e\n"],["\u003cp\u003eUse the Rich Results Test or the URL Inspection Tool to see how Googlebot crawls and renders your web pages, including loaded resources, JavaScript errors, and the rendered DOM.\u003c/p\u003e\n"],["\u003cp\u003eEnsure your single-page application handles soft 404 errors correctly by redirecting to a 404 page or using the noindex meta tag for error pages.\u003c/p\u003e\n"],["\u003cp\u003eAvoid using URL fragments for loading content and rely on the History API instead, and ensure your web components render as expected by using the \u003ccode\u003e<slot>\u003c/code\u003e mechanism for light DOM content.\u003c/p\u003e\n"],["\u003cp\u003eGooglebot has limitations, such as declining permission requests and not supporting data persistence or connections like WebSockets and WebRTC, so design your content accordingly with fallbacks.\u003c/p\u003e\n"]]],["To resolve JavaScript-related search issues, first, test how Google crawls and renders your URLs using the Rich Results Test or URL Inspection Tool. Collect and audit JavaScript errors. Prevent soft 404 errors by redirecting or adding a \"noindex\" meta tag. Avoid relying on user permission requests, URL fragments, or data persistence. Employ content fingerprinting to avoid caching issues. Use feature detection for critical APIs, ensure HTTP connection compatibility, and verify web component rendering. Finally, retest your page after fixes.\n"],null,["Fix Search-related JavaScript problems\n\n\nThis guide helps you identify and fix JavaScript issues that may be blocking your page, or specific content on JavaScript powered pages, from showing up in Google Search.\nWhile Google Search does run JavaScript, there are some differences and limitations that you need to account for when designing your pages and applications to accommodate how crawlers access and render your content.\nOur [guide on JavaScript SEO basics](/search/docs/guides/javascript-seo-basics) has more information on how you can optimize your JavaScript site for Google Search.\n\nGooglebot is designed to be a good citizen of the web. Crawling is its [main priority](/search/blog/2017/01/what-crawl-budget-means-for-googlebot), while making sure it doesn't degrade the experience of users visiting the site.\nGooglebot and its Web Rendering Service (WRS) component continuously analyze and identify resources that don't contribute to essential page content and may not fetch such resources.\nFor example, reporting and error requests that don't contribute to essential page content, and other similar types of requests are unused or unnecessary to extract essential page content. Client-side analytics may not provide a full or accurate representation of Googlebot and WRS activity on your site.\nUse [the crawl stats report in Google Search Console](https://support.google.com/webmasters/answer/9679690) to monitor Googlebot and WRS activity and feedback on your site.\n\n\nIf you suspect that JavaScript issues might be blocking your page, or specific content on JavaScript powered pages, from showing up in Google Search, follow these steps. If you're not sure if JavaScript is the main cause, follow our [general debugging guide](/search/docs/guides/debug) to determine the specific issue.\n\n1. **To test how Google crawls and renders a URL** , use the [Rich Results Test](https://search.google.com/test/rich-results) or the [URL Inspection Tool](https://support.google.com/webmasters/answer/9012289) in Search Console. You can see loaded resources, JavaScript console output and exceptions, rendered DOM, and more information.\n\n\n Optionally, we also recommend collecting and auditing JavaScript errors encountered by users, including Googlebot, on your site to identify potential issues that may affect how content is rendered.\n Here's an example that shows how to log JavaScript errors that are logged in the [global onerror handler](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror). Note that some types of JavaScript errors, such as a parse error, cannot be logged with this method. \n\n ```javascript\n window.addEventListener('error', function(e) {\n var errorText = [\n e.message,\n 'URL: ' + e.filename,\n 'Line: ' + e.lineno + ', Column: ' + e.colno,\n 'Stack: ' + (e.error && e.error.stack || '(no stack trace)')\n ].join('\\n');\n\n // Example: log errors as visual output into the host page.\n // Note: you probably don't want to show such errors to users, or\n // have the errors get indexed by Googlebot; however, it may\n // be a useful feature while actively debugging the page.\n var DOM_ID = 'rendering-debug-pre';\n if (!document.getElementById(DOM_ID)) {\n var log = document.createElement('pre');\n log.id = DOM_ID;\n log.style.whiteSpace = 'pre-wrap';\n log.textContent = errorText;\n if (!document.body) document.body = document.createElement('body');\n document.body.insertBefore(log, document.body.firstChild);\n } else {\n document.getElementById(DOM_ID).textContent += '\\n\\n' + errorText;\n }\n\n // Example: log the error to remote service.\n // Note: you can log errors to a remote service, to understand\n // and monitor the types of errors encountered by regular users,\n // Googlebot, and other crawlers.\n var client = new XMLHttpRequest();\n client.open('POST', 'https://example.com/logError');\n client.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');\n client.send(errorText);\n\n });\n ```\n2. **Make sure to prevent [`soft 404` errors](/search/docs/crawling-indexing/http-network-errors#soft-404-errors).** In a single-page application (SPA), this can be especially difficult. To prevent error pages from being indexed, you can use one or both of the following strategies:\n - Redirect to a URL where the server responds with a `404` status code. \n\n ```javascript\n fetch(`https://api.kitten.club/cats/${id}`)\n .then(res =\u003e res.json())\n .then((cat) =\u003e {\n if (!cat.exists) {\n // redirect to page that gives a 404\n window.location.href = '/not-found';\n }\n });\n ```\n - Add or change the robots `meta` tag to `noindex`. \n\n ```javascript\n fetch(`https://api.kitten.club/cats/${id}`)\n .then(res =\u003e res.json())\n .then((cat) =\u003e {\n if (!cat.exists) {\n const metaRobots = document.createElement('meta');\n metaRobots.name = 'robots';\n metaRobots.content = 'noindex';\n document.head.appendChild(metaRobots);\n }\n });\n ```\n\n\n When a SPA is using client-side JavaScript to handle errors they often report a `200` HTTP status code instead of the [appropriate status code](/search/docs/crawling-indexing/javascript/javascript-seo-basics#use-meaningful-http-status-codes). This can lead to error pages being indexed and possibly shown in search results.\n3. **Expect Googlebot to decline [user permission requests](https://w3c.github.io/permissions/#permission-registry).** \n Features that require user permission don't make sense for Googlebot, or for all users. For example, if you make the `Camera API` required, Googlebot can't provide a camera to you. Instead, provide a way for users to access your content without being forced to allow camera access.\n4. **Don't use URL fragments to load different content.** \n A SPA may use URL fragments (for example https://example.com/#/products) for loading different views. The [AJAX-crawling scheme has been deprecated](/search/blog/2015/10/deprecating-our-ajax-crawling-scheme) since 2015, so you can't rely on URL fragments to work with Googlebot. We recommend using the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History) to load different content based on the URL in a SPA.\n5. **Don't rely on data persistence to serve content.** \n WRS loads each URL (refer to [How Google Search Works](/search/docs/fundamentals/how-search-works) for an overview of how Google discovers content), following server and client redirects, same as a regular browser. However, WRS does not retain state across page loads:\n - Local Storage and Session Storage data are cleared across page loads.\n - HTTP Cookies are cleared across page loads.\n6. **Use content fingerprinting to avoid caching issues with Googlebot.** \n Googlebot caches aggressively in order to reduce network requests and resource usage. WRS may ignore caching headers. This may lead WRS to use outdated JavaScript or CSS resources. Content fingerprinting avoids this problem by making a fingerprint of the content part of the filename, like `main.2bb85551.js`. The fingerprint depends on the content of the file, so updates generate a different filename every time. Check out the [web.dev guide on long-lived caching strategies](https://web.dev/articles/http-cache#versioned-urls) to learn more.\n7. **Ensure that your application uses [feature detection](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Feature_detection) for all critical APIs that it needs and provide a fallback behavior or polyfill where applicable.** \n Some web features may not yet be adopted by all user agents and some may intentionally disable certain features. For example, if you use WebGL to render photo effects in the browser, feature detection shows that Googlebot doesn't support WebGL. To fix this, you could skip the photo effect or decide to use server-side rendering to prerender the photo effects, which makes your content accessible to everyone, including Googlebot.\n8. **Make sure your content works with HTTP connections.** \n Googlebot uses HTTP requests to retrieve content from your server. It does not support other types of connections, such as [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) or [WebRTC](https://developer.mozilla.org/en-US/docs/Glossary/WebRTC) connections. To avoid problems with such connections, make sure to provide an HTTP fallback to retrieve content and use robust error handling and [feature detection](#feature-detection).\n9. **Make sure your web components render as expected.** Use the [Rich Results Test](https://search.google.com/test/rich-results) or the [URL Inspection Tool](https://support.google.com/webmasters/answer/9012289) to check if the rendered HTML has all content you expect. \n WRS flattens the [light DOM and shadow DOM](/web/fundamentals/web-components/shadowdom#lightdom). If the web components you use aren't using [`\u003cslot\u003e` mechanism](/web/fundamentals/web-components/shadowdom#slots) for light DOM content, consult the documentation of the web component for further information or use another web component instead. For more information, see [best practices for web components](/search/docs/guides/javascript-seo-basics#web-components).\n10. **After you fix the items in this checklist, test your page** with the [Rich Results Test](https://search.google.com/test/rich-results) or the [URL inspection tool](https://search.google.com/search-console) in Search Console again.\n\n If you fixed the issue, a green check mark appears and no errors display. If you still see errors, post in the [Search Central help community](https://support.google.com/webmasters/community)."]]