Crabar Crabar - 3 months ago 26
Ajax Question

Ember app and google crawler

I'm trying to make my ember application crawlable. As I know Google supports JS, CSS and AJAX now (from october 2015). But when I test my site by "Fetch as Google" I get empty page with background: https://gyazo.com/2b28487ac1a25e11e2e87888779e3f2a

In real of course I have content and page looks completely different: https://gyazo.com/009a5a9719f80aef70fc22bc3d777cba

What am I doing wrong?

Answer

After a day of investigation I found two problems that prevented crawling.

1.Input validation component.

import Ember from 'ember';

const {
  computed,
  observer,
  defineProperty,
  run
} = Ember;

export default Ember.Component.extend({
  classNames: ['form-group', 'has-feedback', 'validated-input'],
  classNameBindings: ['isValid:has-success', 'showErrorClass:has-error'],
  isValid: false,
  model: null,
  value: null,
  rawInputValue: null,
  type: 'text',
  valuePath: '',
  placeholder: '',
  attributeValidation: null,
  isTyping: false,

  didValidate: computed.oneWay('targetObject.didValidate'),

  showErrorClass: computed('isTyping', 'showMessage', 'hasContent', 'attributeValidation', function() {
    return this.get('attributeValidation') && !this.get('isTyping') && this.get('showMessage');
  }),

  hasContent: computed.notEmpty('rawInputValue'),

  isValid: computed.and('hasContent', 'attributeValidation.isValid'),

  isInvalid: computed.oneWay('attributeValidation.isInvalid'),

  inputValueChange: observer('rawInputValue', function() {
    this.set('isTyping', true);
    run.debounce(this, this.setValue, 500, false);
  }),

  showMessage: computed('attributeValidation.isDirty', 'isInvalid', 'didValidate', function() {
    return (this.get('attributeValidation.isDirty') || this.get('didValidate')) && this.get('isInvalid');
  }),

  setValue() {
    this.set('value', this.get('rawInputValue'));
    this.set('isTyping', false);
  },

  init() {
    this._super(...arguments);
    var valuePath = this.get('valuePath');
    defineProperty(this, 'attributeValidation', computed.oneWay(`model.validations.attrs.${valuePath}`));
    this.set('rawInputValue', this.get(`model.${valuePath}`));
    defineProperty(this, 'value', computed.alias(`model.${valuePath}`));
  }
});

I replaced this component with newer.

2.This piece of code (I wrote it for performance optimization):

ingredients: function() {
    var self = this;
    return DS.PromiseArray.create({
      promise: new Promise((resolve, reject) => {
        let loadedIngredients = self.store.peekAll('ingredient');
        if (loadedIngredients && loadedIngredients.get('length') > 0) {
          let mappedIngredients = self.get('recipe').get('ingredientsWithQuantities').map(function(item) {
            return {
              name: self.store.peekRecord('ingredient', item.ingredientId).get('name'),
              quantity: item.quantity
            };
          });
          resolve(mappedIngredients);
        } else {
          this.store.findAll('ingredient').then(function() {
            let mappedIngredients = self.get('recipe').get('ingredientsWithQuantities').map(function(item) {
              return {
                name: self.store.peekRecord('ingredient', item.ingredientId).get('name'),
                quantity: item.quantity
              };
            });
            resolve(mappedIngredients);
          })
        }
      })
    });
  }.property('recipe.ingredientsWithQuantities')

I fixed these two thing and now google bot able to render my app.

Comments