Overview
Core library for the Layer0, infrastructure to run your big, dynamic website frontend and make it load in less than one second.
The @layer0/core
package provides a CDN-as-JavaScript API for defining edge routing and logic on Layer0.
Example Routes File
import { Router } from '@layer0/core/router'
import { createNextPlugin } = from '@layer0/next'
import { renderNextPage, nextRoutes } from '@layer0/next'
export default new Router()
.match('/some/path/:withVar', async ({ redirect }) => {
redirect('/some/other/path/:withVar', { statusCode: 301 })
})
.match('/some/path/:withVar', async ({ proxy }) => {
proxy('legacy', {
path: '/some/other/path/:withVar',
})
})
.match({ headers: { 'layer0-device-type': /desktop/ } }, async ({ proxy }) => {
proxy('legacy')
})
.match('/some/vanity/url/:productId', res => {
renderNextPage('/p/[productId]', res)
})
.match('/static-html', ({ setResponseHeader, send }) => {
setResponseHeader('Content-Type', 'text/html; charset=UTF-8')
send('<html><body><h1>Hello world!</h1></body></html>', 200)
})
.use(nextRoutes)
.fallback(({ proxy }) => proxy('legacy'))