API: The render Property

Nuxt.js lets you customize runtime options for rendering pages

bundleRenderer

  • Type: Object

Use this option to customize vue SSR bundle renderer. This option is skipped for spa mode.

  1. export default {
  2. render: {
  3. bundleRenderer: {
  4. directives: {
  5. custom1 (el, dir) {
  6. // something ...
  7. }
  8. }
  9. }
  10. }
  11. }

Learn more about available options on Vue SSR API Reference.It is recommended to not use this option as Nuxt.js is already providing best SSR defaults and misconfiguration might lead to SSR problems.

etag

  • Type: Object
    • Default: { weak: true }

To disable etag for pages set etag: false

See etag docs for possible options.

You can use your own hash function by specifying etag.hash:

nuxt.config.js

  1. import { murmurHash128 } from 'murmurhash-native'
  2. export default {
  3. render: {
  4. etag: {
  5. hash: html => murmurHash128(html)
  6. }
  7. }
  8. }

In this case we use murmurhash-native, which is faster for larger html body sizes. Note that the weak option is ignored, when specifying your own hash function.

compressor

  • Type Object
    • Default: { threshold: 0 }

When providing an object, the compression middlewarewill be used (with respective options).

If you want to use your own compression middleware, you can reference itdirectly (f.ex. otherComp({ myOptions: 'example' })).

To disable compression, use compressor: false.

fallback

  • Type Object
    • Default: { dist: {}, static: { skipUnknown: true } }

Options for serve-placeholder middleware.

If you want to disable one of them or both, you can pass a falsy value.

http2

  • Type Object
    • Default: { push: false, pushAssets: null }

Activate HTTP2 push headers.

You can control what links to push using pushAssets function.

Example:

  1. pushAssets: (req, res, publicPath, preloadFiles) => preloadFiles
  2. .filter(f => f.asType === 'script' && f.file === 'runtime.js')
  3. .map(f => `<${publicPath}${f.file}>; rel=preload; as=${f.asType}`)

You can add your own assets to the array as well.Using req and res you can decide what links to push based on the request headers, for example using the cookie with application version.

The assets will be joined together with , and passed as a single Link header.

injectScripts

  • Type: Boolean
    • Default: true

Adds the <script> for Nuxt bundles, set it to false to render pure HTML without JS (available with 2.8.0+)

resourceHints

  • Type: Boolean
    • Default: true

Adds prefetch and preload links for faster initial page load time.

You may want to only disable this option if you have many pages and routes.

ssr

  • Type: Boolean
    • Default: true on universal mode and false on spa mode

Enable SSR rendering

This option is automatically set based on mode value if not provided.This can be useful to dynamically enable/disable SSR on runtime after image builds (with docker for example).

ssrLog

  • Type: Boolean | String
    • Default: true in dev mode and false in production

Forward server-side logs to the browser for better debugging (only available in development)

To collapse the logs, use 'collapsed' value.

static

  • Type: Object
    • Default: {}

Configure the static/ directory behaviour

See serve-static docs for possible options.

Additional to them, we introduced a prefix option which defaults to true.It will add the router base to your static assets.

Example:

  • Assets: favicon.ico
  • Router base: /t
  • With prefix: true (default): /t/favicon.ico
  • With prefix: false: /favicon.ico

Caveats:

Some URL rewrites might not respect the prefix.

dist

  • Type: Object
    • Default: { maxAge: '1y', index: false }

Options used for serving distribution files. Only applicable in production.

See serve-static docs for possible options.

csp

  • Type: Boolean or Object
    • Default: false

Use this to configure to load external resources of Content-Security-Policy

Note that CSP hashes will not be added if script-src policy contains 'unsafe-inline'. This is due to browser ignoring 'unsafe-inline' if hashes are present. Set option unsafeInlineCompatibility to true if you want both hashes and 'unsafe-inline' for CSPv1 compatibility.

In order to add <meta http-equiv="Content-Security-Policy"/> with all the CSP policies you need to set csp.addMeta to true.

Example (nuxt.config.js)

  1. export default {
  2. render: {
  3. csp: true
  4. }
  5. }
  6. // OR
  7. export default {
  8. render: {
  9. csp: {
  10. hashAlgorithm: 'sha256',
  11. policies: {
  12. 'script-src': [
  13. 'https://www.google-analytics.com',
  14. 'https://name.example.com'
  15. ],
  16. 'report-uri': [
  17. 'https://report.example.com/report-csp-violations'
  18. ]
  19. },
  20. addMeta: true
  21. }
  22. }
  23. }
  24. // OR
  25. /*
  26. The following example allows Google Analytics, LogRocket.io, and Sentry.io
  27. for logging and analytic tracking.
  28. Review to this blog on Sentry.io
  29. https://blog.sentry.io/2018/09/04/how-sentry-captures-csp-violations
  30. To learn what tracking link you should use.
  31. */
  32. const PRIMARY_HOSTS = `loc.example-website.com`
  33. export default {
  34. csp: {
  35. reportOnly: true,
  36. hashAlgorithm: 'sha256',
  37. policies: {
  38. 'default-src': ["'self'"],
  39. 'img-src': ['https:', '*.google-analytics.com'],
  40. 'worker-src': ["'self'", `blob:`, PRIMARY_HOSTS, '*.logrocket.io'],
  41. 'style-src': ["'self'", "'unsafe-inline'", PRIMARY_HOSTS],
  42. 'script-src': ["'self'", "'unsafe-inline'", PRIMARY_HOSTS, 'sentry.io', '*.sentry-cdn.com', '*.google-analytics.com', '*.logrocket.io'],
  43. 'connect-src': [PRIMARY_HOSTS, 'sentry.io', '*.google-analytics.com'],
  44. 'form-action': ["'self'"],
  45. 'frame-ancestors': ["'none'"],
  46. 'object-src': ["'none'"],
  47. 'base-uri': [PRIMARY_HOSTS],
  48. 'report-uri': [
  49. `https://sentry.io/api/<project>/security/?sentry_key=<key>`
  50. ]
  51. }
  52. }
  53. }