Clone this repository and then run the following commands:
npm i
npm run dev
# or
yarn
yarn dev
Using package gray-matter.
Using package marked.
We use the serve package for this.
build: next build && next export
next export
will not work. To make sure, it does ,we can do the following:yarn build
serve -s out
In NextJs, api routes behave as serverless functions when deployed so they cannot use fileSystem(fileReaders and writers), hence we cannot directly read and parse data from markdown files and resond with this result unlike getStaticProps which is run server side(in NodeJs) which can use filesystem.
For this, we make a script ( /scripts/cache.js ), which we will run before committing the code (pre-commit)
This script will parse all markdown files and write that data in another file ( /cache/data.js ) in a constant which is also exported.
Now in API handler, we can simply import the constant from the cache which has the data and return it after processing it.
npx husky add .husky/pre-commit "yarn cache-blogs && git add cache/data.js"
yarn cache-blogs
will make changes to said file so we need to stage it in this hook.