Skip to content

Commit 8bbc5a8

Browse files
committed
ssr built app uses serialized content database
1 parent ebaeded commit 8bbc5a8

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

packages/content/lib/database.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const chokidar = require('chokidar')
55
const JSON5 = require('json5')
66
const Loki = require('@lokidb/loki').default
77
const LokiFullTextSearch = require('@lokidb/full-text-search').default
8+
const LokiFSStorage = require('@lokidb/fs-storage').default
89
const logger = require('consola').withScope('@nuxt/content')
910
const { default: PQueue } = require('p-queue')
1011
const { Markdown, YAML, CSV, XML } = require('../parsers')
@@ -71,6 +72,34 @@ class Database extends Hookable {
7172
logger.info(`Parsed ${this.items.count()} files in ${s},${Math.round(ns / 1e8)} seconds`)
7273
}
7374

75+
/**
76+
* Restore database from file
77+
* @param {string} dir - Directory containing database dump file.
78+
*/
79+
async load (dir) {
80+
const dbFilename = this.db.filename
81+
this.db.filename = join(dir, dbFilename)
82+
await this.db.initializePersistence({ adapter: new LokiFSStorage() })
83+
await this.db.loadDatabase()
84+
this.db.filename = dbFilename
85+
86+
// recreate references
87+
this.items = this.db.getCollection('items')
88+
this.dirs = this.items.mapReduce(doc => doc.dir, dirs => [...new Set(dirs)])
89+
}
90+
91+
/**
92+
* Store database info file
93+
* @param {string} dir - Directory containing database dump file.
94+
*/
95+
async save (dir) {
96+
const dbFilename = this.db.filename
97+
this.db.filename = join(dir, dbFilename)
98+
await this.db.initializePersistence({ adapter: new LokiFSStorage() })
99+
await this.db.saveDatabase()
100+
this.db.filename = dbFilename
101+
}
102+
74103
/**
75104
* Walk dir tree recursively
76105
* @param {string} dir - Directory to browse.
@@ -171,7 +200,7 @@ class Database extends Hookable {
171200
*/
172201
async parseFile (path) {
173202
const extension = extname(path)
174-
// If unkown extension, skip
203+
// If unknown extension, skip
175204
if (!EXTENSIONS.includes(extension) && !this.extendParserExtensions.includes(extension)) {
176205
return
177206
}

packages/content/lib/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,18 @@ module.exports = async function (moduleOptions) {
108108
)
109109
database.hook('file:updated', event => ws.broadcast(event))
110110

111-
// Initialize database from file system
112-
await database.init()
111+
if (this.options.server && !this.options.dev && this.options.ssr) {
112+
// Load database from dist
113+
const dir = resolve(this.options.buildDir, 'dist', 'server')
114+
await database.load(dir)
115+
} else {
116+
// Initialize database from file system
117+
await database.init()
118+
}
113119

114120
// close database when Nuxt closes
115121
this.nuxt.hook('close', () => database.close())
116-
// listen to nuxt server to updrag
122+
// listen to nuxt server to upgrade
117123

118124
const $content = function () {
119125
let options
@@ -225,6 +231,13 @@ module.exports = async function (moduleOptions) {
225231
}
226232
})
227233
} else {
234+
if (!this.options.dev) {
235+
this.nuxt.hook('build:done', async () => {
236+
const dir = resolve(this.options.buildDir, 'dist', 'server')
237+
await database.save(dir)
238+
})
239+
}
240+
228241
this.addPlugin({
229242
fileName: 'content/plugin.client.js',
230243
src: join(__dirname, '../templates/plugin.client.js'),

packages/content/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"main": "lib/",
1414
"dependencies": {
15+
"@lokidb/fs-storage": "^2.1.0",
1516
"@lokidb/full-text-search": "^2.1.0",
1617
"@lokidb/loki": "^2.1.0",
1718
"@nuxt/types": "^2.14.12",

0 commit comments

Comments
 (0)