Onesignal push service in a FREE infinityfree account

Website URL

https://avisgavi.infinityfreeapp.com

Error Message

Error: importScripts is not defined

Other Information

When I connect the Home Page, then everything works OK. But when I load a different page, EVEN AT THE SAME LEVEL OF THE HOME PAGE, I get the error above. Using the RESPONSE tab of Chrome Tools for Developers, loading any page but the Home Page causes Infinityfree to return an unknown HTML/JS chunk of code instead of the Service Worker content, like a test for cookies/JS capabilities of the caller. QUESTION: is there a way to force Infinityfree to avoid such behaviour, and always return the Service Worker code? All site pages are legitimated to retrieve it, to have Onesignal push service available throughout the site.

Beep boop, I’m a bot! I dug through the forum to get you started. I sometimes get things wrong though, so do with this what you will until a human has a look.

No, there’s no way to force it. The security system is mandatory on free accounts and can’t be disabled. The HTML/JS chunk you’re seeing sounds like its browser check, which is the page that asks for JavaScript and cookies before it lets a request through.

The knowledge base article on it says the check needs a client that can run JavaScript and accept cookies. Anything that isn’t a normal page load in a browser can get that check page instead of the file, and the article lists things like APIs, apps and PWAs as examples. It doesn’t mention service workers or OneSignal by name. So I can’t confirm that’s what is happening to your service worker file, but your description of the response fits it.

The article’s suggested way around it is premium hosting, which uses less restrictive protection. It also says that same-domain requests from a page in the browser work fine.

Browser Security System - Features and Limitations

I couldn’t find a topic where someone got OneSignal’s service worker working on a free account. I can’t promise a workaround exists.

I understand the Infinityfree policy, but why everything appears to work when connecting the site Home Page? It should underground the same policy! Instead, the service subscription offer correctly appears, why?

Just trying to understand the problem before I suggest anything.

Is the problem occurring in browser? If so can you provide a link to a specific page where the problem occurs?

If it’s happening when an external tool, bot, or website tries to access your site then it won’t work. Since that’s what the security system is designed to block

Good afternoon Dan, and thanks for your interest. You can try this:

https://avisgavi.infinityfreeapp.com

https://avisgavi.infinityfreeapp.com/sitemap.php

Then, open the RESPONSE tab and check what is returned when getting the Service Worker file (OneSignalSDKWorker.js):

In the first case, you should see a single line (ImportScripts('. . . . ');

In the second, you should see a totally-different chunk of code.

Note that pages are at the same level. Seems they are treated differently…

I’m not at my pc at the moment so I’ll look later if no-one else has been able to help first

Neither am I. See you soon!!

The script at https://avisgavi.infinityfreeapp.com/OneSignalSDKWorker.js returns the same content on both pages, which is the importScripts("..."); code. So I don’t see any “totally different chunk of code”. And it wouldn’t make sense to see that, the same Javascript code included in two different pages does not contain two different contents. That’s not how browsers and servers work.

When I look at the generated HTML code on the home page and the sitemap, I notice one key different: this code is present on the sitemap page, but not on the home page:

<!-- PHP: insertServiceWorker() (oneSignal.php) -->

<script type="application/javascript" charset="utf-8" src="https://avisgavi.infinityfreeapp.com/OneSignalSDKWorker.js">
</script>

My guess is that this code is what triggers the error. The OneSignalSDKWorker.js is just broken, and the importScripts() function doesn’t seem to be defined anywhere, but you don’t see that on the home page because this code is not included there.

The comment suggests that this content is assembled with PHP, so I’m going to guess and say that there is some conditional code that only adds this code to some pages, which is why you see this error only on some pages.

That’s not the root cause of the error though. The error is that your code calls a non-existent function. Or at least, a function that’s not included in the page before this code is loaded.

Sorry Gentlemen, my fault! I equivocated content of different OneSignal-related files (such as OneSignalSDKWorker.js and OneSignalSDKWorker.page.js), the content is obviously the same in all site pages.

The error was cause by the presence of the following code (in all but the Home Page):

<script type="application/javascript" charset="utf-8" src="https://avisgavi.infinityfreeapp.com/OneSignalSDKWorker.js">
</script>

LESSON LEARNED: Service Worker .js files must be dynamically registrated in all pages, BUT NOT included as any other .js script. They contain code normally not accessible, and this is the reason of:

Error: ImportScripts() is not defined