# DataDome Service Worker Plugin

## Overview

This package overrides the global `fetch` function to hook requests and display challenges for blocked requests.

## Installation

### Prerequisites

- Have the [JS Tag](https://docs.datadome.co/docs/javascript-tag) set up on your app

### Step 1: Install the Package

Install the package using the command:

```bash
npm i @datadome/service-worker-plugin
```

### Step 2: Import the Plugin in Your Service Worker

Import the package into your service worker script.

#### For ES Modules

```javascript
import '@datadome/service-worker-plugin';
```

#### For CommonJS

```javascript
require('@datadome/service-worker-plugin');
```

### Step 3: Configure the JS Tag

To enable the JS Tag to listen to the service worker, update its configuration to include the `enableServiceWorkerPlugin` option:

```html
<script>
  window.ddjskey = 'YOUR_DATADOME_JS_KEY';
  window.ddoptions = {
      enableServiceWorkerPlugin: true,
  };
</script>
```

## How it works

The `fetch` override is applied when the service worker is activated.
The interception of requests and responses will be enabled as soon as the service worker is installed and ready to handle fetch events.
