Embedding dynamic challenges in pages

When you embed a dynamic challenge within a page of your web application, you can verify visitors while keeping your application's branding consistent. The challenge will run inline on your webpage instead of redirecting to an interstitial page. We recommend this approach for React and single-page applications.

Prerequisites

  1. Bot Management (pre-cache inspection)
  2. Bot Management (post-cache inspection)
  3. DDoS

Before setting up client challenges for Bot Management with pre-cache inspection (ContentGuard), you must:

  • purchase and deploy Bot Management with pre-cache inspection (ContentGuard) on each CDN service where you intend to use client challenges. Compute services are not supported.
  • configure client challenges for the bot categories or identities you want to challenge.

Adding embedded challenges

On the page where you want the challenge to appear, complete the following steps:

  1. To the page's <head>, add the following JS:

    <script src="/_fs-ch-1T1wmsGaOgGaSxcX/challenge.js" defer></script>

    HINT: As the script waits for the DOM to be ready before loading the challenge, you can use async or async defer instead of defer. For information about these attributes, check out Mozilla's documentation.

  2. In the location of the page where you want the challenge to appear, add the following div:

    <div class="fastly-challenge"></div>
  3. (Optional) Use CSS to make all challenge types visible. By default, embedded challenges don't display on the page, unless Fastly detects suspicious activity and chooses an interactive challenge (e.g., CAPTCHA challenge).

  4. (Optional) If you've deployed Bot Management with post-cache inspection, set up rules to block requests from clients that failed a challenge.

Making all challenge types visible on the page

By default, embedded challenges don't display on the page unless Fastly detects suspicious activity and chooses an interactive challenge (e.g., CAPTCHA challenge). To make all challenge types visible, use the following CSS directives:

fastly-challenge[data-challenge-status="started"]
fastly-challenge[data-challenge-status="processing"]
fastly-challenge[data-challenge-status="complete"]
fastly-challenge[data-challenge-status="error"]
fastly-challenge[data-challenge-status="captcha_prompted"]

Sample CSS

Below is a sample CSS that makes all challenge types visible on the page.

1
2
3
4
5
6
7
/* Base container styling - Pre-allocate space to prevent layout shift */
.fastly-challenge {
content:"Powered by Fastly";
display: inline-flex;
align-items: center;
min-height: 32px;
min-width: 140px;