Middleware
Middleware is used to alter the behavior of a request both before and after Outsmartly makes the request to your origin server and applies any overrides, if applicable.
The signature of a middleware is a function which accepts two arguments: an OutsmartlyMiddlewareEvent and a next() function. The OutsmartlyMiddlewareEvent contains additional information such as the OutsmartlyRequest object, OutsmartlyEdgeVisitor, helpers for cookies, and more.
Middleware should call next() whenever they want to continue on to the "next" middleware, or if it's the last middleware, continue to the default, built-in functionality of Outsmartly.
// a function which accepts an event object and a next() function.
type Middleware = (
event: OutsmartlyMiddlewareEvent,
next: (request?: Request) => Promise<Response>,
) => PromiseOrValue<Response>;
type PromiseOrValue<T> = Promise<T> | T;This pattern gives you the most flexibility: you can do things before, and after the default behavior. Not only that, you can also pass a different Request object when you call next(request) which then changes what request will actually be made.
The simplest middleware that does nothing (no-op) looks like this:
function exampleMiddleware(event, next) {
return next();
}Middleware can be provided in you outsmartly.config.js in two places: at the top-level (applying all routes) or alternatively in a route itself (applying only paths that match.)
export default {
host: 'example.outsmartly.app',
environments: [
{
name: 'production',
origin: 'https://my-example-website.vercel.app',
},
],
// All routes will have this middleware applied
middleware: [
function firstMiddleware(event, next) {
return next();
},
],
routes: [
{
// Any paths that start with /some-base-path/ will have
// this middleware applied.
path: '/some-base-path/*',
middleware: [
function secondMiddleware(event, next) {
return next();
},
],
},
],
};But that's not a very exciting piece of middleware. So let's see how we might add headers both to the request AND the response:
You don't HAVE to call next(), but if you don't, remember that then Outsmartly will not make any requests to your origin or apply overrides to any HTML.
Examples
Set a cookie
Sometimes you want to set a cookie from the edge/server so that it can be an httpOnly cookie, which makes it inaccessible from client-side JavaScript in the browser (better security) and also is much more likely to survive longer without the browser or extensions deleting it.
Often this is inside an interceptor, but if you need to do this from middleware, it is also possible. Here's an example where we set a cookie the first time you land on a product page, so we can later tell to do things like personalization/recommendations.
Redirects
In this example, for request paths that start with /app/ we check if they are authorized, and if not, we redirect them to the /login page.
This demonstrates the using the request's path from event.url.pathname, along with getting a cookie named 'session' by using the event.cookies utility.
Last updated
Was this helpful?