Skip to content

Commit 84b036d

Browse files
authored
feat: pre fetch contents on build (#1411)
1 parent 19bcce5 commit 84b036d

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

src/module.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import { join } from 'pathe'
1717
import type { Lang as ShikiLang, Theme as ShikiTheme } from 'shiki-es'
1818
import { listen } from 'listhen'
1919
import type { WatchEvent } from 'unstorage'
20+
import { createStorage } from 'unstorage'
2021
import { withTrailingSlash } from 'ufo'
2122
import { name, version } from '../package.json'
2223
import {
2324
CACHE_VERSION,
2425
createWebSocket,
26+
getMountDriver,
2527
logger,
2628
MOUNT_PREFIX,
2729
processMarkdownOptions,
@@ -276,6 +278,10 @@ export default defineNuxtModule<ModuleOptions>({
276278
// Register source storages
277279
const sources = useContentMounts(nuxt, contentContext.sources)
278280
nitroConfig.devStorage = Object.assign(nitroConfig.devStorage || {}, sources)
281+
nitroConfig.devStorage['cache:content'] = {
282+
driver: 'fs',
283+
base: resolve(nuxt.options.buildDir, 'content-cache')
284+
}
279285

280286
// Tell Nuxt to ignore content dir for app build
281287
for (const source of Object.values(sources)) {
@@ -511,8 +517,50 @@ export default defineNuxtModule<ModuleOptions>({
511517
...contentContext as any
512518
}
513519

520+
// @nuxtjs/tailwindcss support
521+
// @ts-ignore - Module might not exist
522+
nuxt.hook('tailwindcss:config', (tailwindConfig) => {
523+
tailwindConfig.content = tailwindConfig.content ?? []
524+
tailwindConfig.content.push(resolve(nuxt.options.buildDir, 'content-cache', 'parsed/**/*.md'))
525+
})
526+
514527
// Setup content dev module
515-
if (!nuxt.options.dev) { return }
528+
if (!nuxt.options.dev) {
529+
nuxt.hook('build:before', async () => {
530+
const storage = createStorage()
531+
const sources = useContentMounts(nuxt, contentContext.sources)
532+
sources['cache:content'] = {
533+
driver: 'fs',
534+
base: resolve(nuxt.options.buildDir, 'content-cache')
535+
}
536+
for (const [key, source] of Object.entries(sources)) {
537+
storage.mount(key, getMountDriver(source))
538+
}
539+
let keys = await storage.getKeys('content:source')
540+
541+
// Filter invalid characters & ignore patterns
542+
const invalidKeyCharacters = "'\"?#/".split('')
543+
const contentIgnores: Array<RegExp> = contentContext.ignores.map((p: any) =>
544+
typeof p === 'string' ? new RegExp(`^${p}|:${p}`) : p
545+
)
546+
keys = keys.filter((key) => {
547+
if (key.startsWith('preview:') || contentIgnores.some(prefix => prefix.test(key))) {
548+
return false
549+
}
550+
if (invalidKeyCharacters.some(ik => key.includes(ik))) {
551+
return false
552+
}
553+
return true
554+
})
555+
await Promise.all(
556+
keys.map(async key => await storage.setItem(
557+
`cache:content:parsed:${key.substring(15)}`,
558+
await storage.getItem(key)
559+
))
560+
)
561+
})
562+
return
563+
}
516564

517565
nuxt.hook('nitro:init', async (nitro) => {
518566
if (!options.watch || !options.watch.ws) { return }
@@ -547,13 +595,6 @@ export default defineNuxtModule<ModuleOptions>({
547595
ws.broadcast({ event, key })
548596
})
549597
})
550-
551-
// @nuxtjs/tailwindcss support
552-
// @ts-ignore - Module might not exist
553-
nuxt.hook('tailwindcss:config', (tailwindConfig) => {
554-
tailwindConfig.content = tailwindConfig.content ?? []
555-
tailwindConfig.content.push(`${nuxt.options.buildDir}/cache/content/parsed/**/*.md`)
556-
})
557598
}
558599
})
559600

0 commit comments

Comments
 (0)