Poradnik: JavaScript Redirects

co a JavaScript redirect jest, how Google's render pipeline treats it differently z a serwer-side 301, gdy it's an acceptable ostatni resort, i how to implement i detect one — plus gdzie meta refresh i the History API fit.

Opublikowano po raz pierwszy: 27 cze 2026 · Ostatnia aktualizacja: 3 sie 2026 · Advanced
Języki
1 sygnał dowodowy na tej stronie

A JavaScript redirect wysyła użytkownicy i crawlers to a new URL z client-side code (window.location.zastępować() lub .href). It's the least niezawodny redirect type ponieważ Google tylko sees it po renderowanie — który może być delayed, lub fail entirely, z no fixed timeline either way. Google's official order of preference jest serwer-side (301/302/307/308) → meta refresh → JavaScript, i jego docs say plainly: tylko używać JS redirects if you może't robić the other two. Once Google successfully interprets one, the target becomes a canonicalization signal — ale że's nie a proven guarantee of identical PageRank lub ranking outcomes to a 301, so treat it as a ostatni resort zamiast a like-dla-like swap. używać them on constrained platformy z no serwer access, dla SPA błąd strony że point to a rzeczywisty 404, i little else. If you musi używać one, używać window.location.zastępować() in the <head>, drop the źródło URL z twój mapa witryny, i point linki wewnętrzne at the final destination. Meta refresh jest a oddzielny HTML-level redirect, i history.pushState()/replaceState() aren't redirects at wszystkie.

TL;DR — A JavaScript redirect jest a client-side redirect (window.location.replace(), .href, .assign()) że Google tylko procesy po renderowanie — phase three of crawl → render → index. A serwer-side 301 jest seen at crawl time; a JS redirect waits on the render queue, i Google gives no fixed timeline dla że wait — it może być quick lub it może take a long podczas gdy, i renderowanie może fail outright. Google’s udokumentowany preference order jest serwer-side → meta refresh → JavaScript, i the docs say to używać JS redirects tylko gdy you może’t robić the other two. Once Google successfully interprets one, the target becomes a permanent canonicalization signal — even Google używany them on ich own blog gdy nothing else worked — ale że’s nie udokumentowany proof of identical PageRank lub ranking outcomes to a 301, so they’re a ostatni resort, nie a spam signal. The legitimate używa są constrained platformy z no serwer config i SPA błąd strony że point at a rzeczywisty 404. Implement z window.location.replace() in the <head>, drop the źródło z twój mapa witryny, repoint linki wewnętrzne, i confirm Googlebot może fetch the JS. Meta refresh jest HTML-level (0s = permanent, dowolny opóźnienie = temporary), i history.pushState()/replaceState() aren’t redirects at wszystkie.

co counts as a JavaScript redirect

Script navigation depends on renderowanie i execution, so it jest nie protokół-equivalent to an HTTP redirect. Evidence for this claim Primary standard or official documentation supporting the adjacent article claim. Scope: Protocol semantics and Search behavior are kept separate; no indexing, ranking, or migration-timing guarantee is inferred. Confidence: high · Verified: Google: JavaScript redirects Processing jest possible, nie an dokładny-timing lub indeksowanie guarantee. Evidence for this claim Primary standard or official documentation supporting the adjacent article claim. Scope: Protocol semantics and Search behavior are kept separate; no indexing, ranking, or migration-timing guarantee is inferred. Confidence: high · Verified: Google: Redirects and Search

A JavaScript redirect navigates the przeglądarka to a new URL z client-side code. The common metody, i how they differ:

  • window.location.replace("url") — navigates i removes the original URL z session history. ten jest the one to używać: the back button skips the redirect źródło zamiast bouncing the użytkownik straight back.
  • window.location.href = "url" — navigates ale zachowuje the original in history, so the back button zwroty to the redirecting strona (i może create a loop). document.location.href i window.location.assign("url") behave the same way.
  • history.pushState() / history.replaceState()nie redirects. They rewrite the address bar bez dowolny navigation lub HTTP signal, so crawlers don’t treat them as redirects. SPAs używać them dla in-app URL changes; they need rzeczywisty <a href> links (lub rzeczywisty navigations) to być crawlable.

The dividing wiersz że matters: .replace(), .href, i .assign() wszystkie trigger a rzeczywisty document navigation (the difference między them jest tylko co happens to session history), podczas gdy pushState()/replaceState() nigdy navigate at wszystkie — they touch history state i the address bar i nothing else. None of the four jest an HTTP 301; “redirect” here jest shorthand dla client-side navigation, nie a status code.

Meta refresh (<meta http-equiv="refresh" content="0;url=...">) jest często lumped in z JS redirects, ale it’s an HTML directive parsed przed JavaScript runs — a oddzielny, więcej niezawodny kategoria, covered below.

How Google procesy a JavaScript redirect

ten jest the crux. Google’s pipeline runs in stages, i a JS redirect i a serwer-side redirect get caught at różny ones:

  1. Crawl — Googlebot fetches the URL i reads the raw HTML. A serwer-side 301/302/307/308 jest seen right here.
  2. Render queue — strony że zwracać 200 wait to być wyrenderowany. Google’s docs note the strona “may stay on this queue for a few seconds, but it can take longer than that.” Google doesn’t publikować a fixed service-level timeline beyond że, so treat the wait as unpredictable — it może być quick, lub it może drag — rather than assuming dowolny specific liczba of days lub weeks.
  3. Render + index — headless Chromium runs the JavaScript. ten jest the pierwszy moment a JS redirect exists as far as Google jest concerned.

The same idea I make on Crawling i in SEO JavaScript applies here: renderowanie jest a oddzielny krok z pobieranie, i anything że depends on it inherits że opóźnienie i że risk.

i the risk jest rzeczywisty. Google: “While Google attempts to render every URL Googlebot crawled, rendering may fail for various reasons. This means that if you set a JavaScript redirect, Google might never see it if rendering of the content failed.” (Google Search Central) podczas the window przed the redirect jest processed — i forever, if renderowanie fails — Google może zachowaj empty źródło strona in jego index.

Google’s official preference order

The redirects documentation lays out a hierarchy z najbardziej to least niezawodny:

  1. serwer-side redirects — 301/308 (permanent), 302/307 (temporary). Best dla everything: seen at crawl time, unambiguous.
  2. Meta refresh — HTML-level. A 0-second meta refresh jest treated as a permanent redirect (like a 301); dowolny delayed meta refresh jest treated as temporary.
  3. JavaScript redirects — ostatni resort.

Google’s words: “Only use JavaScript redirects if you can’t do server-side or meta refresh redirects.” Permanent redirects pass the canonical signal to the target; temporary ones zachowaj original in wyniki. (How że interacts z canonical selection jest ponad on Canonicalization.)

Google’s own redirects documentation listy JavaScript location navigation among jego permanent redirection metody, i says the target becomes the canonicalization signal once Google ma interpreted it — so the flat “JS redirects don’t pass PageRank” twierdzenie jest fałszywy. co the documentation doesn’t establish jest że the outcome jest identical, immediate, lub as niezawodny as a serwer-side 301 — it describes the canonical signal, nie a guarantee of matching PageRank flow, ranking, lub timing. The honest framing: a 301 passes the signal at crawl time z near-certainty; a JS redirect passes it tylko if i gdy renderowanie succeeds, i Google doesn’t promise the wynik będzie match a 301’s outcome one-dla-one. że gap — nie lost equity — jest the rzeczywisty cost of choosing JavaScript.

ten jest również why JS redirects aren’t a penalty trigger on ich own. They tylko become a spam problem gdy they’re używany dla cloaking — showing crawlers one strona i redirecting użytkownicy to something różny, lub sending mobilny użytkownicy to an unrelated domain. Google’s sneaky redirects polityka jest o że intent, nie the technique.

gdy a JavaScript redirect jest the right narzędzie

