Error Tracking

You can install error tracking:

Client-side

You can use +client.js in order to make sure that your error tracker is initialized before any other code executes.

// pages/+client.js
 
// This is a good place to initialize an error tracker scuh as Sentry
Sentry.init()
 
// Custom tracking
window.addEventListener('error', (err) => {
   console.error('An error occurred:', err)
})

Server-side

Server-side errors are exposed over pageContext.errorWhileRendering at renderPage():

// server.js
 
import { renderPage } from 'vike/server'
 
// Any server: Express.js, Cloudflare Worker, AWS Lambda Function, Fastify, Hono, Nitro, ...
server.addMiddleware({
  method: 'GET',
  route: '*', // catch-all
  async handler(request) {
    const pageContext = await renderPage({ urlOriginal: request.url })
 
    if (pageContext.errorWhileRendering) {
      /* Vike already calls console.error() so the following line isn't needed.
      console.error(pageContext.errorWhileRendering)
      */
 
      // If you use an error tracker (e.g. Sentry):
      myErrorTracker.intercept(pageContext.errorWhileRendering)
    }
 
    // The HTTP response of the page.
    // If pageContext.errorWhileRendering is defined then this is usually the error page.
    return pageContext.httpResponse
  }
})

Vike calls console.error(err) for any error it encounters. With the upcoming new hook onLog() (#1439) you'll have full control over Vike's logging.

See also API > Error Page.

You can use any error tracker, such as Sentry, Bugsnag, Rollbar, or your own custom error tracking. For that, make sure to use the error tracker's server-side interceptor. For example with Sentry:

import * as Sentry from "@sentry/node";
 
// ...
 
    if (pageContext.errorWhileRendering) {
      Sentry.captureException(e)
    }
 
// ...

See also