Salesforce-B2C-Commerce-Cloud-Developer Practice Test
202 Questions
A developerneeds to perform the same additional checks before completing multiple routes
in a custom controller, in orderto decide whether to render a template or redirect the user to
a different page.
According to SFRA best practices, what is the correct approach to improve code reusability
in this scenario?
A. Define a new middleware function and use it in the existing routes.
B. Append a new function to all the existing routes with the server module.
C. Replace the existing routes by creating a controller in separate new cartridge.
D. Use the superModule property in the existing routes to extend their functionality.
Explanation:
According to Salesforce Storefront Reference Architecture (SFRA) best practices, to perform the same additional checks before completing multiple routes in a custom controller and decide whether to render a template or redirect, the developer should A: define a new middleware function and use it in the existing routes. This approach promotes code reusability by centralizing the logic in a reusable middleware, aligning with SFRA’s modular design.
Reasoning:
SFRA Best Practices: SFRA encourages the use of middleware to handle cross-cutting concerns (e.g., authentication, validation) across routes, improving maintainability and reusability.
A. Define a new middleware function and use it in the existing routes.
Correct. The developer can create a middleware function (e.g., in cartridge/scripts/middleware/checkLogic.js) to perform the checks (e.g., user status, conditions) and decide to res.render() or res.redirect(). This middleware is then added to the relevant routes.
Example:
// cartridge/scripts/middleware/checkLogic.js
module.exports = function (req, res, next) {
if (someCondition) {
res.render('template');
} else {
res.redirect('/different-page');
}
next();
};
// cartridge/controllers/Custom.js
var server = require('server');
var checkLogic = require('~/cartridge/scripts/middleware/checkLogic');
server.get('Route1', checkLogic, function (req, res, next) { next(); });
server.get('Route2', checkLogic, function (req, res, next) { next(); });
This avoids code duplication and is easily testable.
B. Append a new function to all the existing routes with the server module.
Incorrect. Appending logic directly to each route duplicates code, violating DRY (Don’t Repeat Yourself) principles. It also doesn’t leverage SFRA’s middleware pattern, making maintenance harder.
C. Replace the existing routes by creating a controller in a separate new cartridge.
Incorrect. Creating a new cartridge to replace routes is overkill and disrupts the existing structure. It doesn’t improve reusability and adds complexity without necessity.
D. Use the superModule property in the existing routes to extend their functionality.
Incorrect. The superModule property is used to extend base cartridge controllers (e.g., from app_storefront_base), not to add reusable checks across multiple custom routes. It’s inappropriate for this scenario.
Implementation Details:
Create Middleware:
In cartridge/scripts/middleware/checkLogic.js:
module.exports = function (req, res, next) {
var user = req.currentCustomer;
if (user.authenticated && user.profile.custom.checkPassed) {
res.render('success', { message: 'Check passed' });
} else {
res.redirect('/login');
}
next();
};
Apply to Routes:
In cartridge/controllers/Custom.js:
var server = require('server');
var checkLogic = require('~/cartridge/scripts/middleware/checkLogic');
server.get('Route1', checkLogic, function (req, res, next) { next(); });
server.get('Route2', checkLogic, function (req, res, next) { next(); });
module.exports = server.exports();
Test: Verify that the middleware correctly renders or redirects based on the checks.
Reference:
Salesforce B2C Commerce Documentation: SFRA Middleware recommends middleware for reusability.
Trailhead Module: “B2C Commerce Developer” module on “Customizing Controllers” covers middleware usage.
Salesforce-B2C-Commerce-Cloud-Developer Practice-Test - Home | Previous |
Page 16 out of 202 Pages |