Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(dev-cache): improve localhost markdown page navigation performance (when having 2,000+ pages) #2675

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
defineNuxtModule,
extendViteConfig,
installModule,
addVitePlugin
addVitePlugin,
addServerPlugin
} from '@nuxt/kit'
import type { Component } from '@nuxt/schema'
import { defu } from 'defu'
Expand Down Expand Up @@ -447,6 +448,9 @@ export default defineNuxtModule<ModuleOptions>({
? './plugins/documentDriven'
: './legacy/plugins/documentDriven'
))
if (nuxt.options.dev) {
addServerPlugin(resolveRuntimeModule("./server/plugins/refresh-cache"));
}

if (options.documentDriven.injectPage) {
nuxt.options.pages = true
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/server/plugins/refresh-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineNitroPlugin } from 'nitropack/runtime'
import { cleanCachedContents } from "../storage";
import type { WatchEvent } from 'unstorage';
// @ts-expect-error
import { useStorage } from '#imports'

export default defineNitroPlugin(() => {
const storage = useStorage()

storage.watch(async (event: WatchEvent, key: string) => {
if (key.startsWith('content:source')) {
cleanCachedContents();
}
});
})
10 changes: 7 additions & 3 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ export function* chunksFromArray<T> (arr: T[], n: number) : Generator<T[], void>
}
}

let cachedContents: ParsedContent[] = []
export const cleanCachedContents = () => {
cachedContents = [];
}

export const getContentsList = (() => {
let cachedContents: ParsedContent[] = []
let pendingContentsListPromise: Promise<ParsedContent[]> | null = null

const _getContentsList = async (event: H3Event, prefix?: string) => {
Expand All @@ -144,14 +148,14 @@ export const getContentsList = (() => {
if (event.context.__contentList) {
return event.context.__contentList
}
if (isPrerendering && cachedContents.length) {
if ((isPrerendering || !isProduction) && cachedContents.length) {
return cachedContents
}

if (!pendingContentsListPromise) {
pendingContentsListPromise = _getContentsList(event, prefix)
pendingContentsListPromise.then((result) => {
if (isPrerendering) {
if (isPrerendering || !isProduction) {
cachedContents = result as ParsedContent[]
}
event.context.__contentList = result as ParsedContent[]
Expand Down
Loading