Salesforce-B2C-Commerce-Cloud-Developer Practice Test

Salesforce Spring 25 Release
202 Questions

Given the requirement to add caching toan existing page while adhering to SFRA best practices, which code snippet should be used?

A. Option A

B. Option B

C. Option C

A.   Option A

Explanation:

To add caching to an existing page while adhering to Salesforce Storefront Reference Architecture (SFRA) best practices, the developer should use Option A. This snippet (server.get('Show', cache.applyDefaultCache, function (req, res, next) { // code });) correctly applies the cache.applyDefaultCache middleware before the route handler. This ensures that the page is cached according to the default cache settings defined in the cache.config.js file, following SFRA's middleware-based caching approach for optimal performance and maintainability.

Reasoning:
SFRA Caching Best Practices: In SFRA, caching is implemented using middleware (e.g., cache.applyDefaultCache) within the route configuration. This middleware handles cache settings (e.g., duration, type) and applies them to the response, ensuring consistent caching across pages.

Option A:
server.get('Show', cache.applyDefaultCache, function (req, res, next) { // code }); This follows SFRA best practices by using cache.applyDefaultCache as middleware before the route handler. The middleware applies the default cache settings (e.g., defined in cartridge/scripts/middleware/cache.config.js) and proceeds to the next function if caching is applicable. This is the recommended way to enable caching for a route like Show.

Option B:
< iscache type = " relative " hours = " 24 " > < / iscache >
This is an ISML tag used to set caching headers within a template, not a route-level configuration. It’s not the correct approach for adding caching to a page route in SFRA, as it applies only to the rendered output and doesn’t leverage the middleware pattern.

Option C:
server.get('Show', function (req, res, next) { // code }).applyDefaultCache();
This is incorrect because applyDefaultCache() is not a method that can be chained after the route handler in SFRA. The middleware must be included as part of the route definition (before the handler) to ensure proper caching behavior.

Implementation Details for Option A:

Configure Cache Settings:
Ensure the cache settings are defined in cartridge/scripts/middleware/cache.config.js. For example:
module.exports = {
applyDefaultCache: function (req, res, next) {
res.cachePeriod = '24h'; // Cache for 24 hours
next();
}
};

Update Route:
In cartridge/controllers/Show.js or the main routes.js, add the caching middleware:
var server = require('server');
var cache = require('~/cartridge/scripts/middleware/cache');
server.get('Show', cache.applyDefaultCache, function (req, res, next) {
// Page logic here
res.render('show');
next();
});

Test Caching:
Verify that the Show page is cached for the specified duration (e.g., 24 hours) using browser developer tools or Business Manager’s cache settings.

Reference:
Salesforce B2C Commerce Documentation: SFRA Caching recommends using middleware for caching.
Trailhead Module: “B2C Commerce Developer” module on “Performance and Scalability” covers SFRA caching best practices.

Exam Tip:
For the Salesforce B2C-Commerce-Developer exam, understand SFRA’s middleware-based caching approach using cache.applyDefaultCache. Recognize that ISML tags (Option B) are for template-level caching, not route-level, and avoid incorrect chaining (Option C). Focus on proper route configuration syntax.

Salesforce-B2C-Commerce-Cloud-Developer Practice-Test - Home Previous
Page 39 out of 202 Pages