Skip to content

Best way to retrieve frontmatter from the next page #4831

Answered by brc-dd
BenjaminOddou asked this question in Q&A
Discussion options

You must be logged in to vote

Hmm page is loaded after route change. So, hard to get it that way. Maybe you can extract the relevant data to a separate file (maybe createContentLoader) and do something like this:

// .vitepress/theme/index.ts

import type { Theme } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import { data } from '../../frontmatter.data.js'

export default {
  extends: DefaultTheme,
  enhanceApp({ router }) {
    router.onBeforeRouteChange = (to) => {
     const frontmatter = data[to]
    }
  }
} satisfies Theme

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@BenjaminOddou
Comment options

@brc-dd
Comment options

@brc-dd
Comment options

@BenjaminOddou
Comment options

@BenjaminOddou
Comment options

Answer selected by BenjaminOddou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants