• Type: Object

Configure the generation of your universal web application to a static web application.

When calling nuxt.generate(), Nuxt.js will use the configuration defined in the generate property.

nuxt.config.js

  1. export default {
  2. generate: {
  3. ...
  4. }
  5. }

cache

Introduced in v2.14.0

  • Type: Object or false

This option is used by nuxt generate with static target to avoid re-building when no tracked file has been changed.

Defaults:

  1. {
  2. ignore: [
  3. '.nuxt', // buildDir
  4. 'static', // dir.static
  5. 'dist', // generate.dir
  6. 'node_modules',
  7. '.**/*',
  8. '.*',
  9. 'README.md'
  10. ]
  11. }

If you want to avoid re-building when changing a configuration file, just add it to the list by providing the cache.ignore option:

nuxt.config.js

  1. export default {
  2. generate: {
  3. cache: {
  4. ignore: ['renovate.json'] // ignore changes applied on this file
  5. }
  6. }
  7. }

concurrency

  • Type: Number
  • Default: 500

The generation of routes are concurrent, generate.concurrency specifies the amount of routes that run in one thread.

crawler

  • Type: boolean
  • Default: true

As of Nuxt >= v2.13 Nuxt.js comes with a crawler installed that will crawl your relative links and generate your dynamic links based on these links. If you want to disable this feature you can set the value to false

  1. export default {
  2. generate: {
  3. crawler: false
  4. }
  5. }

dir

  • Type: String
  • Default: 'dist'

Directory name created when building web applications using the nuxt generate command.

devtools

  • Type: boolean
  • Default: false

Configure whether to allow vue-devtools inspection.

If you already activated through nuxt.config.js or otherwise, devtools enable regardless of the flag.

exclude

  • Type: Array
    • Items: String or RegExp

It accepts an array of string or regular expressions and will prevent generation of routes matching them. The routes will still be accessible when generate.fallback is used.

Taking this examples of structure:

  1. -| pages/
  2. ---| index.vue
  3. ---| admin/
  4. -----| about.vue
  5. -----| index.vue

By default, running nuxt generate a file will be created for each route.

  1. -| dist/
  2. ---| index.html
  3. ---| admin/
  4. -----| about.html
  5. -----| item.html

When adding a regular expression which matches all routes with “ignore”, it will prevent the generation of these routes.

nuxt.config.js

  1. export default {
  2. generate: {
  3. exclude: [
  4. /^\/admin/ // path starts with /admin
  5. ]
  6. }
  7. }
  1. -| dist/
  2. ---| index.html

You can also exclude a specific route by giving a string:

nuxt.config.js

  1. export default {
  2. generate: {
  3. exclude: ['/my-secret-page']
  4. }
  5. }

fallback

  • Type: String or Boolean
  • Default: 200.html

nuxt.config.js

  1. export default {
  2. generate: {
  3. fallback: '404.html'
  4. }
  5. }

The path to the fallback HTML file. It should be set as the error page, so that also unknown routes are rendered via Nuxt. If unset or set to a falsy value, the name of the fallback HTML file will be 200.html. If set to true, the filename will be 404.html. If you provide a string as a value, it will be used instead.

nuxt.config.js

  1. fallback: false;

If working with statically generated pages then it is recommended to use a 404.html for error pages and for those covered by excludes (the files that you do not want generated as static pages).

nuxt.config.js

  1. fallback: true

However, Nuxt allows you to configure any page you like so if you don’t want to use the 200.html or 404.html you can add a string and then you just have to make sure you redirect to that page instead. This is of course not necessary and is best to redirect to 200.html/404.html.

nuxt.config.js

  1. fallback: 'fallbackPage.html'

Note: Multiple services (e.g. Netlify) detect a 404.html automatically. If you configure your web server on your own, please consult its documentation to find out how to set up an error page (and set it to the 404.html file)

interval

  • Type: Number
  • Default: 0

Interval in milliseconds between two render cycles to avoid flooding a potential API with calls from the web application.

