README
xdn-router
Overview
This package provides a router that allows the developer to declare routes their either run at the edge (for example, proxy and redirect), or defer to next.js's router. You can also create clean routes for next.js.
Configuring Routes
import { Router } from 'xdn'
new Router()
// redirect at edge
.match('/some/path/:withVar', ({ redirect }) => {
redirect('/some/other/path/{withVar}', 301)
})
// proxy the origin site
.match('/some/path/:withVar', ({ proxy }) => {
proxy('desktop', {
path: '/some/other/path/{withVar}',
})
})
// match based on header and proxy the origin site
.match({ headers: { 'xdn-device-type': 'desktop' } }, ({ proxy }) => {
proxy('desktop')
})
// vanity URL for next.js
.match('/holiday-sale', ({ render }) => {
render((req, res) => nextRenderFunction(req, res, { productId: 2 }, '/p/[productId]'))
})
// OR
.match('/holiday-sale/:productId', ({ render, params }) => {
render((req, res) => nextRenderFunction(req, res, params, '/p/[productId]'))
})
// Synthetic html response
.match('/static-html', ({ respond, responseHeaders }) => {
responseHeaders({ set: { 'Content-Type': 'text/html; charset=UTF-8' } })
respond('<html><body><h1>Hello world!</h1></body></html>', 200)
})
// fall back to next's router
.fallback(({ render }) => render(nextRenderFunction))
Creating an Outer Edge Manager config
At build time, an outer edge manager config can be derived from the router by calling router.createEdgeConfig()
.
Example
To run routes example file use any node process manager and run the file as:
$ node ./routes.example.js
This example outputs oem.example.json
with outer edge configuration and will run on http://127.0.0.1:3001