# Registering a custom Service Worker

### Uploading the Service Worker to a folder of your website

Some CMS don't allow you to upload a file directly at the root of your website. In this case, you can upload the file to a folder of your website.

#### Upload the service worker

Let's say you can only upload the `batchsdk-worker-loader.js` file to the *media* folder of your website, the path to this file will be: `https://mywebsite.com/media/batchsdk-worker-loader.js.`

The server needs to get a success response with GET and OPTIONS methods.

The Service-Worker-Allowed header with the value " /" has to be served. Otherwise, you will need to add it.

You can test it as below, from the terminal, with [httpie](https://github.com/httpie/httpie) for example (test with the service worker path).

![](/files/1iIYum3F9adI9vnk3MeC)

Register the service worker

You need to manually register the service worker with your custom path and give the returned promise to the [JavaScript tag](/developer/sdk/web/getting-started/javascript-snippet.md)'s setup object as following:

```javascript
  const registrationPromise = navigator.serviceWorker.register("/media/batchsdk-worker-loader.js");
  batchSDK('setup', {
    ...,
    // Service worker related configuration
    serviceWorker: {
      // Set it to `false` to prevent Batch from registering its own service worker
      automaticallyRegister: false,
      // A promise that resolves the service worker registration with your custom path 
      registration: registrationPromise 
    }
  });
```

### Registering a Service Worker in a Sub-Scope

You may want to register a Service Worker in a sub-scope that does not control the current page.

To do that, you need to manually register the service worker with your sub-scope and prevent Batch from registering its own by updating the [JavaScript tag](/developer/sdk/web/getting-started/javascript-snippet.md)'s setup object as follows:

```javascript

  const subScopeRegistration = navigator.serviceWorker.register("batchsdk-worker-loader.js",
    { scope: "your_scope" });

  batchSDK('setup', {
    ...,
    // Service worker related configuration
    serviceWorker: {
      // Time Batch will wait for your service worker to be ready
      waitTimeout: 10,
      // Set it to `false` to prevent Batch from registering its own service worker
      automaticallyRegister: false,
      // A promise that resolves the service worker registration 
      registration: subScopeRegistration
    }
  });
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.batch.com/developer/sdk/web/advanced/declare-path-service-worker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
