我的问题是 Google Search Console 中的抓取无法在 React 中找到子路由。

网址是https://huynhsamha.github.io/crypto ,爬虫可以获取并渲染主页(路由/)和静态文件,例如/robots.txt/favicon.ico ,但找不到React渲染的子路由(SPA,使用Redux),例如/algorithm/sha256。例如,https://huynhsamha.github.io/crypto/algorithm/sha256爬虫无法找到,但可以访问。

这是我在 Google Search Console 中尝试过的屏幕截图。

谁能解释一下为什么以及如何解决我的问题?我正在使用 react-router-domreact-redux 我在 github 上的存储库 here

编辑 1

我也尝试过答案 https://stackoverflow.com/a/53966338/8828489在这个问题上,但不起作用。我已在 index.html ( https://github.com/huynhsamha/crypto/blob/gh-pages/index.html ) 中添加了脚本,但搜索控制台仍然找不到,因此它也无法在屏幕上呈现任何错误。

编辑2

我也尝试过答案https://stackoverflow.com/a/54040745/8828489https://stackoverflow.com/a/54048119/8828489在这个问题上,但不起作用。我已经创建了 404.html 文件并按照答案的指示添加了脚本,但它也不起作用。

编辑3

我也尝试过答案 https://stackoverflow.com/a/54044148/8828489在这个问题中,通过创建一个简单的 sitemap.xml,googlebot 可以找到该文件并发现我在站点地图中定义的所有网址。但它也无法获取和呈现提到的 URL。

请您参考如下方法:

我发现当我打开https://huynhsamha.github.io/crypto/algorithm/sha256时,我实际上收到了 404 作为 response 。我认为您使用 404.html 在 GitHub 上托管 SPA 的解决方法是这里的问题。虽然我们人类看到您的应用程序在我们的浏览器上正确运行,但 googlebot 并不关心,只是查看响应代码并发现它已收到 404 。您需要一种不涉及使用 404.html 的不同解决方法直接作为您的应用程序的入口点。

尝试以下 this workaround by rafrex相反,它将浏览器重定向到 index.html使用404.html在保留原始路线的同时,它声称 googlebot 将其注册为 301而不是404 ,对于您的情况,这意味着将以下这些更改添加到您的站点,请注意 <!-- ------Single Page Apps GitHub Pages Workaround------ --> 下面的脚本:

<!-- 404.html --> 
 
<!DOCTYPE html> 
<html> 
  <head> 
    <meta charset="utf-8"> 
    <title>Cryptography</title> 
 
    <!-- ------Single Page Apps GitHub Pages Workaround------ --> 
    <script type="text/javascript"> 
      // Single Page Apps for GitHub Pages 
      // https://github.com/rafrex/spa-github-pages 
      // Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License 
      // ---------------------------------------------------------------------- 
      // This script takes the current url and converts the path and query 
      // string into just a query string, and then redirects the browser 
      // to the new url with only a query string and hash fragment, 
      // e.g. http://www.foo.tld/one/two?a=b&c=d#qwe, becomes 
      // http://www.foo.tld/?p=/one/two&q=a=b~and~c=d#qwe 
      // Note: this 404.html file must be at least 512 bytes for it to work 
      // with Internet Explorer (it is currently > 512 bytes) 
      // If you're creating a Project Pages site and NOT using a custom domain, 
      // then set segmentCount to 1 (enterprise users may need to set it to > 1). 
      // This way the code will only replace the route part of the path, and not 
      // the real directory in which the app resides, for example: 
      // https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes 
      // https://username.github.io/repo-name/?p=/one/two&q=a=b~and~c=d#qwe 
      // Otherwise, leave segmentCount as 0. 
      var segmentCount = 1; 
      var l = window.location; 
      l.replace( 
        l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') + 
        l.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=/' + 
        l.pathname.slice(1).split('/').slice(segmentCount).join('/').replace(/&/g, '~and~') + 
        (l.search ? '&q=' + l.search.slice(1).replace(/&/g, '~and~') : '') + 
        l.hash 
      ); 
    </script> 
  </head> 
  <body> 
  </body> 
</html> 
<!-- index.html --> 
 
<!DOCTYPE html> 
<html lang="en"> 
 
<head> 
  <meta charset="utf-8"> 
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
  <meta name="theme-color" content="#000000"> 
  <meta name="description" content="Cryptography Algorithms: Secure Hash Algorithm (sha256, sha512, ...), Message Digest Algorithm (md5, ripemd160), HMAC-SHA, HMAC-MD, pbkdf2, Advanced Encryption Standard (AES), Triple Data Encryption Standard, (TripleDES, DES), RC4, Rabbit, ..."> 
  <meta name="keywords" content="crypto, algorithms, secure hash, sha, sha512, sha256, message digest, md5, hmac-sha, aes, des, tripledes, pbkdf2, rc4, rabbit, encryption, descryption"> 
  <meta name="author" content="huynhsamha"> 
 
  <!-- Open Graph --> 
  <meta property="fb:app_id" content="440168923127908"> 
  <meta property="og:url" content="https://huynhsamha.github.io/crypto"> 
  <meta property="og:title" content="Cryptography Algorithms"> 
  <meta property="og:description" content="Cryptography Algorithms: Secure Hash Algorithm (sha256, sha512, ...), Message Digest Algorithm (md5, ripemd160), HMAC-SHA, HMAC-MD, pbkdf2, Advanced Encryption Standard (AES), Triple Data Encryption Standard, (TripleDES, DES), RC4, Rabbit, ..."> 
  <meta property="og:type" content="website"> 
  <meta property="og:image" content="%PUBLIC_URL%/img/main.jpeg"> 
  <meta property="og:site_name" content="Cryptography"> 
  <meta property="og:locale" content="vi_VN"> 
 
  <!-- Twitter Card --> 
  <meta name="twitter:card" content="summary"> 
  <meta name="twitter:site" content="@huynhsamha"> 
  <meta name="twitter:creator" content="@huynhsamha"> 
  <meta name="twitter:url" content="https://huynhsamha.github.io/crypto"> 
  <meta name="twitter:title" content="Cryptography Algorithms"> 
  <meta name="twitter:description" content="Cryptography Algorithms: Secure Hash Algorithm (sha256, sha512, ...), Message Digest Algorithm (md5, ripemd160), HMAC-SHA, HMAC-MD, pbkdf2, Advanced Encryption Standard (AES), Triple Data Encryption Standard, (TripleDES, DES), RC4, Rabbit, ..."> 
  <meta name="twitter:image:src" content="%PUBLIC_URL%/img/main.jpeg"> 
 
  <!-- 
      manifest.json provides metadata used when your web app is added to the 
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/ 
    --> 
  <link rel="manifest" href="%PUBLIC_URL%/manifest.json"> 
  <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> 
  <link rel="author" href="//github.com/huynhsamha"> 
  <link rel="canonical" href="//huynhsamha.github.io/crypto"> 
  <!-- 
      Notice the use of %PUBLIC_URL% in the tags above. 
      It will be replaced with the URL of the `public` folder during the build. 
      Only files inside the `public` folder can be referenced from the HTML. 
      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will 
      work correctly both with client-side routing and a non-root public URL. 
      Learn how to configure a non-root public URL by running `npm run build`. 
    --> 
  <link href="//fonts.googleapis.com/css?family=Open+Sans:400,600,700&amp;subset=vietnamese" rel="stylesheet"> 
  <link rel="stylesheet" href="%PUBLIC_URL%/css/bootstrap.min.css"> 
  <link rel="stylesheet" href="%PUBLIC_URL%/lib/font-awesome/css/font-awesome.min.css"> 
 
  <!-- ------Single Page Apps GitHub Pages Workaround------ --> 
  <script type="text/javascript"> 
    // Single Page Apps for GitHub Pages 
    // https://github.com/rafrex/spa-github-pages 
    // Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License 
    // ---------------------------------------------------------------------- 
    // This script checks to see if a redirect is present in the query string 
    // and converts it back into the correct url and adds it to the 
    // browser's history using window.history.replaceState(...), 
    // which won't cause the browser to attempt to load the new url. 
    // When the single page app is loaded further down in this file, 
    // the correct url will be waiting in the browser's history for 
    // the single page app to route accordingly. 
    (function(l) { 
      if (l.search) { 
        var q = {}; 
        l.search.slice(1).split('&').forEach(function(v) { 
          var a = v.split('='); 
          q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&'); 
        }); 
        if (q.p !== undefined) { 
          window.history.replaceState(null, null, 
            l.pathname.slice(0, -1) + (q.p || '') + 
            (q.q ? ('?' + q.q) : '') + 
            l.hash 
          ); 
        } 
      } 
    }(window.location)) 
   </script> 
 
 
  <title>Cryptography</title> 
 
</head> 
 
<body> 
  <noscript> 
    You need to enable JavaScript to run this app. 
  </noscript> 
 
  <div id="root"></div> 
 
  <!-- 
      This HTML file is a template. 
      If you open it directly in the browser, you will see an empty page. 
      You can add webfonts, meta tags, or analytics to this file. 
      The build step will place the bundled scripts into the <body> tag. 
      To begin the development, run `npm start` or `yarn start`. 
      To create a production bundle, use `npm run build` or `yarn build`. 
    --> 
 
  <script src="%PUBLIC_URL%/js/jquery-3.3.1.slim.min.js" type="text/javascript"></script> 
  <script src="%PUBLIC_URL%/js/popper.min.js" type="text/javascript"></script> 
  <script src="%PUBLIC_URL%/js/bootstrap.min.js" type="text/javascript"></script> 
 
  <!-- Google Adsense --> 
  <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 
 
</body> 
 
</html> 

有关 GitHub 对单页应用程序支持的更多信息和讨论 here .


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!