MCPcopy Create free account
hub / github.com/github/docs / getRedirect

Function getRedirect

components/ClientSideRedirectExceptions.tsx:33–61  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

31 .replace(`/${router.query.versionId || ''}`, '')
32
33 async function getRedirect() {
34 try {
35 const sp = new URLSearchParams()
36 sp.set('path', barePath)
37 sp.set('hash', hash.replace(/^#/, ''))
38
39 // call the anchor-redirect endpoint to get the redirect url
40 const response = await fetch(`/anchor-redirect?${sp.toString()}`, {
41 signal,
42 })
43
44 // the response status will always be 200 unless there
45 // was a problem with the fetch request. When the
46 // redirect doesn't exist the json response will be empty
47 if (response.ok) {
48 const { to } = await response.json()
49 if (to) {
50 // we want to redirect with the language and version in tact
51 // so we'll replace the full url's path and hash
52 const fromUrl = pathname + hash
53 const bareUrl = barePath + hash
54 const toUrl = fromUrl.replace(bareUrl, to)
55 router.replace(toUrl)
56 }
57 }
58 } catch (error) {
59 console.warn('Unable to fetch client-side redirect:', error)
60 }
61 }
62 getRedirect()
63
64 return () => controller.abort()

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected