Reviewing a storefront controller for best-practice violations
Key Takeaway
Set page caching in the controller with a cache decorator or response.setExpires(). An endpoint with no cache time is never stored in the page cache, so Reports & Dashboards shows a 100 percent cache miss rate for it.
A storefront controller is a server-side JavaScript module that handles a shopper request, such as Product-Show, and builds the response. Reviewing a controller for best practices means checking how it loads code, reads request data, handles errors and sets caching. How a controller makes these choices decides how fast the site is and how well it scales.
How a controller is built
Controllers are server-side scripts that handle storefront requests. A controller creates ViewModels to build each response. You write controllers in JavaScript and B2C Commerce script, and each controller must follow the CommonJS module standard. A controller file uses a .js or .ds extension and must sit in the controllers folder at the top level of the cartridge.
A controller function can handle storefront requests only if it is explicitly made public. The SFRA example below defines a route named World.
server.get("World", cache.applyDefaultCache, function (req, res, next) {
res.render("hello/world");
next();
});
module.exports = server.exports();The handler renders the response with res.render and then calls next(). The module ends with module.exports = server.exports(). The request URL uses the controller-function format, such as Hello-World for the World function of a Hello controller.
Some system names are reserved and can't be used for custom controllers or their functions.