Nuxt.js: How to exclude some routes when using nuxt generate

Created on 30 Jan 2018  路  7Comments  路  Source: nuxt/nuxt.js

current router

 --- about.vue
 --- index.vue

I just want to generate an about HTML file when using nuxt generate, how can I do that?

This question is available on Nuxt.js community (#c2362)

Most helpful comment

Do you want only a subset of your pages generated when you run nuxt generate?

I was wanting to do the same thing today. My solution was to use a custom module to leverage the internal plugin system (Note that in the latest releases these are now called hooks and have been re-worked a bit. They don't appear to be documented yet.)

I'm not sure if this is the best approach but it certainly seems to do the trick for now.

modules/custom-generate.js

module.exports = function () {
  this.nuxt.hook('generate:extendRoutes', async routes => {
    const whiteList = ['/about', '/login'];
    const routesToGenerate = routes.filter(page => whiteList.includes(page.route));
    routes.splice(0, routes.length, ...routesToGenerate);
  });
};

nuxt.config.js

modules: [
  '~modules/custom-generate.js'
],

All 7 comments

you just create a page named about.vue right there

Do you want only a subset of your pages generated when you run nuxt generate?

I was wanting to do the same thing today. My solution was to use a custom module to leverage the internal plugin system (Note that in the latest releases these are now called hooks and have been re-worked a bit. They don't appear to be documented yet.)

I'm not sure if this is the best approach but it certainly seems to do the trick for now.

modules/custom-generate.js

module.exports = function () {
  this.nuxt.hook('generate:extendRoutes', async routes => {
    const whiteList = ['/about', '/login'];
    const routesToGenerate = routes.filter(page => whiteList.includes(page.route));
    routes.splice(0, routes.length, ...routesToGenerate);
  });
};

nuxt.config.js

modules: [
  '~modules/custom-generate.js'
],

@qm3ster Good point. I think the only catch is that generate.routes only gives you control over dynamic routes. If you want to blacklist some static routes from generate, you still need the extendRoutes hook, or is there a better way?

I tried but it did not work

 generate: {
    routes: ["about"]
  },

How to setting generate.routes make nuxt disables route autogeneration ? Can you show the code?

Nuxt Version : 1.3.0

@dan-peterson @SzHeJason I was wrong, this has been changed a gorillion months ago.
The generate.routes are now merged with autodetected routes.
generate:extendRoutes seems like the only way now.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

For all googlers, this is possible via regex in generate.exclude. if the route URL matches the regex, the page will be excluded from generating but can still be served as "SPA route".

See https://github.com/nuxt/nuxt.js/pull/4754

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredreich picture jaredreich  路  3Comments

vadimsg picture vadimsg  路  3Comments

danieloprado picture danieloprado  路  3Comments

bimohxh picture bimohxh  路  3Comments

maicong picture maicong  路  3Comments