nuxt.renderRoute(route, context)

Render a specific route with a given context.


  • Type: Function
  • Arguments:
    1. String : route to render
    2. Optional, Object, context given, available keys: req & res
  • Returns: Promise
    • html: String
    • error: null or Object
    • redirected: false or Object

This method should be used mostly for test purposes as well as with nuxt.renderAndGetWindow.

nuxt.renderRoute(route, context) - 图1

nuxt.renderRoute should be executed after the build process in production mode.

  1. const { loadNuxt, build } = require('nuxt')
  2. async function start() {
  3. // Get nuxt instance for start (production mode)
  4. // Make sure to have run `nuxt build` before running this script
  5. const nuxt = await loadNuxt({ for: 'start' })
  6. const { html, error, redirected } = await nuxt.renderRoute('/')
  7. // `html` will always be a string
  8. // `error` not null when the error layout is displayed, the error format is:
  9. // { statusCode: 500, message: 'My error message' }
  10. // `redirected` is not `false` when `redirect()` has been used in `asyncData()` or `fetch()`
  11. // { path: '/other-path', query: {}, status: 302 }
  12. }
  13. start()