Hello. I use Vuex and Axios.
The code works. But, the server-side rendering, does not show the component, if I use ajax, in the source code. I want the search engines to see the content in the source code that I get with ajax.
Static codes look at the page source. But, I get with ajax data, does not show in the browser source code.
? Code:
<script type="text/javascript" defer>window.__NUXT__={"data":[null],"error":null,"state":{"topics":[],"categories":[]},"serverRendered":true}</script>
But the state is actually(not emty). And the data is coming.
view - not problem <img width="1176" alt="ekran resmi 2017-02-09 16 43 53" src="https://cloud.githubusercontent.com/assets/1280469/22785844/a1888d6a-eede-11e6-83c5-e1107131b280.png">
vuex store - not problem <img width="1679" alt="ekran resmi 2017-02-09 16 43 44" src="https://cloud.githubusercontent.com/assets/1280469/22785842/a1854ed4-eede-11e6-8052-86ab7654650e.png">
initial state ? I think the problem <img width="1054" alt="ekran resmi 2017-02-09 16 43 28" src="https://cloud.githubusercontent.com/assets/1280469/22785843/a1878028-eede-11e6-996a-2e39e4c3838e.png">
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as getters from './getters'
Vue.use(Vuex)
const defaultState = {
topics: [],
categories: []
}
const inBrowser = typeof window !== 'undefined'
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__NUXT__) || defaultState
const mutations = {
TOPICS_LIST: (state, topics) => {
state.topics = topics
},
CATEGORIES_LIST: (state, categories) => {
state.categories = categories
}
}
export default new Vuex.Store({
state,
actions,
mutations,
getters
})
store/action.js
import request from 'axios'
request.defaults.baseURL = 'http://apiurl.com'
export const getTopics = ({ commit, state }) => {
return request.get('movies').then((response) => {
commit('TOPICS_LIST', response.data.data)
}).catch((error) => {
console.log(error)
})
}
export const getCategories = ({ commit, state }) => {
return request.get('genres').then((response) => {
commit('CATEGORIES_LIST', response.data.data)
}).catch((error) => {
console.log(error)
})
}
store/getters.js
export const getTopics = state => state.topics
export const getCategories = state => state.categories
component/itemList :
<script>
import { mapGetters } from 'vuex'
const fetchInitialData = store => {
return store.dispatch(`getTopics`)
}
export default {
prefetch: fetchInitialData,
computed: {
...mapGetters({
topics: 'getTopics'
})
},
mounted () {
fetchInitialData(this.$store)
}
}
</script>
pages/index :
<template>
<section class="container">
<itemList></itemList>
<nuxt-link class="button" to="/about">
About page
</nuxt-link>
</section>
</template>
<script>
import itemList from '~components/itemList.vue'
export default {
components: {
itemList
}
}
</script>
<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c194">#c194</a>)</em></sub></div>