JavaScript SEO 基礎:Google 如何處理 JavaScript 網頁
更新 2026-10-12
若懷疑 JavaScript 導致網頁或部分內容無法出現在 Google 搜尋中,請先參閱JavaScript 疑難排解指南,了解如何修正相關問題。
JavaScript 是網路平台的關鍵元件:這類程式語言讓網站能做成功能完整的應用程式。只要讓他人能透過 Google 搜尋找到你的 JavaScript 網路應用程式,就有機會在使用者搜尋相關內容時觸及新使用者,並再次吸引既有使用者。Google 搜尋會以持續更新的 Chromium 版本1執行 JavaScript,你仍可以透過最佳化作法修正部分問題。
目錄(15)
來源聲明 原文
完整來源聲明
來源聲明:本文依 Google Search Central 文件〈Understand JavaScript SEO Basics〉(CC BY 4.0)全文翻譯為繁體中文,並依本站體例加入常見問題、名詞註腳與編輯解讀;段落順序與原文一致。原文連結:https://developers.google.com/search/docs/crawling-indexing/javascript/javascript-seo-basics。授權:Creative Commons Attribution 4.0 International。文中「編輯解讀」為本站觀點,不屬於原文。檢索日期:2026-10-12。
本指南說明 Google 搜尋如何處理 JavaScript,以及針對 Google 搜尋改善 JavaScript 網路應用程式的最佳做法。
Google 如何處理 JavaScript
Google 處理 JavaScript 網頁應用程式大致分三個階段:
- 檢索(crawling)
- 轉譯(rendering)
- 建立索引(indexing)
Googlebot 會把網頁排入檢索佇列與轉譯佇列。網頁等待檢索與轉譯時,沒有任何明確訊號能即時反映當下狀態。在對檢索佇列中的網址發出 HTTP 要求前,Googlebot 會先讀取 robots.txt2,確認你是否允許檢索。若 robots.txt 禁止該網址,Googlebot 就不會發出 HTTP 要求,也不會檢索該網址。Google 搜尋不會從被封鎖的檔案或被封鎖的網頁中轉譯 JavaScript。
接著 Googlebot 會剖析 HTML,並從連結的 href 屬性3找出其他網址,加入檢索佇列。若要阻止 Googlebot 發現特定連結,請使用 nofollow 機制4。你可以用 JavaScript 把連結插入 DOM,前提是這些連結須符合可檢索連結的最佳做法。
在傳統網站或伺服器端轉譯的網頁上,HTTP 回應中的 HTML 通常已包含主要內容,因此 Googlebot 能直接檢索並剖析。部分 JavaScript 網站採用應用程式 Shell 模型(application shell model),初始 HTML 不含實際內容,此時 Google 必須執行 JavaScript 才能看見由程式產生的內容。
除非 robots meta 標記或 HTTP 標頭5要求 Google 不要建立索引,否則 Googlebot 會把回傳 200 HTTP 狀態碼的網頁排入轉譯佇列。網頁可能在佇列中停留數秒,也可能更久。在資源允許時,無頭 Chromium 會轉譯網頁並執行 JavaScript。Googlebot 會再次剖析轉譯後 HTML 中的連結,把新網址加入檢索佇列;Google 也會用轉譯後的 HTML 建立索引。所有回傳 200 的網頁都會進入轉譯佇列,無論頁面上是否有 JavaScript。若 HTTP 狀態碼不是 200(例如 404 錯誤頁),系統可能略過轉譯。
Google 仍建議採用伺服器端轉譯或預先轉譯(pre-rendering),讓使用者與檢索器更快瀏覽網站;也不是所有爬蟲都能執行 JavaScript。
使用獨特的標題與摘要描述網頁
提供獨特、具描述性的 <title> 元素與 meta description(描述中繼標籤)6,有助使用者快速判斷哪一則搜尋結果最符合需求。你可以用 JavaScript 設定或變更描述與 <title>。
設定標準網址
rel="canonical" 連結標記7可協助 Google 找出網頁的標準版本。你可以用 JavaScript 設定標準網址,但不要用 JavaScript 把標準網址改成與原始 HTML 中不同的網址。最佳做法是在 HTML 中設定;若必須用 JavaScript,請維持與原始 HTML 相同的值。若無法在 HTML 中設定,才用 JavaScript 設定,並在原始 HTML 中略過該網址。
撰寫相容的程式碼
瀏覽器 API 種類繁多,JavaScript 又快速演進,Google 對部分 API 與功能支援有限。為確保程式碼與 Google 相容,請遵循 JavaScript 疑難排解指南。
若透過功能偵測發現缺少必要 API,建議採用漸進增強與 polyfill。部分瀏覽器功能無法 polyfill,請閱讀 polyfill 文件以了解限制。
使用有意義的 HTTP 狀態碼
Googlebot 會依 HTTP 狀態碼8判斷檢索過程是否出錯。
用有意義的狀態碼告知 Googlebot 是否應檢索或建立索引:例如以 404 表示找不到網頁,或以 401 表示須登入才能存取。也可用狀態碼告知網頁已移至新網址,以便更新索引。
請參閱 Google 文件中的 HTTP 狀態碼清單,以及各狀態碼對 Google 搜尋的影響。
避免單頁應用程式出現 soft 404
在用戶端轉譯的單頁應用程式(SPA)中,導覽常以用戶端路由完成,此時可能無法使用有意義的 HTTP 狀態碼,或即使使用也無法如預期運作。為避免 soft 404,可採下列其一:
- 使用 JavaScript 重新導向至伺服器回傳
404的網址(例如/not-found)。 - 用 JavaScript 在錯誤頁加入
<meta name="robots" content="noindex">。
重新導向做法範例:
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.
window.location.href = '/not-found'; // redirect to 404 page on the server.
}
})
noindex 標記做法範例:
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 robots meta tag present in the HTML.
const metaRobots = document.createElement('meta');
metaRobots.name = 'robots';
metaRobots.content = 'noindex';
document.head.appendChild(metaRobots);
}
})
使用 History API,而非網址片段(hash)
Google 只能找到 <a> 元素上含 href 屬性9的連結。
對採用用戶端路由的 SPA,請用 History API 在不同檢視間導覽,並確保 Googlebot 能剖析並擷取網址;避免用 hash 片段載入不同內容。下列為不當示例,Googlebot 難以正確解析:
<nav>
<ul>
<li><a href="#/products">Our products</a></li>
<li><a href="#/services">Our services</a></li>
</ul>
</nav>
<h1>Welcome to example.com!</h1>
<div id="placeholder">
<p>Learn more about <a href="#/products">our products</a> and <a href="#/services">our services</a></p>
</div>
<script>
window.addEventListener('hashchange', function goToPage() {
// this function loads different content based on the current URL fragment
const pageToLoad = window.location.hash.slice(1); // URL fragment
document.getElementById('placeholder').innerHTML = load(pageToLoad);
});
</script>
改為使用 History API,讓 Googlebot 能存取可檢索的連結網址:
<nav>
<ul>
<li><a href="/products">Our products</a></li>
<li><a href="/services">Our services</a></li>
</ul>
</nav>
<h1>Welcome to example.com!</h1>
<div id="placeholder">
<p>Learn more about <a href="/products">our products</a> and <a href="/services">our services</a></p>
</div>
<script>
function goToPage(event) {
event.preventDefault(); // stop the browser from navigating to the destination URL.
const hrefUrl = event.target.getAttribute('href');
const pageToLoad = hrefUrl.slice(1); // remove the leading slash
document.getElementById('placeholder').innerHTML = load(pageToLoad);
window.history.pushState({}, window.title, hrefUrl) // Update URL as well as browser history.
}
// Enable client-side routing for all links on the page
document.querySelectorAll('a').forEach(link => link.addEventListener('click', goToPage));
</script>
正確插入 rel=”canonical” 連結標記
Google 不建議用 JavaScript 處理標準網址,但技術上可行。轉譯時 Google 搜尋會採用插入的標準網址。範例:
fetch('/api/cats/' + id)
.then(function (response) { return response.json(); })
.then(function (cat) {
// creates a canonical link tag and dynamically builds the URL
// e.g. https://example.com/cats/simba
const linkTag = document.createElement('link');
linkTag.setAttribute('rel', 'canonical');
linkTag.href = 'https://example.com/cats/' + cat.urlFriendlyName;
document.head.appendChild(linkTag);
});
以 JavaScript 插入 rel="canonical" 時,請確保頁面上只有一個標準連結標記。錯誤實作可能產生多個標記或改寫既有標記,導致非預期結果。
請謹慎使用 robots meta 標記
你可以用 robots meta 標記10禁止 Google 建立索引或追蹤連結。例如在頁首加入:
<!-- Google won't index this page or follow links on this page -->
<meta name="robots" content="noindex, nofollow">
亦可用 JavaScript 新增或變更 robots meta。下列範例在 API 無法回傳內容時,以 JavaScript 設定 noindex:
fetch('/api/products/' + productId)
.then(function (response) { return response.json(); })
.then(function (apiResponse) {
if (apiResponse.isError) {
// get the robots meta tag
var metaRobots = document.querySelector('meta[name="robots"]');
// if there was no robots meta tag, add one
if (!metaRobots) {
metaRobots = document.createElement('meta');
metaRobots.setAttribute('name', 'robots');
document.head.appendChild(metaRobots);
}
// tell Google to exclude this page from the index
metaRobots.setAttribute('content', 'noindex');
// display an error message to the user
errorMsg.textContent = 'This product is no longer available';
return;
}
// display product information
// ...
});
若 Google 在原始 HTML 中發現 noindex,可能略過轉譯與 JavaScript 執行;因此事後用 JavaScript 移除或改寫 noindex 可能無效。若確實希望建立索引,請勿在初始 HTML 中使用 noindex。
採用長效快取
為減少網路要求與資源使用,Googlebot 會積極快取。在此情況下,網頁轉譯服務(WRS)可能因忽略快取標頭而使用過期的 JavaScript 或 CSS。建議在檔名加入內容指紋(例如 main.2bb85551.js),內容變更時檔名即變更。詳見 web.dev 長效快取策略11。
使用結構化資料
在網頁使用結構化資料時,可以用 JavaScript 產生 JSON-LD 並插入頁面。請務必測試導入,以免出錯。
遵循網頁元件(Web Components)最佳做法
Google 支援網頁元件。轉譯時會壓平 shadow DOM 與 light DOM 的內容,也就是說 Google 只看轉譯後 HTML 中可見的部分。請用複合式搜尋結果測試或 Search Console 網址檢查工具查看轉譯後 HTML。
若內容在轉譯後 HTML 中不可見,Google 就無法建立索引。
下列範例建立自訂元件,並在 shadow DOM 中顯示 light DOM 內容;若要兩者都出現在轉譯結果,其中一種做法是使用 <slot> 元素:
<script>
class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
let p = document.createElement('p');
p.innerHTML = 'Hello World, this is shadow DOM content. Here comes the light DOM: <slot></slot>';
this.shadowRoot.appendChild(p);
}
}
window.customElements.define('my-component', MyComponent);
</script>
<my-component>
<p>This is light DOM content. It's projected into the shadow DOM.</p>
<p>WRS renders this content as well as the shadow DOM content.</p>
</my-component>
轉譯完成後,Google 可能將下列內容編入索引:
<my-component>
Hello World, this is shadow DOM content. Here comes the light DOM:
<p>This is light DOM content. It's projected into the shadow DOM<p>
<p>WRS renders this content as well as the shadow DOM content.</p>
</my-component>
修正圖片與延遲載入的內容
載入圖片可能占用大量頻寬並拖慢效能。建議延遲載入,在使用者即將看到圖片時再載入。為符合搜尋服務需求,請遵循 Google 的延遲載入指南。
除非另有說明,本頁內容依 Creative Commons Attribution 4.0 License 授權;程式碼範例依 Apache 2.0 License 授權。詳見 Google Developers 網站政策。原文最後更新:2026-03-05 UTC。
常見問題
- Google 處理 JavaScript 網頁分哪幾個階段?檢索、轉譯與建立索引;網頁會先進入檢索與轉譯佇列,等待期間沒有即時狀態訊號。
- SPA 為什麼不該用 hash 路由?Google 主要依含 href 的 <a> 連結發現網址;hash 片段難以正確解析,應改用 History API 與可檢索的 path URL。
- 用 JavaScript 事後拿掉 noindex 有效嗎?通常不可靠;若初始 HTML 已有 noindex,Google 可能略過轉譯與 JS 執行。要索引就不要在初始 HTML 放 noindex。
- soft 404 在 SPA 怎麼處理?可 JavaScript 重新導向到伺服器回傳 404 的網址,或在錯誤狀態頁動態加入 noindex meta。
- 如何確認 Google 看見的內容?使用 Search Console 網址檢查工具或複合式搜尋結果測試,查看轉譯後 HTML 是否含主要內容與結構化資料。
編輯解讀
一句話重點:JavaScript SEO 的核心是讓 Google 在轉譯後 HTML 看見與使用者相同的可索引內容、可檢索連結與正確 HTTP/meta 訊號。
這對你的網站意味著什麼:
- 流量或索引異常時,先用網址檢查看「Google 所見 HTML」,再改文案或連結策略。
- SPA 用 History API 與真實 path URL;缺貨或不存在資源用伺服器 404 或明確 noindex,避免 soft 404。
- canonical、robots meta 與 title/description 盡量在初始 HTML 或 SSR 輸出,少依賴事後 JS 覆寫。
- 靜態資源用指紋檔名,避免 Googlebot 快取到舊版 JS 導致轉譯錯誤。
- Web Components 與延遲載入圖片須在轉譯結果中可見,並用官方測試工具驗證。
適用與不適用的情境:適合 React/Vue/Angular 等 CSR 或混合渲染站點的技術 SEO 自查。不適合當成「Google 一定執行全部 JS」的保證——佇列與資源仍可能造成延遲。
需要注意的限制:本文為 Google 官方文件翻譯;Bing 與其他 AI 爬蟲的 JS 能力可能不同。原文更新日期 2026-03-05。
延伸閱讀:
可複用規則2 條
從本文提煉,可供人工對照或 AI 分析站點時作為依據。全部規則
JavaScript 站索引問題先比對 Search Console 網址檢查的「Google 所見 HTML」與使用者瀏覽器渲染結果;正文、title、canonical 與可檢索連結須在轉譯後 HTML 出現,再改內容或外鏈策略。
- 轉譯後 HTML 是否含主要正文
- href 是否為可解析的 path URL
單頁應用程式用 History API 與真實 path URL 導覽,避免 hash 路由;不存在或缺貨資源以伺服器 404 或明確 noindex 處理,勿對所有狀態回 200 造成 soft 404。
- 錯誤狀態是否回 meaningful HTTP 或 noindex
- 內部連結是否為 <a href>
名詞與資料來源
- Chromium 與 JavaScript 執行 — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/javascript/javascript-seo-basics(檢索日期:2026-10-12)
- robots.txt — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/robots/robots_txt(檢索日期:2026-10-12)
- 可檢索連結 — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/links-crawlable(檢索日期:2026-10-12)
- nofollow — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/qualify-outbound-links(檢索日期:2026-10-12)
- robots meta 與 X-Robots-Tag — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag(檢索日期:2026-10-12)
- title 與 description — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/appearance/snippet(檢索日期:2026-10-12)
- rel=canonical — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls(檢索日期:2026-10-12)
- HTTP 狀態碼 — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/http-network-errors(檢索日期:2026-10-12)
- href 連結 — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/links-crawlable(檢索日期:2026-10-12)
- robots meta 規格 — Google Search Central/web.dev:〈相關文件〉 https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag(檢索日期:2026-10-12)
- 長效快取 — Google Search Central/web.dev:〈相關文件〉 https://web.dev/uses-long-cache-ttl/(檢索日期:2026-10-12)
8 Comments
Comments are closed.