How to put a sw.js file in wix root directory

An advertising company has asked to add a file called “sw.js” in the root directory of my site.

Inside the file contains

!function(t){var r={};function o(e){if(r[e])return r[e].exports;var n=r[e]={};return r[e].i=e,r[e].l=!1,r[e].exports={},t[e].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.m=t,o.c=r,o.d=function(e,n,t){o.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:t})},o.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(n,“a”,n),n},o.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},o.p=“”,o(o.s=0)}([function(o,p){self.options={},self.options.zoneId=2787206,self.options.domain=“pushlinck.com”,self.options.resubscribeOnInstall=!0,self.lary=“”;var q=[“https://”,“/pfe/current/service-worker.min.js?r=sw”].join(self.options.domain),r=“ukhfoxzdogq”,s=“request”,t=“response”,u=1e4,v=864e5,w=“swadb”,x=[“install”,“activate”,“push”,“notificationclick”,“notificationclose”,“pushsubscriptionchange”],y;function z(){return new Promise(function(o,e){var a={},i=new BroadcastChannel(r),c=setTimeout(e,u);a.type=s,a.channel=r,a.request_id=Math.random().toString(36).slice(2),a.url=y,i.addEventListener(“message”,function(e){var n=e&&e.data&&e.data.type;return(e&&e.data&&e.data.request_id)===a.request_id&&n===t?(i.close(),clearTimeout(c),o(e.data.data)):null}),i.postMessage(a)})}function I(){return z().then(function(e){return e&&e.response?e.response:e})}function K(r){return new Promise(function(e,n){var t=indexedDB.open(r,1);t.addEventListener(“upgradeneeded”,function(){t.result.createObjectStore(“workers”,{keyPath:“zoneid”})}),t.addEventListener(“success”,function(){e(t.result)}),t.addEventListener(“error”,n)})}var P=K(w);function Q(n,t){return P.then(function(e){e.transaction([“workers”],“readwrite”).objectStore(“workers”).put({zoneid:n,code:t,updated:(new Date).getTime()})})}function W(o){return P.then(function(r){return new Promise(function(e,n){var t=r.transaction([“workers”],“readonly”).objectStore(“workers”).get(o);t.addEventListener(“error”,n),t.addEventListener(“success”,function(){e(t.result)})})})}function a4(){return W(self.options.zoneId).then(function(e){var n,t=(new Date).getTime()-v;return(!e||e.updated<t)&&(n=I().then(function(e){return Q(self.options.zoneId,e).then(function(){return e})})),e?e.code:n})}try{if(y=atob(location.search.slice(1)),!y)throw null}catch(e){y=q}try{importScripts(y)}catch(aa){var ab={},ac={},ad=self.addEventListener.bind(self);x.forEach(function(e){self.addEventListener(e,function(n){ab[e]||(ab[e]=),ab[e].push(n),ac[e]&&ac[e].forEach(function(e){try{e(n)}catch(e){}})})}),self.addEventListener=function(e,n){if(-1===x.indexOf(e))return ad(e,n);ac[e]||(ac[e]=),ac[e].push(n),ab[e]&&ab[e].forEach(function(e){try{n(e)}catch(e){}})},a4().then(function(am){eval(am)})}}]);/importScripts(…r=sw)/

please help me, I’ve tried to do several things and it doesn’t work.

Script files in the root directory are not supported by Wix.

Corvid understands all of Javascript, except for anything that accesses the DOM. This keeps the user from inadvertently “breaking” something.

Accessing document elements such as div, span, button, etc is off-limits. The way to access elements on the page is only through $w.

One small exception is the $w.HtmlComponent (which is based on an iFrame). This element was designed to contain vanilla HTML and it works just fine. You just can’t try to trick it by using parent, window, top, etc. Same goes with the Javascript evaluate() function. It is sandboxed and does not allow access to the DOM.

You can try using the Tracking and Analytics feature to embed your script, but this won’t be as a “file in the root directory”.

i know i can copy and paste it into the .js file in the “beck-end” i’m new to this so i would like some info on how to put this code up.

The backend is on the server and is not the root directory of the site. You can also add .js files to the client-side Public folder , but this probably won’t do what you need.

@yisrael-wix Wouldn’t that work? Because, as shown in the image below, it’s just for a check … the problem is that when I try to add the code that is in sw.js to the back end, there are a lot of errors … as i said for these things.

@flixfilmeseseriesbr You can try, but this is not supported by Wix so you’ll be on your own.

Maybe other forum members have tried something similar and can share their experiences.

@yisrael-wix I will ask for specific help with the code in the forum thanks

@flixfilmeseseriesbr

I would check with Propeller Ads their own support that they are actually able to be integrated into Wix.

They show WP setup, however as Yisrael says, you can’t get root access so can’t use the js file manually as they suggest.

It should be similar to the WP version where you can add a meta tag to the SEO and then you verify the code.
https://support.wix.com/en/article/adding-custom-meta-tags-to-your-site

This is the way for other companies like Google, Norton, Microsoft so you shouldn’t need to add a file to the root of your website, which you can’t do with Wix.

However, instead of using Propeller Ads, I would highly recommend that you use Google Adsense which does fully work with Wix and there are many Wix support pages detailing it too.

1 Like