I have a weird thing in a Nuxt project whereby an asyncData
callback is first fired client-side, when the page is first visited (by clicking a nuxt-link
link), and then on server-side when the page is refreshed or visited manually (i.e. via a URL).
This is obviously not desirable; the request should be sent server-side only - and indeed will fail on the client due to CORS.
Here's my Nuxt config:
export default {
target: 'server',
ssr: true,
head: {
meta: [
{charset: 'utf-8'},
{name: 'viewport', content: 'width=device-width, initial-scale=1'}
],
titleTemplate: 'Rescue Partners%s'
},
css: [
'~/assets/css/buefy-init.scss',
'~/assets/css/main.scss'
],
components: true,
buildModules: [
'@nuxtjs/style-resources',
'@nuxtjs/google-fonts'
],
modules: [
['nuxt-buefy', {css: false}],
],
build: {},
//style resources (global SASS vars)
styleResources: {
scss: ['~/assets/css/project-vars.scss']
},
//Google fonts
googleFonts: {
families: {
'M PLUS Rounded 1c': [700]
}
}
}
And here's the asyncData
callback in the page in question:
//async data...
async asyncData(ctx) {
let merge = {};
//...groups (for filter)
const at = ctx.route.params.animalType;
let bGroups = await apiReq.call(ctx, 'cache', 'breedGroups', {
at: at.replace(/s$/, '')
});
if (bGroups == null) return;
merge.filters = ctx.store.getters.getFilters(at, bGroups);
return merge;
},
(apiReq
is a util which fires a fetch()
request.)