From 2ee0e77f592bf7da6b89ee9f3cf8551bf5f8b7d2 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Wed, 3 Aug 2022 18:01:16 +0430 Subject: [PATCH 1/6] feat: pre fetch contents on build --- src/module.ts | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/module.ts b/src/module.ts index c8f298cb8..3fed80600 100644 --- a/src/module.ts +++ b/src/module.ts @@ -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, @@ -511,8 +513,35 @@ export default defineNuxtModule({ ...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`) + }) + // 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') + } + for (const [key, source] of Object.entries(sources)) { + storage.mount(key, getMountDriver(source)) + } + const keys = await storage.getKeys() + await Promise.all( + keys.map(async (key) => { + const content = await storage.getItem(key) + await storage.setItem(`cache:content:parsed:${key.substring(14)}`, content) + }) + ) + }) + return + } nuxt.hook('nitro:init', async (nitro) => { if (!options.watch || !options.watch.ws) { return } @@ -543,13 +572,6 @@ export default defineNuxtModule({ 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`) - }) } }) From d55452dc6ea5daf77adc8135e70d94ed628c46c2 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Wed, 3 Aug 2022 18:09:38 +0430 Subject: [PATCH 2/6] chore: handle ignores keys --- src/module.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/module.ts b/src/module.ts index 3fed80600..88e65bdd0 100644 --- a/src/module.ts +++ b/src/module.ts @@ -532,9 +532,25 @@ export default defineNuxtModule({ for (const [key, source] of Object.entries(sources)) { storage.mount(key, getMountDriver(source)) } + /** + * Invalid key characters + */ + const invalidKeyCharacters = "'\"?#/".split('') + /** + * Content ignore patterns + */ + const contentIgnores: Array = contentContext.ignores.map((p: any) => + typeof p === 'string' ? new RegExp(`^${p}|:${p}`) : p + ) const keys = await storage.getKeys() await Promise.all( keys.map(async (key) => { + if (key.startsWith('preview:') || contentIgnores.some(prefix => prefix.test(key))) { + return + } + if (invalidKeyCharacters.some(ik => key.includes(ik))) { + return + } const content = await storage.getItem(key) await storage.setItem(`cache:content:parsed:${key.substring(14)}`, content) }) From 051eb7477a01d71ffe9de537d950cc2e43d7b2e8 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 8 Aug 2022 17:35:35 +0430 Subject: [PATCH 3/6] chore: cleanup --- src/module.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/module.ts b/src/module.ts index 88e65bdd0..95ae561ae 100644 --- a/src/module.ts +++ b/src/module.ts @@ -532,28 +532,27 @@ export default defineNuxtModule({ for (const [key, source] of Object.entries(sources)) { storage.mount(key, getMountDriver(source)) } - /** - * Invalid key characters - */ + let keys = await storage.getKeys() + + // Filter invalid characters & ignore patterns const invalidKeyCharacters = "'\"?#/".split('') - /** - * Content ignore patterns - */ const contentIgnores: Array = contentContext.ignores.map((p: any) => typeof p === 'string' ? new RegExp(`^${p}|:${p}`) : p ) - const keys = await storage.getKeys() + 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) => { - if (key.startsWith('preview:') || contentIgnores.some(prefix => prefix.test(key))) { - return - } - if (invalidKeyCharacters.some(ik => key.includes(ik))) { - return - } - const content = await storage.getItem(key) - await storage.setItem(`cache:content:parsed:${key.substring(14)}`, content) - }) + keys.map(async key => await storage.setItem( + `cache:content:parsed:${key.substring(14)}`, + await storage.getItem(key) + )) ) }) return From b71427d93fe8e6ab0b455014db052a2920c1a55f Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 8 Aug 2022 19:43:12 +0430 Subject: [PATCH 4/6] chore: freeze cache dir --- src/module.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/module.ts b/src/module.ts index 95ae561ae..11eec1e36 100644 --- a/src/module.ts +++ b/src/module.ts @@ -278,6 +278,10 @@ export default defineNuxtModule({ // 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') + } // Tell Nuxt to ignore content dir for app build for (const source of Object.values(sources)) { @@ -517,7 +521,7 @@ export default defineNuxtModule({ // @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`) + tailwindConfig.content.push(resolve(nuxt.options.rootDir, '.nuxt', 'content-cache', 'parsed/**/*.md')) }) // Setup content dev module @@ -525,9 +529,9 @@ export default defineNuxtModule({ nuxt.hook('build:before', async () => { const storage = createStorage() const sources = useContentMounts(nuxt, contentContext.sources) - sources.cache = { + sources['cache:content'] = { driver: 'fs', - base: resolve(nuxt.options.buildDir, 'cache') + base: resolve(nuxt.options.rootDir, '.nuxt', 'content-cache') } for (const [key, source] of Object.entries(sources)) { storage.mount(key, getMountDriver(source)) From 37085b15bf833b52634a08f75b7547766be211cb Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 8 Aug 2022 20:26:47 +0430 Subject: [PATCH 5/6] fix: get correct keys --- src/module.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/module.ts b/src/module.ts index 11eec1e36..ee286af09 100644 --- a/src/module.ts +++ b/src/module.ts @@ -536,7 +536,7 @@ export default defineNuxtModule({ for (const [key, source] of Object.entries(sources)) { storage.mount(key, getMountDriver(source)) } - let keys = await storage.getKeys() + let keys = await storage.getKeys('content:source') // Filter invalid characters & ignore patterns const invalidKeyCharacters = "'\"?#/".split('') @@ -554,7 +554,7 @@ export default defineNuxtModule({ }) await Promise.all( keys.map(async key => await storage.setItem( - `cache:content:parsed:${key.substring(14)}`, + `cache:content:parsed:${key.substring(15)}`, await storage.getItem(key) )) ) From a5913b41f293a87ff0489df8c946a140f6d9bf93 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 8 Aug 2022 20:30:07 +0430 Subject: [PATCH 6/6] chore: use buildDir --- src/module.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/module.ts b/src/module.ts index ee286af09..6745c67e6 100644 --- a/src/module.ts +++ b/src/module.ts @@ -280,7 +280,7 @@ export default defineNuxtModule({ nitroConfig.devStorage = Object.assign(nitroConfig.devStorage || {}, sources) nitroConfig.devStorage['cache:content'] = { driver: 'fs', - base: resolve(nuxt.options.rootDir, '.nuxt', 'content-cache') + base: resolve(nuxt.options.buildDir, 'content-cache') } // Tell Nuxt to ignore content dir for app build @@ -521,7 +521,7 @@ export default defineNuxtModule({ // @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')) + tailwindConfig.content.push(resolve(nuxt.options.buildDir, 'content-cache', 'parsed/**/*.md')) }) // Setup content dev module @@ -531,7 +531,7 @@ export default defineNuxtModule({ const sources = useContentMounts(nuxt, contentContext.sources) sources['cache:content'] = { driver: 'fs', - base: resolve(nuxt.options.rootDir, '.nuxt', 'content-cache') + base: resolve(nuxt.options.buildDir, 'content-cache') } for (const [key, source] of Object.entries(sources)) { storage.mount(key, getMountDriver(source))