read from file system with getstaticprops

read from file system with getstaticprops

Files can be read directly from the filesystem in getStaticProps with `process.cwd()`. In order to do so you have to get the full path to a file.

Copy Snippet
// This function gets called at build time on server-side.
// It won't be called on client-side.
export async function getStaticProps() {
  const postsDirectory = path.join(process.cwd(), 'posts')
  const filenames = fs.readdirSync(postsDirectory)

  const posts = => {
    const filePath = path.join(postsDirectory, filename)
    const fileContents = fs.readFileSync(filePath, 'utf8')

    // Generally you would parse/transform the contents
    // For example you can transform markdown to HTML here

    return {
      content: fileContents,
  // By returning { props: posts }, the Blog component
  // will receive `posts` as a prop at build time
  return {
    props: {