What you'll learn
  • how to intercept the request
  • what you can do with the request at this point


In the 5.34.0 version we added a possibility for users to intercept the Fastify request in it’s initial phase. It can be done via the HandlerOnRequestPluginexternal link.

With this plugin you can do anything with the Fastify’s Requestexternal link object, even stop the request from being processed. To find out why you can stop the request from being processed, read about it in the Fastify Lifecycleexternal link and Fastify Hooksexternal link documentation.

Creating and Registering the Plugin to Intercept the Request

Of course, as with our other plugins, you must register it in the Lambda handler plugins array:


Intercepting theOPTIONSRequest and Stopping the Request From Being Processed

By default, Webiny intercepts the OPTIONS request and stop all of the Fastify lifecycles, after the onRequest, from being ran. We do this because we have no need to run all the code we usually run for, eg., POST request. You can see the code hereexternal link.

Via the HandlerOnRequestPlugin, users can prevent our default behavior.

Why Would the User Prevent the Webiny Default Behavior?

The most simple answer would be: to output their own headers in the OPTIONS request.

How Can Users Prevent the Webiny Default Behavior onOPTIONSRequest to Be Executed?

To prevent the Webiny default behavior on OPTIONS request, user would need to add a simple plugin which returns false as the result.

Here is an example of sending a custom header myCustomHeader, and prevent the Webiny default behavior, on OPTIONS request:


Simply, with the HandlerOnRequestPlugin user can, in the initial stage of the Fastify Request Lifecycle:

  • intercept the request and prevent it to go further down the lifecycle line
  • modify the Webiny default OPTIONS headers output to suit their own need
  • log the request to some outside service

Users must be careful on which path are they when executing the plugin code. Webiny has one built-in path: /graphql.