Unverified Commit a7bd2db9 authored by Ronie's avatar Ronie Committed by GitHub

Example: update netlify-cms to use SSG (#12072)

parent 945c1fac
...@@ -41,5 +41,4 @@ Sites take its content from markdown files in `/content`. Two of pages (`home` a ...@@ -41,5 +41,4 @@ Sites take its content from markdown files in `/content`. Two of pages (`home` a
Blog component loads all posts (during build!) and lists them out [How to load multiple md files](https://medium.com/@shawnstern/importing-multiple-markdown-files-into-a-react-component-with-webpack-7548559fce6f) Blog component loads all posts (during build!) and lists them out [How to load multiple md files](https://medium.com/@shawnstern/importing-multiple-markdown-files-into-a-react-component-with-webpack-7548559fce6f)
Posts are separate static sites thanks to dynamically created export map. I took inspiration on how to do it from Updated to take advantange of the new `getStaticPaths` and `getStaticProps` data-fetching functions.
[here](https://medium.com/@joranquinten/for-my-own-website-i-used-next-js-725678e65b09)
const fs = require('fs')
const blogPostsFolder = './content/blogPosts'
const getPathsForPosts = () =>
fs.readdirSync(blogPostsFolder).reduce((acc, blogName) => {
const trimmedName = blogName.substring(0, blogName.length - 3)
return Object.assign(acc, {
[`/blog/post/${trimmedName}`]: {
page: '/blog/post/[slug]',
query: {
slug: trimmedName,
},
},
})
}, {})
module.exports = { module.exports = {
webpack: configuration => { webpack: configuration => {
configuration.module.rules.push({ configuration.module.rules.push({
...@@ -22,10 +6,4 @@ module.exports = { ...@@ -22,10 +6,4 @@ module.exports = {
}) })
return configuration return configuration
}, },
async exportPathMap(defaultPathMap) {
return {
...defaultPathMap,
...getPathsForPosts(),
}
},
} }
...@@ -40,9 +40,14 @@ const Blog = ({ postsList }) => ( ...@@ -40,9 +40,14 @@ const Blog = ({ postsList }) => (
</Layout> </Layout>
) )
Blog.getInitialProps = async () => { export async function getStaticProps() {
const postsList = await importBlogPosts() const postsList = await importBlogPosts()
return { postsList }
return {
props: {
postsList,
}, // will be passed to the page component as props
}
} }
export default Blog export default Blog
import fs from 'fs'
import path from 'path'
import Layout from '../../../components/layout' import Layout from '../../../components/layout'
const Post = ({ blogpost }) => { const Post = ({ blogpost }) => {
if (!blogpost) return <div>not found</div> if (!blogpost) return <div>not found</div>
const { html, attributes } = blogpost.default const { html, attributes } = blogpost
return ( return (
<Layout> <Layout>
...@@ -24,12 +26,34 @@ const Post = ({ blogpost }) => { ...@@ -24,12 +26,34 @@ const Post = ({ blogpost }) => {
) )
} }
Post.getInitialProps = async ({ query }) => { export async function getStaticPaths() {
const { slug } = query const paths = fs
.readdirSync(path.join(process.cwd(), 'content/blogPosts'))
.map(blogName => {
const trimmedName = blogName.substring(0, blogName.length - 3)
return {
params: { slug: trimmedName },
}
})
return {
paths,
fallback: false, // constrols wheter not predefined paths should be processed on demand, check for more info: https://nextjs.org/docs/basic-features/data-fetching#the-fallback-key-required
}
}
export async function getStaticProps({ params }) {
const { slug } = params
const blogpost = await import(`../../../content/blogPosts/${slug}.md`).catch( const blogpost = await import(`../../../content/blogPosts/${slug}.md`).catch(
() => null () => null
) )
return { blogpost }
return {
props: {
blogpost: blogpost.default,
},
}
} }
export default Post export default Post
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment