impress-2020/scripts/build-cached-data.js
Matchu e20364cc0a create cache-asset-manifests script
Just gonna bulk load all those manifests into the db, and then that should make most loads notably faster by removing the net request! 🤞

We'll still load manifests inline sometimes, but only the first time anyone pulls up the layer in impress-2020. After that, it should be cached forever!
2020-08-17 18:23:39 -07:00

50 lines
1.4 KiB
JavaScript

// We run this on build to cache some stable database tables on the server!
const fs = require("fs").promises;
const path = require("path");
const connectToDb = require("../src/server/db");
const { normalizeRow } = require("../src/server/util");
const cachedDataPath = path.join(__dirname, "..", "build", "cached-data");
async function buildZonesCache(db) {
const [rows] = await db.query(`SELECT * FROM zones;`);
const entities = rows.map(normalizeRow);
const filePath = path.join(cachedDataPath, "zones.json");
fs.writeFile(filePath, JSON.stringify(entities, null, 4), "utf8");
console.log(`📚 Wrote zones to ${path.relative(process.cwd(), filePath)}`);
}
async function buildZoneTranslationsCache(db) {
const [rows] = await db.query(
`SELECT * FROM zone_translations WHERE locale = "en";`
);
const entities = rows.map(normalizeRow);
const filePath = path.join(cachedDataPath, "zone_translations.json");
fs.writeFile(filePath, JSON.stringify(entities, null, 4), "utf8");
console.log(
`📚 Wrote zone translations to ${path.relative(process.cwd(), filePath)}`
);
}
async function main() {
const db = await connectToDb();
await fs.mkdir(cachedDataPath, { recursive: true });
try {
await Promise.all([buildZonesCache(db), buildZoneTranslationsCache(db)]);
} catch (e) {
db.close();
throw e;
}
db.close();
}
main().catch((e) => {
console.error(e);
process.exitCode = 1;
});