Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created April 14, 2023 12:42
Show Gist options
  • Save bennadel/604665d9b6eca03703e876175c559a58 to your computer and use it in GitHub Desktop.
Save bennadel/604665d9b6eca03703e876175c559a58 to your computer and use it in GitHub Desktop.
Building Resilient Feature Flags That "Fail Open" In ColdFusion
<cfscript>
apiResponse = serializeJson({
// ... truncated ....
featureFlags: {
canSeeDuplicate: true // This feature is now ENABLED for this user!
}
});
</cfscript>
<cfscript>
apiResponse = serializeJson({
// ... truncated ....
featureFlags: {
// ... the concept of this flag has been removed from the server ...
}
});
</cfscript>
// If the feature flag is TRUE(thy), show the duplicate button.
if ( response.featureFlags.canSeeDuplicate ) {
contactDuplicate.style.display = "block";
}
// Show the feature any time the flag is not EXPLICITLY FALSE!
if ( response.featureFlags.canSeeDuplicate !== false ) {
contactDuplicate.style.display = "block";
}
<cfscript>
apiResponse = serializeJson({
featureFlags: {
// 2023-04-14: This feature no longer exists in the application.
// Leaving this response hard-coded so that long-running client
// code doesn't accidentally turn something on. We can delete this
// line in a few months after we know that all clients have likely
// refreshed their client-side bundles.
canSeeDuplicate: false
}
});
</cfscript>
<cfscript>
// IMAGINE: This is an API response to a request from the Single-Page Application.
apiResponse = serializeJson({
contact: {
id: 4,
name: "Jo Jo Bananas",
email: "jojo.b@example.com",
role: "Admin"
},
// As part of the response, we are going to include some feature flags that
// determine which features are enabled / visible in the client-side interface.
// Imagine that we are in the middle of building a "duplication" feature. And, we
// only want to show the option to "duplicate" if this feature flag is enabled.
featureFlags: {
canSeeDuplicate: false // User cannot see this feature... yet!
}
});
</cfscript>
<!--- ------------------------------------------------------------------------------ --->
<!--- ------------------------------------------------------------------------------ --->
<!--- Imagine that this is a ColdFusion Server / Single-Page Application (SPA) --->
<!--- divide. And, that the following code is part of a LONG RUNNING PROCESS. --->
<!--- ------------------------------------------------------------------------------ --->
<!--- ------------------------------------------------------------------------------ --->
<!doctype html>
<html lang="en">
<head>
<title>
Single-Page Application (SPA)
</title>
<link rel="stylesheet" type="text/css" href="./main.css" />
</head>
<body>
<h1 id="contactName">
{{ name }}
</h1>
<p id="contactEmail">
{{ email }}
</p>
<p id="contactRole">
{{ role }}
</p>
<p class="tools">
<a href="./edit.cfm">
Edit Contact
</a>
<a href="./delete.cfm">
Delete Contact
</a>
<!---
Note that the "duplicate" feature DEFAULTS to being HIDDEN (display: none).
It will only be turned-on if the feature flag is enabled.
--->
<a id="contactDuplicate" href="./duplicate.cfm" style=" display: none ; ">
Duplicate Contact
</a>
</p>
<!--- IMAGINE: Controller for the Single-Page Application (SPA) view. --->
<script type="text/javascript">
(function applyApiResponse( response ) {
// Reconcile the view with the API response.
contactName.textContent = response.contact.name;
contactEmail.textContent = response.contact.email;
contactRole.textContent = response.contact.role;
// If the feature flag is TRUE(thy), show the duplicate button.
if ( response.featureFlags.canSeeDuplicate ) {
contactDuplicate.style.display = "block";
}
})(JSON.parse( "<cfoutput>#encodeForJavaScript( apiResponse )#</cfoutput>" ));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment