Service Workers & Concrete5

Permalink
hi community

i've a problem with my service worker. actually it works fine, everything saved BUT once service worker is active the bar in the backend, to amend the website is gone. because the path of the site is saved in the service worker:

...
var precacheConfig = [
   ['/index.php', "23122018-007"],
   ['/', "23122018-007"],
   ['/about-us', "23122018-007"],
   ['/service', "23122018-007"],
   ['/references', "23122018-007"],
...


is there any way to somehow exclude that once some one is logged in as admin?

many thanks!

 
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Maybe something like this
<?php 
$u = new User();
if (!$u->isSuperUser()) {
// Your code goes here
} ?>
eivissa89 replied on at Permalink Reply
thank you for your message!

i am not entirely sure, where i put this?
everything inside of this php command?

thank yoU!
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
If this is working for you, please mark my answer as 'Best Answer'
eivissa89 replied on at Permalink Reply
i did not say it works?!

i asked where i have to put this snippet? it's not tested yet?!

where does this snippet go?
mnakalay replied on at Permalink Reply
mnakalay
<?php 
$u = new User();
if (!$u->isSuperUser()) {
// Your code goes here
?>
    ...
    var precacheConfig = [
    ['/index.php', "23122018-007"],
    ['/', "23122018-007"],
    ['/about-us', "23122018-007"],
    ['/service', "23122018-007"],
    ['/references', "23122018-007"],
    ...
<?php
} ?>

This code should be in a PHP file of course.
eivissa89 replied on at Permalink Reply
thanks for you reply!

but you know that the service worker file is JS?

how should that work?
mnakalay replied on at Permalink Reply
mnakalay
I guessed as much which is why I made clear that was PHP code.

You have several possibilities.
1- put only the triggering code in your PHP like I showed you and leave the rest in the JS file
2- Using the code I showed you set a JS variable in your PHP with a value of true or false and read it from your JS
3- Your JS file is loaded from PHP so maybe you can simply use the code above to decide to load the whole file or not.
4- In your JS use a little Ajax call to a PHP script that will simply check the user and return true or false
5- Since your problem is with the toolbar it might work if you check for the class "ccm-toolbar-visible" on the HTML element.

That last possibility comes with plenty of caveats. First I don't know if the class will even be added when your workers are in place. Second, since that class name is added by JS, you'll have to wait for the whole document to be ready to check for it.

Any way you look at it, you probably need some refactoring of your code and except for the last solution that might or might not work, you'll need some PHP.
eivissa89 replied on at Permalink Reply
hi mnakalay

thank you very much for you help!
and sorry for asking again but i am pretty unsure what to do... :)

one solution is to just wrap the
<script src="<?php echo $view->getThemePath() ?>/js/serviceWorkerInstall.js"></script>


with you php? right?

with ajax or loading js in a php file i have absolutely no clue... :-S
mnakalay replied on at Permalink Reply
mnakalay
You are correct
<?php 
$u = new User();
if (!$u->isSuperUser()) {
?>
<script src="<?php echo $view->getThemePath() ?>/js/serviceWorkerInstall.js"></script>
<?php
} ?>
eivissa89 replied on at Permalink Reply
doesn't work unfortunately... :(

isn't there a command where you can say "do this only / or do not do this" once you are logged in?
mnakalay replied on at Permalink Reply
mnakalay
yes. instead of
!$u->isSuperUser()

use
!$u->isLoggedIn()
eivissa89 replied on at Permalink Reply
many thanks again!

unfortunately still not...
i tried this:
<?php
$u = new User();
if (!$u->isLoggedIn()) {
    ?>
    <script>
        navigator.serviceWorker.getRegistrations().then(function(registrations) {
            for(let registration of registrations) {
                registration.unregister()
            } })
    </script>
    <?php
} ?>


so once the admin is logged in, the service worker will be unregistered but it doesn't work. only when i enter the script directly in the console of chrome... but then again, i log out and the backend bar stays - although without any functionality... maybe c5 is not made for serviceworkers...
mnakalay replied on at Permalink Best Answer Reply
mnakalay
This has nothing to do with Concrete5 being meant for service workers or not. It's just a CMS you can do whatever you want with it and I know websites using service workers with it successfully.

So let's take it from the start. Your code is:
<?php
$u = new User();
if (!$u->isLoggedIn()) {
    ?>
    <script>
        navigator.serviceWorker.getRegistrations().then(function(registrations) {
            for(let registration of registrations) {
                registration.unregister()
            } })
    </script>
    <?php
} ?>

Which means if the user is NOT logged in then run that JS code and unregister the service worker.

So when you say "once the admin is logged in, the service worker will be unregistered but it doesn't work" it is correct. If you're the admin, you are logged in so that code doesn't run at all.

At first my understanding was that you wanted to LOAD the script only for users who are NOT logged in.

Now I understand that if the user IS logged in, you want to run that piece of code that will unregister your service workers. And if the user is NOT logged in (normal users) then do NOT run that piece of code.

Do this
<?php
$u = new User();
if ($u->isLoggedIn()) {
    ?>
    <script>
        navigator.serviceWorker.getRegistrations().then(function(registrations) {
            for(let registration of registrations) {
                registration.unregister()
            } })
    </script>
    <?php
} ?>

Notice I simply removed the ! before $u->isLoggedIn(). Now, if a user IS logged in the code will run If a user is NOT logged in it will not run.

This is for any logged in user.

If you want to do that only for the SuperUser (admin) you would use $u->isSuperUser() instead of $u->isLoggedIn()
eivissa89 replied on at Permalink Reply
many thanks mnakalay!

it still doesn't work but at least i see how it works.

i do not doubt concrete5 but it would be interesting to see how other solved this issued.
eivissa89 replied on at Permalink Reply
anyone else who has an idea how to handle service worker?
i am also willing to spend some money for a good solution with sw.

many thanks!
mnakalay replied on at Permalink Reply
mnakalay
You should post in the job forum then to make it official :)
eivissa89 replied on at Permalink Reply