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

feat: pre fetch contents on build #1411

Merged
merged 6 commits into from
Aug 8, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 45 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import { join } from 'pathe'
import type { Lang as ShikiLang, Theme as ShikiTheme } from 'shiki-es'
import { listen } from 'listhen'
import type { WatchEvent } from 'unstorage'
import { createStorage } from 'unstorage'
import { withTrailingSlash } from 'ufo'
import { name, version } from '../package.json'
import {
CACHE_VERSION,
createWebSocket,
getMountDriver,
logger,
MOUNT_PREFIX,
processMarkdownOptions,
Expand Down Expand Up @@ -511,8 +513,50 @@ export default defineNuxtModule<ModuleOptions>({
...contentContext as any
}

// @nuxtjs/tailwindcss support
// @ts-ignore - Module might not exist
nuxt.hook('tailwindcss:config', (tailwindConfig) => {
tailwindConfig.content = tailwindConfig.content ?? []
tailwindConfig.content.push(`${nuxt.options.buildDir}/cache/content/parsed/**/*.md`)
pi0 marked this conversation as resolved.
Show resolved Hide resolved
})

// Setup content dev module
if (!nuxt.options.dev) { return }
if (!nuxt.options.dev) {
nuxt.hook('build:before', async () => {
const storage = createStorage()
const sources = useContentMounts(nuxt, contentContext.sources)
sources.cache = {
driver: 'fs',
base: resolve(nuxt.options.buildDir, 'cache')
farnabaz marked this conversation as resolved.
Show resolved Hide resolved
}
for (const [key, source] of Object.entries(sources)) {
storage.mount(key, getMountDriver(source))
}
let keys = await storage.getKeys()

// Filter invalid characters & ignore patterns
const invalidKeyCharacters = "'\"?#/".split('')
const contentIgnores: Array<RegExp> = contentContext.ignores.map((p: any) =>
typeof p === 'string' ? new RegExp(`^${p}|:${p}`) : p
)
keys = keys.filter((key) => {
if (key.startsWith('preview:') || contentIgnores.some(prefix => prefix.test(key))) {
return false
}
if (invalidKeyCharacters.some(ik => key.includes(ik))) {
return false
}
return true
})
await Promise.all(
keys.map(async key => await storage.setItem(
`cache:content:parsed:${key.substring(14)}`,
await storage.getItem(key)
))
)
})
return
}

nuxt.hook('nitro:init', async (nitro) => {
if (!options.watch || !options.watch.ws) { return }
Expand Down Expand Up @@ -543,13 +587,6 @@ export default defineNuxtModule<ModuleOptions>({
ws.broadcast({ event, key })
})
})

// @nuxtjs/tailwindcss support
// @ts-ignore - Module might not exist
nuxt.hook('tailwindcss:config', (tailwindConfig) => {
tailwindConfig.content = tailwindConfig.content ?? []
tailwindConfig.content.push(`${nuxt.options.buildDir}/cache/content/parsed/**/*.md`)
})
}
})

Expand Down