Cleanup post caching
This commit is contained in:
1
util/post-cache.d.ts
vendored
1
util/post-cache.d.ts
vendored
@@ -1 +0,0 @@
|
||||
export default function cachePostLinkData(): any;
|
||||
@@ -1,16 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const { getAllPosts } = require('./slug');
|
||||
const { join } = require('path');
|
||||
|
||||
const publicDir = join(process.cwd(), 'public');
|
||||
|
||||
module.exports = {
|
||||
cachePostLinkData: () => {
|
||||
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
|
||||
fs.writeFile(`${publicDir}/posts.json`, JSON.stringify(posts), (e) => {
|
||||
if (e)
|
||||
console.error(e);
|
||||
});
|
||||
return posts;
|
||||
}
|
||||
}
|
||||
17
util/slug.d.ts
vendored
17
util/slug.d.ts
vendored
@@ -1,4 +1,17 @@
|
||||
interface Post { slug?: string, rawslug?: string, content?: string, title?: string };
|
||||
interface Post {
|
||||
slug?: string;
|
||||
rawslug?: string;
|
||||
content?: string;
|
||||
title?: string;
|
||||
};
|
||||
|
||||
interface PostMeta {
|
||||
title: string;
|
||||
slug: string;
|
||||
created_at: string;
|
||||
last_updated: string;
|
||||
};
|
||||
|
||||
export function getAllPosts(filter: Array<any> = []): Post[];
|
||||
export function getPost(rawslug: string, filter: Array<any> = []): Post;
|
||||
export function getPost(rawslug: string, filter: Array<any> = []): Post;
|
||||
export function getPostsMeta(): PostMeta[];
|
||||
30
util/slug.js
30
util/slug.js
@@ -2,11 +2,13 @@ const fs = require('fs');
|
||||
const matter = require('gray-matter');
|
||||
const { join } = require('path');
|
||||
|
||||
const postsDirectory = join(process.cwd(), 'posts');
|
||||
const postsDir = join(process.cwd(), 'posts');
|
||||
const cacheDir = join(process.cwd(), '.next', 'cache');
|
||||
const publicDir = join(process.cwd(), 'public');
|
||||
|
||||
function getPost(rawslug, filter = []) {
|
||||
const slug = rawslug.replace(/\.md$/, '');
|
||||
const path = join(postsDirectory, `${slug}.md`);
|
||||
const path = join(postsDir, `${slug}.md`);
|
||||
const file = fs.readFileSync(path, 'utf-8');
|
||||
const { data, content } = matter(file);
|
||||
|
||||
@@ -35,7 +37,7 @@ function getPost(rawslug, filter = []) {
|
||||
}
|
||||
|
||||
function getAllPosts(filter = []) {
|
||||
const files = fs.readdirSync(postsDirectory);
|
||||
const files = fs.readdirSync(postsDir);
|
||||
|
||||
return files
|
||||
.filter(c => !c.match(/^\./))
|
||||
@@ -44,4 +46,24 @@ function getAllPosts(filter = []) {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { getAllPosts, getPost };
|
||||
|
||||
function cachePostsMeta() { // public access cache
|
||||
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
|
||||
fs.writeFile(join(publicDir, 'posts.json'), JSON.stringify(posts), (e) => {
|
||||
if (e)
|
||||
console.error(e);
|
||||
});
|
||||
return posts;
|
||||
}
|
||||
|
||||
function getPostsMeta() {
|
||||
const file = fs.readFileSync(join(publicDir, 'posts.json'), 'utf-8');
|
||||
|
||||
if (!file) {
|
||||
return cachePostsMeta();
|
||||
}
|
||||
|
||||
return JSON.parse(file);
|
||||
}
|
||||
|
||||
module.exports = { getAllPosts, getPost, getPostsMeta, cachePostsMeta };
|
||||
Reference in New Issue
Block a user