The Jamstack Book by Brian Rinaldi & Raymond K. Camden
Author:Brian Rinaldi & Raymond K. Camden [Brian Rinaldi]
Language: eng
Format: epub
Publisher: Manning Publications
Published: 2022-05-17T00:00:00+00:00
gray-matterâOur Markdown files will contain front matter metadata. This library will allow us to easily parse that metadata.
react-markdownâAs weâve discussed, React applications use a virtual DOM, and this library will render the Markdown within Reactâs virtual DOM, meaning that React will be able to properly update only the changed DOM elements.
If youâre still running your local site, youâll need to stop it first. Next, install all three libraries:
yarn add raw-loader gray-matter react-markdown
Letâs use raw-loader first. To do this, we need to edit the Webpack configuration of our Next.js site. Webpack is a popular module bundler for building web apps that Next.js uses to bundle its JavaScript files for the browser. To edit the Webpack configuration, open next.config.js and add a new rule that looks for files with the .md extension and loads them with the raw-loader.
Listing 5.10 The Next.js configuration file at next.config.js to load Markdown files
module.exports = { env: { CHEC_PUBLIC_KEY: process.env.CHEC_PUBLIC_KEY, }, webpack: function (config) { config.module.rules.push({ test: /\.md$/, use: 'raw-loader', }); return config; }, };
Create a new file at /pages/about.js for our About page. Within the getStaticProps() method, we can use the filesystem to load the raw Markdown file from /content/ about.md. Weâll then use the gray-matter library to read raw Markdown and separate the front matter metadata from the content. Weâll pass the front matter and Markdown content as props to the page, where ReactMarkdown is used to render the Markdown as React components.
Listing 5.11 The About page that loads Markdown content at /pages/about.js
import fs from 'fs'; import Nav from '../components/nav'; import matter from 'gray-matter'; import ReactMarkdown from 'react-markdown'; export default function About({ frontmatter, content }) { return ( <div> <Nav /> <div className="content container px-5 py-24 mx-auto"> <h1>{frontmatter.title}</h1> <ReactMarkdown children={content} /> </div> </div> ); } export async function getStaticProps() { const file = fs.readFileSync(`${process.cwd()}/content/about.md`, 'utf8'); const data = matter(file); return { props: { frontmatter: data.data, content: data.content, }, }; }
As you may have guessed from the code, before this page will work, we need to create a Markdown file at /content/about.md. Placing content files in a /content folder is a typical structure of Next.js sites that load Markdown content. My about.md file is pretty simple, but feel free to experiment by adding more Markdown markup of your own:
--- title: About the Jam Store --- The Jam Store is built with: * Next.js * Commerce.js.
Letâs restart our local site using yarn dev and see our About page by clicking on the About navigation item (figure 5.11).
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(25499)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(18218)
Shot Through the Heart by Mercy Celeste(18159)
The Fifty Shades Trilogy & Grey by E L James(17774)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(13189)
The Subtle Art of Not Giving a F*ck by Mark Manson(12904)
Scorched Earth by Nick Kyme(11831)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(11036)
Drei Generationen auf dem Jakobsweg by Stein Pia(10216)
Suna by Ziefle Pia(10185)
Scythe by Neal Shusterman(9259)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(8608)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(8418)
This is Going to Hurt by Adam Kay(7693)
Dirty Filthy Fix: A Fixed Trilogy Novella by Laurelin Paige(6450)
He Loves Me...KNOT by RC Boldt(5804)
How to Make Love to a Negro Without Getting Tired by Dany LaFerrière(5377)
Interdimensional Brothel by F4U(5303)
Thankful For Her by Alexa Riley(5158)