GitHub

Feature flags

The JavaScript calls one module, tutorial/Greeting, and the server decides which implementation that is: a variant of a feature flag or an A/B test for the user, or none at all. See Modules the server chooses in the docs.
  • defineCond($module, $target)

    The browser requires $target whenever $module is required or called.

  • defineCond($module, null)

    Turns the module off: its calls do nothing.

  • ifRequired()

    Treats a module that is turned off as missing and runs the fallback.

  • Bootloader

    When the variants are bootloadable, only the chosen one is downloaded.

Controller.php
class FeatureFlagController extends Controller
{
    public function greet(Request $request)
    {
        // 'tutorial/GreetingClassic', 'tutorial/GreetingNext' or null (off)
        $variant = $this->variantOf($request->user());

        return (new AsyncResponse())
            ->defineCond('tutorial/Greeting', $variant)
            ->call('tutorial/Greeting', 'greet', [
                TransportMarker::element('greeting'), 'Ada',
            ])
            ->send();
    }
}

// JavaScript: a fallback when the server turned it off
ifRequired(
    'tutorial/Greeting',
    Greeting => console.log(Greeting.variant),
    () => console.log('The greeting is off'),
);
Try it
Nothing ran yet.

tutorial/Greeting is not defined yet.

One module name, the variant the server chose

Pick a variant: the server keeps it in your session. Say hello: the response chooses the module for tutorial/Greeting and calls it, and the status shows which one ran. Off: the call does nothing and ifRequired() runs its fallback.