FBR
FBR

Reputation: 179

Nuxt local json file is not loading in deployment

I have a json file stored in static folder of a nuxt project.

{
  "volvo": {
     "brand": "Volvo"
     "model": "v60",
     "price": 36150,
   },
  "lexus": {
     "brand": "Lexus",
     "model": "is-250-c",
     "price": 36150,
  },
  ....
}

Imported this json file inside of a page pages/cars/_car.vue

<template>
  <div>
    <h1> {{ carInfo.brand }} </h1>
    <p>Model: {{ carInfo.model }} </p>
    <p>Price: {{ carInfo.price }} </p>
  </div>
</template>

<script>
 const carList = () => import('~/static/cars.json').then(m => m.default || m)

 export default {
   name: 'Car',
   async asyncData({params, redirect}) {
     const cars = await carList()
     if (!cars[params.car]) {
        redirect('/')
     }
     const carInfo = cars[params.car]
     return {
       carInfo,
       params.car
     }
   }
 }


</script>

This page renders without any error locally and I get no errors. http://localhost:3000/cars/volvo

But when it's deployed to Netlify, I get error on console that title is undefined. Even when I console logged carList data, I get nothing in console after deployment. It seems like that the json file is not being imported after nuxt generate.

nuxt.config.js

export default {
  // Target: https://go.nuxtjs.dev/config-target
  target: 'static',

  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'test_app',
    htmlAttrs: {
      lang: 'en',
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/eslint
    '@nuxtjs/eslint-module',
    // https://go.nuxtjs.dev/stylelint
    '@nuxtjs/stylelint-module',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/bootstrap
    'bootstrap-vue/nuxt',
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
  ],

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    // Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308
    baseURL: '/',
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {},
}

Upvotes: 1

Views: 593

Answers (1)

Avraham
Avraham

Reputation: 968

If you are using nuxt/vue v2 you should consider using Vuex to store enums.

Upvotes: 0

Related Questions