title.js 852 Bytes
import {
  SET_TITLE
} from 'store/yoho/types';

const getTitle = vue => {
  const {title} = vue.$options;

  if (title) {
    return typeof title === 'function' ? title.call(vue) : title;
  }
};
const serverTitleMixin = {
  created() {
    const title = getTitle(this);

    if (title) {
      this.$ssrContext.title = title;
      this.$store.commit(SET_TITLE, {title});
    }
  }
};

const clientTitleSet = (vue) => {
  const {title} = vue.$options;

  if (title) {
    if (typeof title === 'function') {
      title.call(vue, t => {
        t && (document.title = t);
      });
    } else {
      document.title = title;
    }
  }
};

const clientTitleMixin = {
  activated() {
    clientTitleSet(this);
  },
  mounted() {
    clientTitleSet(this);
  }
};

export default process.env.VUE_ENV === 'server' ?
  serverTitleMixin :
  clientTitleMixin;