Translates redirect params into normalized version that solves backward compatibility issues and also takes into account query parameters that may be included in the query string itself.
string to where we are redirecting the request; may have query string
options with which we are redirecting the request
Adds a set-cookie
header to the response.
This does not replace any cookies with the same name - for that you should use updateResponseCookie
.
Example
new Router()
.get('/some/path', ({ addResponseCookie, proxy }) => {
proxy('origin')
addResponseCookie('my-cookie', 'my-cookie-value', { domain: 'test.com' })
})
Name of the cookie to add.
Value to set
Optional options to add to cookie
Adds a set-cookie
header to the response before attempting to cache the response.
This does not replace any cookies with the same name - for that you should use updateUpstreamResponseCookie
.
Furthermore, Edgio does not cache responses with set-cookie
so if you wish to add a cookie
after the response is cached, you should use addResponseCookie
.
Example
new Router()
.get('/some/path', ({ addUpstreamResponseCookie, proxy }) => {
proxy('origin')
addUpstreamResponseCookie('my-cookie', 'my-cookie-value', { domain: 'test.com' })
})
Name of the cookie to add.
Value to set
Optional options to add to cookie
Sends the necessary response headers to allow CORS
Example
new Router()
.match('/api/:path*', ({ allowCors }) => {
allowCors({
origin: '*', // this is the default
methods: ['get', 'post'],
headers: ['x-some-header'],
maxAge: 60 * 60, // one hour
credentials: true
})
})
Serves an HTML app shell from a static file.
The path to the app shell html file
Example
new Router()
.fallback(({ appShell }) => {
appShell('dist/index.html')
})
Sets the caching behavior for both browser and edge.
Example
new Router()
.get('/p/:productId', ({ cache, proxy }) => {
cache({
browser: {
maxAgeSeconds: 0,
serviceWorkerSeconds: 60 * 60, // 1 hour
},
edge: {
maxAgeSeconds: 60 * 60 * 24, // 24 hours
staleWhileRevalidateSeconds: 60 * 60 // 1 hour
}
})
proxy('origin')
})
The cache()
method can be called in the same route where the response is sent, or any prior route. For example,
with Next.js, it is common to use the next plugin to automatically inherit page routes based on Next.js conventions,
and use Edgio router simply to add caching:
import { Router } = from '@edgio/core/router'
import { createNextPlugin } from '@edgio/next'
const { nextMiddleware, renderNext } = createNextPlugin()
new Router()
.get('/p/:productId', ({ cache, proxy }) => {
cache({
browser: {
maxAgeSeconds: 0,
serviceWorkerSeconds: 60 * 60, // 1 hour
},
edge: {
maxAgeSeconds: 60 * 60 * 24, // 24 hours
staleWhileRevalidateSeconds: 60 * 60 // 1 hour
}
})
proxy('origin')
})
.use(nextMiddleware)
Execute the provided callback function in the cloud. The callback is passed the request and the response.
Use this method when you need to compute a response in the cloud rather than at the edge or at build time. A common example is when the response must be computed based on request parameters, headers, or cookies.
A common example is to look up the destination for a redirect from an external API:
new Router()
.get('/products/:id', ({ redirect, compute }) => {
compute(async (request, response) => {
const destination = await getDestinationFromAPI(request.params.id)
redirect(destination)
})
})
This method can be combined with cache
to compute responses in the cloud and cache them at edge:
new Router()
.get('/products/:id', ({ cache, redirect, compute }) => {
cache({
edge: {
maxAgeSeconds: 60 * 60 * 24,
staleWhileRevalidateSeconds: 60 * 60,
}
})
compute(async (request, response) => {
const destination = await getDestinationFromAPI(request.params.id)
redirect(destination)
})
})
Note: when calling compute
in combination with serveStatic
, the result of the serveStatic
must be
awaited in order to get the result from serveStatic
, otherwise only reslut of the compute
method
will be returned.
new Router()
.get('/products/:id', ({ redirect, compute }) => {
compute(async (request, response) => {
const destination = await getDestinationFromAPI(request.params.id)
await serveStatic(destination)
})
})
A function to run in the cloud to compute the response
Relays the request to the specified backend.
Example
new Router()
.get('/some/path/with/:variable', ({ proxy }) => {
proxy('legacy', { path: '/some/other/path/with/:variable' })
})
In this example, we relay the request to the "legacy" backend. In this case, edgio.config.js
must
contain a definition for the legacy
backend. For example:
// edgio.config.js
module.exports = {
backends: {
legacy: {
domainOrIp: 'legacy.domain.com',
hostHeader: 'domain.com'
}
}
}
The name of one of the backends in your edgio.config.js
file.
A promise the resolves once the response has been fetched from the upstream site.
Redirects the browser to a new location. Query params from the original request are added to the redirect URL
Examples
new Router()
.get('/p/:productId', ({ redirect }) => {
return redirect('/products/:productId', { statusCode: 301 })
})
// Extract id from route and apply as query string
new Router()
.get('/p/:productId', ({ redirect }) => {
return redirect('/product', { query: { id: ':productId' }})
})
// Extract id from query string and apply to route
new Router()
.get({ path: '/p', query: { id: ':id' } }, ({ redirect }) => {
return redirect('/product/:id')
})
The URL to which the browser will be redirected.
Removes a request header.
Example
new Router()
.get('/some/path', async ({ removeRequestHeader, proxy }) => {
removeRequestHeader('some-header')
proxy('origin')
})
The case-insensitive name of the request header
Removes a specific set-cookie
header from the response.
If you wish to remove all such headers use removeHeader
.
Example
new Router()
.get('/some/path', ({ removeResponseCookie, proxy }) => {
proxy('origin')
removeResponseCookie('my-cookie')
})
The case-insensitive name of the cookie
Removes a response header immediately before delivering the response downstream.
Example
new Router()
.get('/some/path', ({ removeResponseHeader, proxy }) => {
proxy('origin')
removeResponseHeader('some-header')
})
The case-insensitive name of the response header
Removes a specific set-cookie
header from the response before attempting to cache the response.
If you wish to remove all such headers use removeUpstreamHeader
.
Example
new Router()
.get('/some/path', ({ removeUpstreamResponseCookie, proxy }) => {
proxy('origin')
removeUpstreamResponseCookie('my-cookie')
})
The case-insensitive name of the cookie
Removes a response header before attempting to cache the response.
In local development this is same as calling updateUpstreamResponseHeader
but
on edge it removes a header immediately after the upstream fetch has finished.
Example
new Router()
.get('/some/path', ({ removeUpstreamResponseHeader, proxy }) => {
proxy('origin')
removeUpstreamResponseHeader(HTTP_HEADERS.setCookie)
})
The case-insensitive name of the response header
Calls the provided callback to send a response.
Render callback function
Renders a result in Edgio's serverless cloud using your application. Use this method to respond with an SSR or API result from your application.
Options for transforming the request sent to your app and the response received from it.
Sends content back to client. If content is a string, the respons will be sent directly from the edge. If it is a function, the request will be computed by a JavaScript worker.
Example
new Router()
.get('/some/path', ({ send }) => {
return send('<html><body>Hello World!</body></html>', 200, 'OK')
})
The response body as a string
The status to send
The status message to send
Responds with a static asset from the specified path.
Example
serveStatic('path/to/asset/from/app/root')
You can also use variables in the asset path. For example, to return files under the assets
directory
when the url starts with /static
:
new Router()
.get('/static/:path*', ({ serveStatic }) => {
serveStatic('assets/:path*')
})
Notes:
if a folder contains an 'index.html', that page is also served when requesting the parent folder route. Let's take the example of /posts/1 that will render /posts/1/index.html) They are multiple components involved in this rewrite to make it work with S3 storage:
We did not use S3 built-in Static Website feature for multiple reasons:
ISG/ISR: When handling a request for a static asset that is not in S3, the request follows this path:
Browser => Edge => S3 (requested asset, which results in a 404)
Edge => Buffer Proxy => S3 (options.loadingPage, if provided) => Response returned
=> Edge => Buffer Proxy => Lambda => BehindEdgeResponseWriter => options.onNotFound => SSR (then this response is cached for the next request)
Note that BehindEdgeResponseWriter is the key here. It overrides serveStatic and if options.onNotFound is provided, it will always be called.
The relative path to the asset from the app's root directory. You can reference path variables using :variable
.
The different options to serving static assets including fallback to compute
A promise the resolves once the asset has been fetched from storage.
Returns the service worker with proper edge and browser cache headers
The path to the service worker relative to the root directory of your app
Example
new Router()
.get('/service-worker.js', ({ serviceWorker }) => {
serviceWorker('dist/service-worker.js')
})
Adds or replaces a request header.
Example
new Router()
.get('/some/path', ({ setRequestHeader, proxy }) => {
setRequestHeader('some-header', 'some-value')
proxy('origin')
})
The case-insensitive name of the request header
The value to set
Adds or replaces a response header immediately before delivering the response downstream.
Example
new Router()
.get('/some/path', ({ setResponseHeader, proxy }) => {
proxy('origin')
setResponseHeader('some-header', 'some-value')
})
The case-insensitive name of the response header
The value to set
Adds or replaces an upstream response header before attempting to cache the response.
In local development, this is the same as calling setResponseHeader
but on edge, it adds
a header immediately after the upstream fetch has finished.
Example
new Router()
.get('/some/path', ({ setUpstreamResponseHeader, proxy }) => {
proxy('origin')
setUpstreamResponseHeader('x-0-surrogate-key', 'proxy=origin')
})
The case-insensitive name of the response header
The value to set
Rewrites the request path.
Example:
router.get('/products/:id', ({ updatePath }) => {
updatePath('/p/:id')
})
a new route path, which can include params captured from the original path
Alters a request header. Use this method to derive the new header value from the existing one.
Example
new Router()
.get('/some/path', ({ updateRequestHeader, proxy }) => {
updateRequestHeader('some-header', /some-.*-part/gi, 'some-replacement')
proxy('origin')
})
The case-insensitive name of the request header
Regex to find the part that should be replaced.
Value that will replace the matched part.
Updates a set-cookie
header in the response. If there are multiple cookies
with the same name, all of them will be updated.
Example
new Router()
.get('/some/path', ({ updateResponseCookie, proxy }) => {
proxy('origin')
updateResponseCookie('my-cookie', /Domain=.+;/, 'Domain=mydomain.com;')
})
Case-insensitive name of the cookie to update
Regex to find the part that should be replaced.
Value that will replace the matched part.
Alters a response header immediately before delivering the response downstream. Use this method to derive the new header value from the existing one.
Example
new Router()
.get('/some/path', ({ updateResponseHeader, proxy }) => {
proxy('origin')
updateResponseHeader('some-header', /some-.*-part/gi, 'some-replacement')
})
The case-insensitive name of the response header
Regex to find the part that should be replaced.
Value that will replace the matched part.
Updates a set-cookie
header in the response before attempting to cache the response.
If there are multiple cookies with the same name, all of them will be updated.
Example
new Router()
.get('/some/path', ({ updateUpstreamResponseCookie, proxy }) => {
proxy('origin')
updateUpstreamResponseCookie('my-cookie', /Domain=.+;/, 'Domain=mydomain.com;')
})
Case-insensitive name of the cookie to update
Regex to find the part that should be replaced.
Value that will replace the matched part.
Alters an upstream response header before attempting to cache the response.
Use this method to derive the new header value from the existing one.
In local development this is same as calling updateResponseHeader
but on
edge it updates a header immediately after the upstream fetch has finished.
Example
new Router()
.get('/some/path', ({ updateUpstreamResponseHeader, proxy }) => {
proxy('origin')
updateUpstreamResponseHeader('cache-control', /public/gi, 'private')
})
The case-insensitive name of the response header
Regex to find the part that should be replaced.
Value that will replace the matched part.
Generated using TypeDoc
The API that is provided to route callbacks.