Skip to content Skip to sidebar Skip to footer

Nuxt Dynamic Routing On Firebase

My Nuxt.js App has this structure: /pages/index.vue /pages/_slug/index.vue When user gets /{any_page}, it will use the path to build the page content: /pages/_slug/index.vue

Solution 1:

If using nuxt generate, then the explanation below should work. If using nuxt build, look at this repo, it is a template i wrote for nuxt + firebase as ssr.

Generate says it skips over dynamic routes, unless you explicitly specify them, hence 404 errors. In your Nuxt config file, you want to specify those routes. Here is an example of fetching from the database during generation. Note that in my example below, the doc id is the slug for the page.

nuxt.config.js

generate: {
    asyncroutes() {
      const { db } = require('./services/fireInit');
      const qs = await db.collection('recipes').get();
      return qs.docs.map(x =>`/recipes/${x.id}`);
    }
  },

The downside to this approach is as you add new items via a CMS, you will need to rebuild/deploy your site each time if you want to be able to use these new links without a 404. CMS without redeploy does work if you navigate to new content 'organically'...404 only occurs on a page refresh or on jumping directly to the page from a hyperlink.

Post a Comment for "Nuxt Dynamic Routing On Firebase"