Skip to content

Instantly share code, notes, and snippets.

@elmasse
Created September 23, 2017 01:57
Show Gist options
  • Save elmasse/37d705c027e31aacf9f978bf83d89a8d to your computer and use it in GitHub Desktop.
Save elmasse/37d705c027e31aacf9f978bf83d89a8d to your computer and use it in GitHub Desktop.
nextein: custom routes
const nexteinConfig = require('nextein/config').default
const stories = ['food', 'cars'].reduce( (prev, entry) => ({
...prev,
[`/${entry}`]: { page: '/stories', query: { category: `stories/${entry}` } }
}), {})
module.exports = nexteinConfig({
exportPathMap: () => ({
'/all-posts': { page: '/all-posts' },
'/sub-section': { page: '/sub-section' },
'/tags': { page: '/tags'},
...stories
})
})
// pages/stories.js
import React, { Component } from 'react'
import withPosts, { withPostsFilterBy, inCategory } from 'nextein/posts'
import { Content } from 'nextein/post'
const category = 'stories'
const withStories = withPostsFilterBy(inCategory(category, { includeSubCategories: true }))
class Stories extends Component {
static async getInitialProps ({ query }) {
if (query) {
const { category } = query
return {
selected: category
}
}
}
render() {
const { posts, selected } = this.props
const stories = posts.filter(inCategory(selected))
return (
<main>
<h1>{selected.replace(`${category}/`, ``)}</h1>
<section>
{
stories.map((story, idx) => (
<article key={`${selected}-story-${idx}`}>
<h2>{story.data.title}</h2>
<Content {...story} excerpt />
</article>
))
}
</section>
</main>
)
}
}
export default withStories(Stories)
@elmasse
Copy link
Author

elmasse commented Sep 23, 2017

Here is an example of custom routes with nextein. In this example we use the posts under stories category. This also are divided in subcategories (food & cars) The code in next.config.js creates entries for a custom route for each subcategory using a common page stories.js component. The page component also declares the getInitialProps method the is needed to determine which sub-collection will be rendered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment