How to add service worker file to root directory

You can’t access the root directory in Wix.

You will have to look at using a new custom tool in Tracking and Analytics section.
https://support.wix.com/en/article/embedding-custom-code-to-your-site

Also, note that as this is third party provided code, that Wix won’t provide support for it and you will need to be going through the third party providers own support for more help with this.
https://support.wix.com/en/article/using-iframes-to-display-visible-content-on-your-site
Wix cannot provide support for external codes that were not created or tested with Wix.
If you are experiencing an issue with your code snippet, please contact the provider directly.