-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Errors with service worker js #155
Comments
@mohamedhamad thanks a lot for sharing the URL for your site. There is indeed a JS syntax error in the service worker which is preventing it from installing. This is the service worker JS response: https://www.mohamed-hamad.com/?wp_service_worker=1 I'll look into what is going on. Have you registered any custom service worker logic? |
hey @westonruter, thanks for having a look at things. |
For reference, this is the syntax error being emitted from the document: This is the line in question: @mohamedhamad do you by chance have any optimizer plugin running which strips out all HTML comments from PHP responses? |
That appears to be the case. If so, try a patch like this (or I can open a PR) which masks the presence of the HTML comments: diff --git a/wp-includes/js/service-worker-navigation-routing.js b/wp-includes/js/service-worker-navigation-routing.js
index 3231a0d..a0c7be3 100644
--- a/wp-includes/js/service-worker-navigation-routing.js
+++ b/wp-includes/js/service-worker-navigation-routing.js
@@ -65,9 +65,9 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL
headers: errorResponse.headers
};
- let body = text.replace( /<!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.error );
+ let body = text.replace( /[<]!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.error );
body = body.replace(
- /(<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->)((?:.|\n)+?)(<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->)/,
+ /([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->)((?:.|\n)+?)([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->)/,
( details ) => {
if ( ! responseBody ) {
return ''; // Remove the details from the document entirely.
@@ -86,8 +86,8 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL
details = details.replace( '{{{iframe_srcdoc}}}', srcdoc );
// Replace the comments.
- details = details.replace( '<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->', '' );
- details = details.replace( '<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->', '' );
+ details = details.replace( '<' + '!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->', '' );
+ details = details.replace( '<' + '!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->', '' );
return details;
}
);
@@ -111,7 +111,7 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL
headers: response.headers
};
- const body = text.replace( /<!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.default );
+ const body = text.replace( /[<]!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.default );
return new Response( body, init );
} );
diff --git a/wp-includes/js/service-worker-offline-commenting.js b/wp-includes/js/service-worker-offline-commenting.js
index 59bea59..6d4fbe9 100644
--- a/wp-includes/js/service-worker-offline-commenting.js
+++ b/wp-includes/js/service-worker-offline-commenting.js
@@ -29,9 +29,9 @@
headers: errorResponse.headers
};
- let body = text.replace( /<!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.error );
+ let body = text.replace( /[<]!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.error );
body = body.replace(
- /(<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->)((?:.|\n)+?)(<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->)/,
+ /([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->)((?:.|\n)+?)([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->)/,
( details ) => {
if ( ! errorText ) {
return ''; // Remove the details from the document entirely.
@@ -50,8 +50,8 @@
details = details.replace( '{{{iframe_srcdoc}}}', srcdoc );
// Replace the comments.
- details = details.replace( '<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->', '' );
- details = details.replace( '<!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->', '' );
+ details = details.replace( '<' + '!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN--' + '>', '' );
+ details = details.replace( '<' + '!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END--' + '>', '' );
return details;
}
);
@@ -93,7 +93,7 @@
headers: response.headers
};
- const body = text.replace( /<!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.comment );
+ const body = text.replace( /[<]!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.comment );
return new Response( body, init );
} ); |
@westonruter yes I do have a plugin to optimize the site and does strip out html comments. |
ok, so disabling the html comment stripping in the Asset Cleanup plugin seems to have worked. I cleared the cache and tested in the chrome lighthouse audit extension and I'm getting all checks validated.
|
@mohamedhamad I've noticed that sometimes after clearing caches. If you close DevTools and reload and re-open, that should go away. |
The Asset Cleanup plugin should really be checking the |
amazing! thank you @westonruter |
@mohamedhamad One thing is @amedina and I gave a talk in part about this plugin at CDS last year: https://youtu.be/s1WrBaAyzAI Otherwise, there is the API reference in the readme. See also this comment: #113 (comment) The plugin is really looking for the overall patterns to emerge for how to leverage across the various WordPress use cases, so it's hard to pin down any extensive tutorial about how to use. One important resource to get familiar with is Workbox.js, since the plugin here is in big part a wrapper around the functionality it provides. There are some great tutorials there which give you a feel for how to build logic for the service worker which you should be able to map easily into the PWA plugin (e.g. using the PHP API). |
awesome, thanks for the pointers @westonruter |
@mohamedhamad Would you please re-enable the HTML comment stripping and test #165 to see if it works around the problem? |
just tried it out, the updated file is still up there if you wanna have a look. similar issue as before |
@mohamedhamad I'm not seeing any JS errors like before. Seems to be working. |
@westonruter I’ve been toggling the HTML comment stripping on n off to test on different browsers. It’s definitely the same issue. Will turn it on again n leave it for a while for you to see |
There is no JS error for me. And when looking at https://www.mohamed-hamad.com/?wp_service_worker=1 there is no syntax error. Likewise, I can see the expected code: let body = text.replace( /[<]!--WP_SERVICE_WORKER_ERROR_MESSAGE-->/, errorMessages.error );
body = body.replace(
/([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->)((?:.|\n)+?)([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->)/,
( details ) => {
if ( ! responseBody ) {
return ''; // Remove the details from the document entirely.
}
const src = 'data:text/html;base64,' + btoa( responseBody ); // The errorText encoded as a text/html data URL.
const srcdoc = responseBody
.replace( /&/g, '&' )
.replace( /'/g, ''' )
.replace( /"/g, '"' )
.replace( /</g, '<' )
.replace( />/g, '>' );
const iframe = `<iframe style="width:100%" src="${src}" data-srcdoc="${srcdoc}"></iframe>`;
details = details.replace( '{{{error_details_iframe}}}', iframe );
// The following are in case the user wants to include the <iframe> in the template.
details = details.replace( '{{{iframe_src}}}', src );
details = details.replace( '{{{iframe_srcdoc}}}', srcdoc );
// Replace the comments.
details = details.replace( '<' + '!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->', '' );
details = details.replace( '<' + '!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->', '' );
return details;
}
);
return new Response( body, init ); |
I definitely see it still. I cleared my cache. restarted the browser. on first load its fine. but when I navigate around, I see a console error and an error in the application tab of chrome developer tools |
Strange. I just opened your site in an incognito window and now I see it too. Are you sure you deployed the changes from the PR? I'm not seeing expected code. In particular, I'm seeing: // Replace the comments.
details = details.replace( '', '' );
details = details.replace( '', '' ); But in my PR, I changed the strings around to use concatenation and so I'd expect to see |
Yea, I pulled the file and uploaded directly to the server.
|
@mohamedhamad It looks like you did replace the one file, but there is another file that also had changes. Please update both |
oops, apologies, my bad. updated the second file and it looks good. |
@mohamedhamad great, I'm not seeing any JS errors. If you don't either, I'll go ahead and merge the PR. |
did a site wide test, no errors. I think its good to go |
I'm having a hard time getting service workers to be registered and work. Seems like in the chrome console log there is an a js error in the generated service work
and
and when using Lighthouse extension in chrome, I get the following errors
on my local dev environment, I get non of these issues and its a very similar install. the one thing that does happen in my local dev environment is if the plugin is installed and I add the theme support, the page continually refreshes as the service works are working and updating non-stop.
I'm not really using many of the functions as I'm experimenting.
if anyone is so inclined, you can check out the lighthouse results for the site I'm working on
https://www.mohamed-hamad.com/
I'm also testing with https://www.pwabuilder.com, and their tool picks up everything but the service worker.
is there a tutorial on how to get the basics in, or an article that goes through the options and how to set it up?
The text was updated successfully, but these errors were encountered: