Cert++
Close

Route lifecycle events such as route:BeforeComplete

Key Takeaway

Save user-submitted data, such as form data, in a route:BeforeComplete listener. The event fires after all middleware steps have run but before route:Complete renders the response.

In SFRA, the server module runs each controller route as a chain of steps and emits named events as the chain moves forward. You can subscribe to these route lifecycle events, such as route:BeforeComplete, to run code at a fixed point in the request without rewriting the route. When you know the order of the events, you know where work such as saving form data belongs.

How a route runs

Every SFRA controller requires the server module. The server module registers routes and emits an event at every step of running them.

A route is a chain of functions. In server.get or server.post, the route name comes first and the main function comes last. Any middleware functions sit in between. Each step must call next() at its end, or the next function in the chain doesn't run.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given a requirement, create and extend the functionality of a JavaScript controller that leverages models, decorators, factories, or helpers following API best practices, and renders a template or returns a JSON response

0/11

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers. 796 members and growing.