There są legitimate cases:

  • Constrained platformy. niektóre shared hosting, CDN, lub CMS setups give you no access to serwer-side redirect reguły. A JS redirect jest a prawidłowy fallback — i notably, Google używany JS redirects on ich own Webmaster blog ponieważ, as Gary Illyes put it, “that was the only thing we could use for 1:1 redirects, and it works on Google” (OnCrawl).
  • SPA błąd handling. Google explicitly endorses ten: “Use a JavaScript redirect to a URL for which the server responds with a 404 HTTP status code.” (Google Search Central) A single-strona app że resolves a bad route może redirect to a rzeczywisty 404 endpoint so Google procesy the błąd correctly zamiast indeksowanie a soft 404.

dla permanent URL migrations, ten jest nie the narzędzie — używać a 301. I make the same point in migracje witryn: JavaScript redirects są a ostatni resort, i Google może nigdy see them.

statyczny witryna generators: the Hugo aliases trap

A common surprise: Hugo’s aliases: frontmatter ma historically generated meta refresh HTML strony, nie serwer-side 301s — i other statyczny generators mieć done similar things by domyślny. Generator defaults change między versions, so sprawdzenie twój current deployed version’s rzeczywisty output zamiast assuming; if aliases: isn’t giving you 301s, you need platforma-level redirect reguły (Netlify _redirects, Cloudflare Workers, Vercel vercel.json) plus (on Hugo) disableAliases: true. I cover ten in detail in Hugo SEO.

implementacja dobre praktyki

If a JavaScript redirect jest genuinely twój tylko option:

  • używać window.location.replace(), nie .href. As wyszukiwarka Journal puts it, JS redirects “typically use window.location.replace() function rather than window.location.href to avoid UX redirect loops” (SEJ).
  • Put it in the <head>, nie the <body>. przeglądarki parse HTML sequentially i run scripts as they hit them, so “position JavaScript redirects in the <head> tag rather than <body> to minimize delay” (OnCrawl).
  • Redirect to the final destination in one hop. A JS redirect to a strona że itself 301s elsewhere makes a chain; chains waste budżet indeksowania i może surface in GSC as a redirect błąd.
  • usuń źródło URL z twój mapa witryny XML. Sitemaps powinien lista canonical, indeksowalny URLs — nie redirecting ones.
  • Repoint linki wewnętrzne at the destination, so they don’t route przez the redirect at wszystkie.
  • upewnij się Googlebot może fetch the JS. If the redirect lives in an external script blocked by robots.txt, Google może’t render it i won’t see the redirect.

How to detect JavaScript redirects

They don’t announce themselves like a 301 in a header, so you mieć to render:

  • A crawler z JS renderowanie on. OnCrawl recommends crawling z “JavaScript rendering enabled (5-second timeout minimum)”; Screaming Frog i Ahrefs witryna Audit może oba render. bez renderowanie, a JS-redirecting strona just looks like a normal 200.
  • Chrome DevTools. The Network tab (z “Preserve log”) pokazuje the client-side navigation; the Redirect Path extension flags it too.
  • In Search Console, a successfully processed JS redirect pokazuje up poniżej strona z redirect — the same status as dowolny redirected URL, który jest normal dla non-canonical źródła. że label isn’t guaranteed on dowolny given sprawdzenie, though: it reflects whatever Google fetched, wyrenderowany, interpreted, i canonicalized at the sampled moment, so a URL może pokazywać a różny status (lub no redirect status yet) między sprawdzenia bez że będąc an błąd on twój end.

co I’d actually robić

serwer-side pierwszy, każdy time. Meta refresh (0-second) gdy you może edit HTML ale nie serwer config. JavaScript tylko gdy oba są off the tabela — i then z window.location.replace() in the <head>, a clean mapa witryny, i a sprawdzenie że the redirect actually renders dla Googlebot. dla anything permanent lub wysoki-wartość, the extra reliability of a 301 jest worth almost dowolny effort to obtain.

Add an expert note

Pin an expert quote

New person? Create their unclaimed profile at /admin/experts/ → Pin a quote first.