Home > Web Front-end > JS Tutorial > body text

Announcing GrowthBook on JSR

Barbara Streisand
Release: 2024-11-05 07:41:02
Original
879 people have browsed it

Announcing GrowthBook on JSR

GrowthBook is committed to supporting modern platforms, bringing advanced feature flagging and experimentation to where you are. We’re excited to announce the availability of our JavaScript SDK on JSR, the modern open-source JavaScript registry. This integration empowers JavaScript and Typescript developers with a seamless experience for implementing and managing feature flags in their applications.

JSR simplifies the process of publishing and importing JavaScript modules, offering robust features like TypeScript support, auto-generated documentation, and enhanced security through provenance attestation. This collaboration brings these benefits to GrowthBook users, streamlining the integration and utilization of feature flagging in their development workflows.

Using the GrowthBook JS SDK via JSR offers an excellent developer experience, with first-class TypeScript support, auto-generated documentation accessible in your code editor, and more.

How to Install GrowthBook from JSR

Get started with GrowthBook using the deno add command:

deno add jsr:@growthbook/growthbook
Copy after login

Or using npm:

npx jsr add @growthbook/growthbook
Copy after login

The above commands will generate a deno.json file, listing all project dependencies.

{
  "imports": {
    "@growthbook/growthbook": "jsr:@growthbook/growthbook@0.1.2"
  }
}

Copy after login

deno.json

Use GrowthBook with Express

Let’s use GrowthBook with an Express server. In our main.ts file, we can write:

import express from "express";
import { GrowthBook } from "@growthbook/growthbook";

const app = express();

// Example using Express
app.use(function (req, res, next) {
  // Create a GrowthBook instance and store in the request
  req.growthbook = new GrowthBook({
    apiHost: "<https://cdn.growthbook.io>",
    clientKey: "sdk-qtIKLlwNVKxdMIA5",
  });

  // TODO: Add user targeting attributes from cookies, headers, etc.
  req.growthbook.setAttributes({
    id: req.user?.id,
  });

  // Clean up at the end of the request
  res.on("close", () => req.growthbook.destroy());

  // Wait for features to load (will be cached in-memory for future requests)
  req.growthbook.init({ timeout: 1000 }).then(() => next());
});

app.get("/", (req, res) => {
  const gb = req.growthbook;

  // Boolean on/off flag
  if (gb.isOn("my-boolean-feature")) {
    res.send("Hello, boolean-feature!");
  }

  // String/Number/JSON flag
  const value = gb.getFeatureValue("my-string-feature", "fallback");

  res.send(`Hello, ${value}!`);
});

console.log("Listening on port 8000");
app.listen(8000);

Copy after login

Finally, you can run the following command to execute:

deno -A main.ts
Copy after login

Depending on how you’ve set up your feature flags in GrowthBook (sign up for free), the response will be different:

Announcing GrowthBook on JSR

Check out our official docs to learn more about feature flags, creating and running experiments, and analyzing experiments.

What’s next?

With GrowthBook's JS SDK now on JSR, it’s even easier to bring the power of feature flags and A/B testing to any JavaScript environment.

  • Check out JSR, the JavaScript registry built for the modern web.
  • Read the announcement on Deno's blog.
  • Learn more about GrowthBook’s JS SDK.

The above is the detailed content of Announcing GrowthBook on JSR. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template