How to use - Palmabit-IT/Sacajawea GitHub Wiki
How to use
1. Install
npm install @palmabit/sacajawea --save
routes.js
inside your project:
2. Create const { Routes } = require('@palmabit/sacajawea')
const routes = new Routes({
locale: 'en',
forceLocale: true,
siteUrl: 'https://www.example.com'
})
routes
.add('index', 'en', '/')
.add('index', 'it', '/')
.add('about', 'en', '/about')
.add('about', 'it', '/chi-siamo')
.add('news', 'en','/news/:slug', 'news-detail')
.add('news', 'it','/notizia/:slug', 'news-detail')
.add('contacts', 'en','/contacts', 'contacts-page', {email:'[email protected]'})
.add('contacts', 'it','/contattaci', 'contact-page', {email:'[email protected]'})
// also with object
.add({
name: 'pages',
locale: 'it',
pattern: '/(.*)/:slug',
page: 'dynamic-page',
data: {},
update: true
})
module.exports = routes;
3. Create a custom server
Follow official Next.js guide: https://nextjs.org/docs/#custom-server-and-routing
server.js
file
4. Set routes handler on // server.js
const next = require('next')
const routes = require('./routes')
const app = next({dev: process.env.NODE_ENV !== 'production'})
const handler = routes.getRequestHandler(app)
// With express -- RECOMMENDED
const express = require('express')
app.prepare().then(() => {
express().use(handler).listen(3000)
})
// Without express
const {createServer} = require('http')
app.prepare().then(() => {
createServer(handler).listen(3000)
})
5. Now create your page react component
If you route have match parameter on URL, all data is merged info query
. Available inside getInitialProps
function
RequestHandler automatically sets the local inside of req.locale
export default class News extends React.Component {
static async getInitialProps ({query, req, res}) {
// query.slug
// req.locale
}
render () {
// this.props.url.query.slug
}
}