Why donate
API Explorer
Upgrade Guide
Creating a New Project
The /quasar.config File
Convert q/app-webpack Project
Browser Compatibility
TypeScript Support
Directory Structure
Commands List
CSS Preprocessors
Page Routing with VueRouter
Lazy Loading - Code Splitting
Handling Assets
Boot Files
Prefetch Feature
API Proxying
Handling Vite
Handling import.meta.env
State Management with Pinia
Lint and Format Code
Testing & Auditing
Developing Mobile Apps
Ajax Requests
Opening Dev Server To Public
Quasar CLI with Vite - @quasar/app-vite v3
The ssrContext Object

The ssrContext Object is the SSR context with which all the app’s Vue components are rendered with.

Usage

WARNING

The ssrContext Object is available only on SSR builds, on the server-side compilation (when import.meta.env.QUASAR_SERVER is boolean true).

Among other places, it is supplied as parameter to boot files, to the Pinia instance and Vue Router initialization functions, and to the preFetch method:

// a boot file
export default defineBoot(({ ..., ssrContext }) => { /* ... */ })

// src/router/index.js
export default defineRouter(({ ..., ssrContext }) { /* ... */ })

// src/store/index.js
export default defineStore(({ ..., ssrContext }) { /* ... */ })

// with preFetch:
preFetch: definePreFetch(({ ..., ssrContext }) { /* ... */ })

You can also access the ssrContext in your Vue components. Below are two examples, one with Composition API and one with Options API:


import { useSSRContext } from 'vue'

export default {
  // ...
  setup () {
    // we need to guard it and call it only on SSR server-side:
    const ssrContext = import.meta.env.QUASAR_SERVER ? useSSRContext() : null
    // ...do something with it
  }
}

Anatomy of ssrContext

ssrContext: {
  req, // Express.js object
  res, // Express.js object
  $q, // The Quasar's $q Object
  nonce, // (optional to set it yourself)
  // The global "nonce" attribute to use

  onRendered, // Registers a function to be executed server-side after
  // app has been rendered with Vue. You might need this
  // to access ssrContext again after it has been fully processed.
  // Example: ssrContext.onRendered(() => { /* ... */ })

  rendered // (optional to set it yourself)
  // Set this to a function which will be executed server-side
  // after the app has been rendered with Vue.
  // We recommend using the "onRendered" instead.
  //
  // Purpose: backward compatibility with Vue ecosystem packages
  // (like @vue/apollo-ssr)
  // Example: ssrContext.rendered = () => { /* ... */ }
}

More information on the purpose of the “nonce” property is available on MDN.

The req and res are the Request and Response objects for the current server client. One use-case for req is accessing req.url to get the URL that the client is requesting.

TIP

Feel free to inject your own stuff into ssrContext too, but do NOT tamper with any of the private props (props that start with an underscore, eg. _someProp).