How do search engines deal with AngularJS applications?

后端 未结 15 1220
予麋鹿
予麋鹿 2020-11-22 06:35

I see two issues with AngularJS application regarding search engines and SEO:

1) What happens with custom tags? Do search engines ignore the whole content within tho

相关标签:
15条回答
  • 2020-11-22 07:32

    I have found an elegant solution that would cover most of your bases. I wrote about it initially here and answered another similar StackOverflow question here which references it.

    FYI this solution also includes hardcoded fallback tags in case Javascript isn't picked up by the crawler. I haven't explicitly outlined it, but it is worth mentioning that you should be activating HTML5 mode for proper URL support.

    Also note: these aren't the complete files, just the important parts of those that are relevant. If you need help writing the boilerplate for directives, services, etc. that can be found elsewhere. Anyway, here goes...

    app.js

    This is where you provide the custom metadata for each of your routes (title, description, etc.)

    $routeProvider
       .when('/', {
           templateUrl: 'views/homepage.html',
           controller: 'HomepageCtrl',
           metadata: {
               title: 'The Base Page Title',
               description: 'The Base Page Description' }
       })
       .when('/about', {
           templateUrl: 'views/about.html',
           controller: 'AboutCtrl',
           metadata: {
               title: 'The About Page Title',
               description: 'The About Page Description' }
       })
    

    metadata-service.js (service)

    Sets the custom metadata options or use defaults as fallbacks.

    var self = this;
    
    // Set custom options or use provided fallback (default) options
    self.loadMetadata = function(metadata) {
      self.title = document.title = metadata.title || 'Fallback Title';
      self.description = metadata.description || 'Fallback Description';
      self.url = metadata.url || $location.absUrl();
      self.image = metadata.image || 'fallbackimage.jpg';
      self.ogpType = metadata.ogpType || 'website';
      self.twitterCard = metadata.twitterCard || 'summary_large_image';
      self.twitterSite = metadata.twitterSite || '@fallback_handle';
    };
    
    // Route change handler, sets the route's defined metadata
    $rootScope.$on('$routeChangeSuccess', function (event, newRoute) {
      self.loadMetadata(newRoute.metadata);
    });
    

    metaproperty.js (directive)

    Packages the metadata service results for the view.

    return {
      restrict: 'A',
      scope: {
        metaproperty: '@'
      },
      link: function postLink(scope, element, attrs) {
        scope.default = element.attr('content');
        scope.metadata = metadataService;
    
        // Watch for metadata changes and set content
        scope.$watch('metadata', function (newVal, oldVal) {
          setContent(newVal);
        }, true);
    
        // Set the content attribute with new metadataService value or back to the default
        function setContent(metadata) {
          var content = metadata[scope.metaproperty] || scope.default;
          element.attr('content', content);
        }
    
        setContent(scope.metadata);
      }
    };
    

    index.html

    Complete with the hardcoded fallback tags mentioned earlier, for crawlers that can't pick up any Javascript.

    <head>
      <title>Fallback Title</title>
      <meta name="description" metaproperty="description" content="Fallback Description">
    
      <!-- Open Graph Protocol Tags -->
      <meta property="og:url" content="fallbackurl.com" metaproperty="url">
      <meta property="og:title" content="Fallback Title" metaproperty="title">
      <meta property="og:description" content="Fallback Description" metaproperty="description">
      <meta property="og:type" content="website" metaproperty="ogpType">
      <meta property="og:image" content="fallbackimage.jpg" metaproperty="image">
    
      <!-- Twitter Card Tags -->
      <meta name="twitter:card" content="summary_large_image" metaproperty="twitterCard">
      <meta name="twitter:title" content="Fallback Title" metaproperty="title">
      <meta name="twitter:description" content="Fallback Description" metaproperty="description">
      <meta name="twitter:site" content="@fallback_handle" metaproperty="twitterSite">
      <meta name="twitter:image:src" content="fallbackimage.jpg" metaproperty="image">
    </head>
    

    This should help dramatically with most search engine use cases. If you want fully dynamic rendering for social network crawlers (which are iffy on Javascript support), you'll still have to use one of the pre-rendering services mentioned in some of the other answers.

    Hope this helps!

    0 讨论(0)
  • 2020-11-22 07:35

    Things have changed quite a bit since this question was asked. There are now options to let Google index your AngularJS site. The easiest option I found was to use http://prerender.io free service that will generate the crwalable pages for you and serve that to the search engines. It is supported on almost all server side web platforms. I have recently started using them and the support is excellent too.

    I do not have any affiliation with them, this is coming from a happy user.

    0 讨论(0)
  • 2020-11-22 07:37

    With Angular Universal, you can generate landing pages for the app that look like the complete app and then load your Angular app behind it.
    Angular Universal generates pure HTML means no-javascript pages in server-side and serve them to users without delaying. So you can deal with any crawler, bot and user (who already have low cpu and network speed).Then you can redirect them by links/buttons to your actual angular app that already loaded behind it. This solution is recommended by official site. -More info about SEO and Angular Universal-

    0 讨论(0)
提交回复
热议问题