re: Create a blog with Markdown & Sapper VIEW POST


Any suggestion for pagination? Its one of most basic features of a Blog.


That is true, pagination is important.

Here's one way to do it:

1) Group posts into pages/chunks inside src/posts.js

// 10 posts per page
export const pages = _.chunk(posts, 10)

2) Inside routes/index.svelte, define a preload function that looks at the query params:

<script context="module">
  import {pages} from '../posts'

  export function preload(page) {
    const index = +(page.query.page || 1)

    return {
      posts: pages[index-1],
      hasMore: pages.length > index + 1,
      page: index

  export let posts, hasMore, page

3) Add conditional next & previous links

{#if page > 1}
  <a href="/?page={page-1}">Previous</a>

{#if hasMore}
  <a href="/?page={page+1}">Next</a>

Hope that helps!

code of conduct - report abuse