minify

routes

  • Type: Array

generate - 图1

As of Nuxt v2.13 there is a crawler installed that will crawl your link tags and generate your routes when running nuxt generate.

If have unlinked pages (such as secret pages) and you would like these to also be generated then you can use the generate.routes property.

generate - 图2

Dynamic routes are ignored by the generate command when using Nuxt <= v2.12

Example:

  1. -| pages/
  2. ---| index.vue
  3. ---| users/
  4. -----| _id.vue

Only the route / will be generated by Nuxt.js.

If you want Nuxt.js to generate routes with dynamic params, you need to set the generate.routes property to an array of dynamic routes.

We add routes for /users/:id:

nuxt.config.js

  1. export default {
  2. generate: {
  3. routes: ['/users/1', '/users/2', '/users/3']
  4. }
  5. }

Then when we launch nuxt generate:

  1. [nuxt] Generating...
  2. [...]
  3. nuxt:render Rendering url / +154ms
  4. nuxt:render Rendering url /users/1 +12ms
  5. nuxt:render Rendering url /users/2 +33ms
  6. nuxt:render Rendering url /users/3 +7ms
  7. nuxt:generate Generate file: /index.html +21ms
  8. nuxt:generate Generate file: /users/1/index.html +31ms
  9. nuxt:generate Generate file: /users/2/index.html +15ms
  10. nuxt:generate Generate file: /users/3/index.html +23ms
  11. nuxt:generate HTML Files generated in 7.6s +6ms
  12. [nuxt] Generate done

Great, but what if we have dynamic params?

  1. Use a Function which returns a Promise.
  2. Use a Function with a callback(err, params).

Function which returns a Promise

nuxt.config.js

  1. import axios from 'axios'
  2. export default {
  3. generate: {
  4. routes() {
  5. return axios.get('https://my-api/users').then(res => {
  6. return res.data.map(user => {
  7. return '/users/' + user.id
  8. })
  9. })
  10. }
  11. }
  12. }

Function with a callback

nuxt.config.js

  1. import axios from 'axios'
  2. export default {
  3. generate: {
  4. routes(callback) {
  5. axios
  6. .get('https://my-api/users')
  7. .then(res => {
  8. const routes = res.data.map(user => {
  9. return '/users/' + user.id
  10. })
  11. callback(null, routes)
  12. })
  13. .catch(callback)
  14. }
  15. }
  16. }

Speeding up dynamic route generation with payload

In the example above, we’re using the user.id from the server to generate the routes but tossing out the rest of the data. Typically, we need to fetch it again from inside the /users/_id.vue. While we can do that, we’ll probably need to set the generate.interval to something like 100 in order not to flood the server with calls. Because this will increase the run time of the generate script, it would be preferable to pass along the entire user object to the context in _id.vue. We do that by modifying the code above to this:

nuxt.config.js

  1. import axios from 'axios'
  2. export default {
  3. generate: {
  4. routes() {
  5. return axios.get('https://my-api/users').then(res => {
  6. return res.data.map(user => {
  7. return {
  8. route: '/users/' + user.id,
  9. payload: user
  10. }
  11. })
  12. })
  13. }
  14. }
  15. }

Now we can access the payload from /users/_id.vue like so:

  1. async asyncData ({ params, error, payload }) {
  2. if (payload) return { user: payload }
  3. else return { user: await backend.fetchUser(params.id) }
  4. }

subFolders

  • Type: Boolean
  • Default: true

By default, when running nuxt generate, Nuxt.js will create a directory for each route & serve an index.html file.

Example:

  1. -| dist/
  2. ---| index.html
  3. ---| about/
  4. -----| index.html
  5. ---| products/
  6. -----| item/
  7. -------| index.html

When set to false, HTML files are generated according to the route path:

nuxt.config.js

  1. export default {
  2. generate: {
  3. subFolders: false
  4. }
  5. }
  1. -| dist/
  2. ---| index.html
  3. ---| about.html
  4. ---| products/
  5. -----| item.html