onBeforeRender() hook

The onBeforeRender() hook is usually used to provide further pageContext values.

We can also provide initial pageContext values at our server middlware (see createPageRenderer()) which is where we usually pass information about the authenticated user (see Authentication guide).

The onBeforeRender() hook is usually used together with passToClient to fetch data, see Data Fetching guide.

Since onBeforeRender() is always called in Node.js, ORM/SQL database queries can be used.

// /pages/movies.page.server.js

import fetch from "node-fetch";

export { onBeforeRender }

async function onBeforeRender(pageContext){
  const response = await fetch("https://api.imdb.com/api/movies/")
  const { movies } = await response.json()
  /* Or with an ORM:
  const movies = Movie.findAll() */
  /* Or with SQL:
  const movies = sql`SELECT * FROM movies;` */
  return {
    pageContext: {
      movies
    }
  }
}
Edit this page