Cleanup post caching
This commit is contained in:
parent
24a76f8f7c
commit
415f950a42
@ -1,8 +1,8 @@
|
|||||||
import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react';
|
import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import Fuzzy from './_fuzzy';
|
import Fuzzy from './_fuzzy';
|
||||||
import pages from '../public/pages.json';
|
import pages from '../public/pages.json';
|
||||||
import posts from '../public/posts.json';
|
|
||||||
import style from '../styles/fuzzy.module.css';
|
import style from '../styles/fuzzy.module.css';
|
||||||
|
import posts from '../public/posts.json'
|
||||||
|
|
||||||
function FuzzyBar(): JSX.Element {
|
function FuzzyBar(): JSX.Element {
|
||||||
const searchField = useRef<any>(null);
|
const searchField = useRef<any>(null);
|
||||||
|
@ -6,13 +6,13 @@ module.exports = {
|
|||||||
webpack: (config, options) => {
|
webpack: (config, options) => {
|
||||||
config.experiments = { asset: true };
|
config.experiments = { asset: true };
|
||||||
|
|
||||||
const { cachePostLinkData } = require('./util/post-cache');
|
const { cachePostsMeta } = require('./util/slug');
|
||||||
|
|
||||||
config.plugins.push(
|
config.plugins.push(
|
||||||
{
|
{
|
||||||
apply: (compiler) => {
|
apply: (compiler) => {
|
||||||
compiler.hooks.beforeCompile.tap('cachePostLinkDataInit', _ => {
|
compiler.hooks.initialize.tap('cachePostDataBC', _ => {
|
||||||
cachePostLinkData();
|
cachePostsMeta();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
test: /\.svg$/,
|
test: /\.svg$/,
|
||||||
use: [{ loader: "@svgr/webpack" }],
|
use: [{ loader: '@svgr/webpack' }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
test: /\.md$/,
|
test: /\.md$/,
|
||||||
|
@ -2,6 +2,6 @@ import type { AppProps } from 'next/app'
|
|||||||
import 'normalize.css';
|
import 'normalize.css';
|
||||||
import '../styles/global.css';
|
import '../styles/global.css';
|
||||||
|
|
||||||
export default function MyApp({ Component, pageProps }: AppProps) {
|
export default function App({ Component, pageProps }: AppProps) {
|
||||||
return <Component {...pageProps} />
|
return <Component {...pageProps} />
|
||||||
}
|
}
|
@ -2,12 +2,12 @@ import Link from 'next/link';
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Layout from '../components/layout';
|
import Layout from '../components/layout';
|
||||||
import Pages from '../public/pages.json';
|
import Pages from '../public/pages.json';
|
||||||
import Posts from '../public/posts.json';
|
|
||||||
import style from '../styles/home.module.css';
|
import style from '../styles/home.module.css';
|
||||||
import prettyDatePrint from '../util/pretty-date';
|
import prettyDatePrint from '../util/pretty-date';
|
||||||
|
import { getPostsMeta, PostMeta } from '../util/slug';
|
||||||
|
|
||||||
function HomePage({ posts }: any) {
|
function HomePage(props: {postsMeta: PostMeta[]}) {
|
||||||
Pages.sort((x, y) => { return (x.title).localeCompare(y.title) });
|
props.postsMeta.sort((x, y) => { return (x.title).localeCompare(y.title) });
|
||||||
return (
|
return (
|
||||||
<Layout name='' title='PaulW.XYZ'>
|
<Layout name='' title='PaulW.XYZ'>
|
||||||
<section className='block'>
|
<section className='block'>
|
||||||
@ -25,7 +25,7 @@ function HomePage({ posts }: any) {
|
|||||||
<section className='block'>
|
<section className='block'>
|
||||||
<table style={{ width: '100%' }}>
|
<table style={{ width: '100%' }}>
|
||||||
<th className='h2'>Posts</th> <th>Posted</th>
|
<th className='h2'>Posts</th> <th>Posted</th>
|
||||||
{Posts?.map((post: any) => {
|
{props.postsMeta?.map((post: any) => {
|
||||||
return <tr key={post.slug}>
|
return <tr key={post.slug}>
|
||||||
<td className='h5'>
|
<td className='h5'>
|
||||||
<Link href={`posts/${post.slug}`}>
|
<Link href={`posts/${post.slug}`}>
|
||||||
@ -42,4 +42,10 @@ function HomePage({ posts }: any) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getServerSideProps() {
|
||||||
|
return {
|
||||||
|
props: { postsMeta: getPostsMeta() }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default HomePage;
|
export default HomePage;
|
@ -1,12 +1,9 @@
|
|||||||
import Layout from '../../components/layout';
|
import Layout from '../../components/layout';
|
||||||
import { useRouter } from 'next/router';
|
|
||||||
import { getAllPosts, getPost } from '../../util/slug';
|
import { getAllPosts, getPost } from '../../util/slug';
|
||||||
import ReactMarkdown from 'react-markdown';
|
import ReactMarkdown from 'react-markdown';
|
||||||
import Image from 'next/image';
|
|
||||||
import style from '../../styles/post.module.css';
|
import style from '../../styles/post.module.css';
|
||||||
|
|
||||||
function Post({ post }: any) { // eh
|
function Post({ post }: any) { // eh
|
||||||
const router = useRouter();
|
|
||||||
return (<>
|
return (<>
|
||||||
<Layout name={post.title} title={post.title} ancestors={[{ name: 'Posts', path: 'posts' }]}>
|
<Layout name={post.title} title={post.title} ancestors={[{ name: 'Posts', path: 'posts' }]}>
|
||||||
<div className={style.imageBlock} style={{ backgroundImage: post.cover ? `url(/assets/images/${post.cover})` : '' }}>
|
<div className={style.imageBlock} style={{ backgroundImage: post.cover ? `url(/assets/images/${post.cover})` : '' }}>
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Layout from '../../components/layout';
|
import Layout from '../../components/layout';
|
||||||
import Posts from '../../public/posts.json';
|
|
||||||
import prettyDatePrint from '../../util/pretty-date';
|
import prettyDatePrint from '../../util/pretty-date';
|
||||||
|
import { getPostsMeta, PostMeta } from '../../util/slug';
|
||||||
|
|
||||||
function HomePage({posts}: any) {
|
function HomePage(props: {postsMeta: PostMeta[]}) {
|
||||||
Posts.sort((x, y) => { return (x as any).title.localeCompare((x as any).title) });
|
props.postsMeta.sort((x: any, y: any) => { return (x as any).title.localeCompare((y as any).title) });
|
||||||
// todo: create a table-like interface
|
// todo: create a table-like user interface
|
||||||
return (
|
return (
|
||||||
<Layout name='Posts'>
|
<Layout name='Posts'>
|
||||||
<>
|
<>
|
||||||
<section className='h4 block'>
|
<section className='h4 block'>
|
||||||
Post Name <span style={{float: 'right', margin: 'auto 1rem'}}> Created on </span> <span style={{float: 'right', margin: 'auto 1rem'}}>Last Updated </span>
|
Post Name <span style={{float: 'right', margin: 'auto 1rem'}}> Created on </span> <span style={{float: 'right', margin: 'auto 1rem'}}>Last Updated </span>
|
||||||
</section>
|
</section>
|
||||||
{Posts.map((post: any) => {
|
{props.postsMeta.map((post: any) => {
|
||||||
return <section key='' className='h5 block'>
|
return <section key='' className='h5 block'>
|
||||||
<Link href={`posts/${post.slug}`}>
|
<Link href={`posts/${post.slug}`}>
|
||||||
{post.title}
|
{post.title}
|
||||||
@ -27,4 +27,10 @@ function HomePage({posts}: any) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getServerSideProps() {
|
||||||
|
return {
|
||||||
|
props: { postsMeta: getPostsMeta() }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default HomePage;
|
export default HomePage;
|
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 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 matter = require('gray-matter');
|
||||||
const { join } = require('path');
|
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 = []) {
|
function getPost(rawslug, filter = []) {
|
||||||
const slug = rawslug.replace(/\.md$/, '');
|
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 file = fs.readFileSync(path, 'utf-8');
|
||||||
const { data, content } = matter(file);
|
const { data, content } = matter(file);
|
||||||
|
|
||||||
@ -35,7 +37,7 @@ function getPost(rawslug, filter = []) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getAllPosts(filter = []) {
|
function getAllPosts(filter = []) {
|
||||||
const files = fs.readdirSync(postsDirectory);
|
const files = fs.readdirSync(postsDir);
|
||||||
|
|
||||||
return files
|
return files
|
||||||
.filter(c => !c.match(/^\./))
|
.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 };
|
Loading…
Reference in New Issue
Block a user