vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.65k stars 2.05k forks source link

`enhanceApp()` `ctx.router` is not a router instance #4204

Open davestewart opened 1 week ago

davestewart commented 1 week ago

Describe the bug

The theme docs and code completion state that the router instance is exposed in enhanceApp() but it appears the router instance is actually a route instance, and doesn't conform to the Router interface.

CleanShot 2024-09-16 at 13 06 24

Reproduction

Initialize a new project, then add the following code to enhanceApp():

    console.log({ router })
    router.onAfterRouteChanged((to: string) => {
      console.log(`changed to: ${to}`)
    })

Expected behavior

We should be able to interact with the router instance

System Info

System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M3 Pro
    Memory: 213.02 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - /usr/local/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.7.0 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  Browsers:
    Brave Browser: 122.1.63.169
    Chrome: 128.0.6613.138
    Chrome Canary: 130.0.6720.0
    Edge: 128.0.2739.79
    Safari: 17.5
  npmPackages:
    vitepress: ^1.3.4 => 1.3.4

Additional context

No response

Validations

brc-dd commented 1 week ago

Ah, it's router, but that function is not exactly a hook. You need to define it - https://github.com/vuejs/vitepress/pull/2564#issue-1783819597

router.onBeforeRouteChange = (to) => {
  console.log('onBeforeRouteChange', to)
}
davestewart commented 1 week ago

Aha! Yes, I think defo a docs issue.

Thanks for coming back so very quickly!!! 🥰