Skip to content

Commit

Permalink
fix: server-routes watcher (#205)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
arashsheyda and antfu committed Apr 26, 2023
1 parent ff27b92 commit 19fda12
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/devtools/package.json
Expand Up @@ -57,6 +57,7 @@
"nypm": "^0.2.0",
"pacote": "^15.1.2",
"pathe": "^1.1.0",
"perfect-debounce": "^0.1.3",
"picocolors": "^1.0.0",
"pkg-types": "^1.0.2",
"rc9": "^2.1.0",
Expand Down
13 changes: 13 additions & 0 deletions packages/devtools/src/server-rpc/server-routes.ts
Expand Up @@ -2,19 +2,32 @@ import type { Nitro } from 'nitropack'
import { join, resolve } from 'pathe'
import fg from 'fast-glob'
import { withBase, withLeadingSlash, withoutTrailingSlash } from 'ufo'
import { debounce } from 'perfect-debounce'
import type { NuxtDevtoolsServerContext, ServerFunctions, ServerRouteInfo } from '../types'

export function setupServerRoutesRPC({ nuxt, refresh }: NuxtDevtoolsServerContext) {
let nitro: Nitro

let promiseCache: Promise<ServerRouteInfo[]> | null = null

const refreshDebounced = debounce(() => {
promiseCache = null
refresh('getServerRoutes')
}, 500)

nuxt.hook('nitro:init', (_) => {
nitro = _
promiseCache = null
refresh('getServerRoutes')
})

nuxt.hook('ready', () => {
nitro?.storage.watch((event, key) => {
if (key.startsWith('src:api:'))
refreshDebounced()
})
})

function scan() {
if (promiseCache)
return promiseCache
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 19fda12

Please sign in to comment.