The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Read A Local File In Nextjs

TODO: Pull subtitle into page object

This is done with `getStaticProps` which happens on the server side

code_start_default_section code_end_default_section

Note that in the node docs` but here it's done with `import fs from 'fs'`

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Read A Local File In Nextjs

-- p

This is done with `getStaticProps` which happens on the server side

-- code/
-- js

export default function Page({fileAsString}) {
      return <div>{fileAsString}</div>
  }

  export async function getStaticProps(context) {
    try {
      const fileText = fs.readFileSync(
        `./pages/switch-components-via-state.js`,
        `utf8`
      )
      return {
        props: {
          fileAsString: fileText,
        },
      }
    } catch (err) {
      console.log(err)
      return {
        props: {
          fileAsString: `could not find file`,
        },
      }
    }
  }

-- /code

-- p

Note that in the <<link|node docs|https://nodejs.dev/learn/reading-files-with-nodejs) `fs` is included via `const fs = require('fs'>>` but here it's done with `import fs from 'fs'`


-- categories
-- NextJS
-- React

-- metadata
-- date: 2022-01-08 14:33:54
-- id: 23qdqnq7
-- status: draft
-- type: post
-- SCRUBBED_NEO: false
-- site: aws