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 4 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
57 changes: 49 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 @@ -276,6 +278,10 @@ export default defineNuxtModule<ModuleOptions>({
// Register source storages
const sources = useContentMounts(nuxt, contentContext.sources)
nitroConfig.devStorage = Object.assign(nitroConfig.devStorage || {}, sources)
nitroConfig.devStorage['cache:content'] = {
driver: 'fs',
base: resolve(nuxt.options.rootDir, '.nuxt', 'content-cache')
farnabaz marked this conversation as resolved.
Show resolved Hide resolved
}

// Tell Nuxt to ignore content dir for app build
for (const source of Object.values(sources)) {
Expand Down Expand Up @@ -511,8 +517,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(resolve(nuxt.options.rootDir, '.nuxt', 'content-cache', 'parsed/**/*.md'))
})

// 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:content'] = {
driver: 'fs',
base: resolve(nuxt.options.rootDir, '.nuxt', 'content-cache')
}
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 +591,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