Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add pagination to blog #93

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ exports.createPages = async ({ graphql, actions }) => {
fromPath: `/chat`,
toPath: `https://discord.gg/KZRDXmTm9v`,
})

createRedirect({
fromPath: `/blog/1/`,
toPath: `/blog/`,
})

// Create blog-list pages
const blogPostListPage = path.resolve('./src/templates/blog-post-list.js');
const blogPosts = allMarkdown.data.allMarkdownRemark.edges.filter(({ node }) => node.fields.slug.includes('blog/'));
const blogPostsPerPage = 3
const numPages = Math.ceil(blogPosts.length / blogPostsPerPage)

Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: blogPostListPage,
context: {
limit: blogPostsPerPage,
skip: i * blogPostsPerPage,
numPages,
currentPage: i + 1,
},
})
})
};

function pad(n) {
Expand Down
32 changes: 29 additions & 3 deletions src/pages/blog.js → src/templates/blog-post-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import MetaInfo from '../components/meta-info';
import Page from '../components/page';
import Lead from '../components/lead';
import theme from '../theme';
import Button from '../components/button';

function Blog({ data }) {
function Blog({ data, pageContext }) {
const { currentPage, limit, numPages, skip } = pageContext
const isFirst = currentPage === 1
const isLast = currentPage === numPages
const prevPage = currentPage - 1 === 1 ? "" : (currentPage - 1).toString()
const nextPage = (currentPage + 1).toString()
return (
<Layout>
<Helmet>
Expand All @@ -36,7 +42,25 @@ function Blog({ data }) {
<Lead>{node.frontmatter.description}</Lead>
</article>
))}
{/* TODO: pagination */}
<div css={css`
display: flex;
justify-content: space-between;
`}>
{!isFirst && (
<Button href={`/blog/${prevPage}`} rel="prev" className='primary' css={css`
margin: ${theme.space[4]} 0;
`}>
← Previous Page
</Button>
)}
{!isLast && (
<Button href={`/blog/${nextPage}`} rel="next" className='primary' css={css`
margin: ${theme.space[4]} 0;
`}>
Next Page →
</Button>
)}
</div>
</Container>
</Page>
</Layout>
Expand All @@ -46,10 +70,12 @@ function Blog({ data }) {
export default Blog;

export const pageQuery = graphql`
query blogList {
query blogList ($skip: Int!, $limit: Int!) {
allMarkdownRemark(
filter: { fields: { slug: { regex: "/blog/" } } }
sort: { frontmatter: { date: DESC } }
limit: $limit
skip: $skip
) {
edges {
node {
Expand Down