<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[8963] sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins: Service Worker Caching: Initial commit.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/8963">8963</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/8963","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-06-19 18:55:06 +0000 (Wed, 19 Jun 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Service Worker Caching: Initial commit.
Props avillegasn, Iceable, westonruter, iandunn.
See https://make.wordpress.org/community/2019/04/30/wordcamp-pwa-an-update/
See https://github.com/wceu/wordcamp-pwa-page/</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentmupluginsloadothermupluginsphp">sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/load-other-mu-plugins.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentmupluginsserviceworkercachingphp">sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/service-worker-caching.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentmupluginsloadothermupluginsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/load-other-mu-plugins.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/load-other-mu-plugins.php 2019-06-19 05:10:56 UTC (rev 8962)
+++ sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/load-other-mu-plugins.php 2019-06-19 18:55:06 UTC (rev 8963)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4,6 +4,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wcorg_include_individual_mu_plugins();
</span><span class="cx" style="display: block; padding: 0 10px"> wcorg_include_mu_plugin_folders();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+enable_pwa_alpha_test(); // Do _not_ comment out this line to disable this, see instructions in docblock instead.
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Load individually-targeted files
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,3 +50,69 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/*
+ * Enable the day-of-event and PWA features on alpha test sites.
+ *
+ * If something goes seriously wrong during WCEU2019, the following should disable the features and restore
+ * stability:
+ *
+ * 1) Comment out the `2019.europe` ID inside `$pwa_test_sites`.
+ * 2) Uncomment the `2019.europe` ID in the `rest_pre_dispatch` callback below.
+ * 3) Deploy the above changes.
+ * 4) Deactivate the `pwa` plugin on `2019.europe`.
+ * 5) Visit https://2019.europe.wordcamp.org/wp-admin/options-reading.php and set the homepage to "Your latest posts".
+ *
+ * If that doesn't work, ask Systems for details about what's causing problems, and work with them to resolve it.
+ * They may need to block REST API requests to `2019.europe` at the network level. Also, call Ian's cell phone until
+ * he picks up :)
+ *
+ * todo When enabling this for all sites, most of this function can be removed, and the `require_once()` statements
+ * can be integrated into `wcorg_include_individual_mu_plugins()`, and then this function can be removed.
+ */
+function enable_pwa_alpha_test() {
+ /*
+ * These are experimental features that were rushed to activate before the WCEU 2019 deadline, and are _not_
+ * ready for production beyond this limited alpha test. There are many UX and code issues that need to be
+ * resolved before any other sites are added to this list, including WCEU 2020.
+ *
+ * See https://github.com/wceu/wordcamp-pwa-page/issues/6, the other open issues in that repository, and the
+ * `todo` notes in all of these files.
+ *
+ * When adding more sites here, you'll need to activate the `pwa` plugin for that site first.
+ *
+ * WARNING: Do _not_ add more sites, or network-enable this, until the issues mentioned above are resolved.
+ */
+ $pwa_test_sites = array(
+ 928, // 2017.testing
+ // todo enable after testing on 2017.testing production -- 1026, // 2019.europe
+ );
+
+ if ( 'development' === WORDCAMP_ENVIRONMENT || in_array( get_current_blog_id(), $pwa_test_sites, true ) ) {
+ require_once( __DIR__ . '/service-worker-caching.php' );
+ // require_once( __DIR__ . '/theme-templates/bootstrap.php' );
+ }
+
+ /**
+ * Disable the REST API if the server is overloaded.
+ *
+ * This is a kill switch for the REST API, because even after disabling the `require()` calls above and the
+ * `pwa` plugin, browsers will still be making requests to the API. This will short-circuit those, so that
+ * the server isn't overloaded.
+ */
+ add_action( 'rest_pre_dispatch', function( $result, $server, $request ) {
+ $overloaded_sites = array(
+ //1026, // 2019.europe
+ );
+
+ if ( in_array( get_current_blog_id(), $overloaded_sites, true ) ) {
+ $result = new WP_Error(
+ 'api_temporarily_disabled',
+ 'The REST API has been temporarily disabled on this site because of unexpected stability problems',
+ array( 'status' => 503 )
+ );
+ }
+
+ return $result;
+ }, 10, 3 );
+}
</ins></span></pre></div>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentmupluginsserviceworkercachingphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/service-worker-caching.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/service-worker-caching.php (rev 0)
+++ sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/service-worker-caching.php 2019-06-19 18:55:06 UTC (rev 8963)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,114 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordCamp\PWA\Caching;
+use WP_Service_Worker_Caching_Routes;
+
+add_action( 'wp_front_service_worker', __NAMESPACE__ . '\register_caching_routes' );
+ // todo is this the most appropriate hook to register these?
+ // seemed to have problems when using `default_service_workers`, like infinite loop.
+ // But do caching routes always affect both workers regardless? Or should they?
+ // Even if the caching is restricted to front end worker, that would still affect Tagregator and others,
+ // so still need to worry about side-effects.
+add_action( 'wp_front_service_worker', __NAMESPACE__ . '\set_navigation_caching_strategy' );
+
+
+/**
+ * Register caching routes with both service workers.
+ */
+function register_caching_routes() {
+ /*
+ * todo
+ *
+ * Is the wp-content/includes caching route even working? Didn't cache assets for offline template.
+ * How can you tell if it's working, compared to regular browser caching?
+ * devtools Network panel should say "From service worker" for size?
+ * Is this the most appropriate way to make the day-of template performant?
+ * Should an eTag be used in addition to -- or instead of -- this?
+ * See https://github.com/wceu/wordcamp-pwa-page/issues/6#issuecomment-499366120
+ * Are the expiration periods here appropriate for all consumers of these resources?
+ * What side effects does this introduce, if any?
+ * Will cachebuster params in asset URLs still work?
+ * Will Gutenberg, Tagregator, etc receive outdated responses to their GET requests?
+ * If so that would fundamentally break them, and this needs to be fixed.
+ * If not, then the reason should be documented here, because it's not obvious.
+ * Gutenberg probably ok as long as only registering caching route w/ front-end service worker.
+ * For tagregator, maybe should only cache the specific endpoints that the day-of-event template calls?
+ * Or maybe cache all routes, but then add an extra route that caches Tagregator less often?
+ * All of this needs to be tested to verify that it's working as intended.
+ * What's the best way to do that? Document it here if it's not obvious.
+ */
+
+ wp_register_service_worker_caching_route(
+ '/wp-(content|includes)/.*\.(?:png|gif|jpg|jpeg|svg|webp|css|js)(\?.*)?$',
+ [
+ 'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST,
+ 'cacheName' => 'assets',
+ 'plugins' => [
+ 'expiration' => [
+ 'maxEntries' => 60,
+ 'maxAgeSeconds' => DAY_IN_SECONDS,
+ ],
+ ],
+ ]
+ );
+
+ wp_register_service_worker_caching_route(
+ '/wp-json/.*',
+ [
+ 'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST,
+ 'cacheName' => 'rest-api',
+ 'plugins' => [
+ 'expiration' => [
+ 'maxEntries' => 60,
+ 'maxAgeSeconds' => 15 * MINUTE_IN_SECONDS,
+ ],
+ ],
+ ]
+ );
+}
+
+/**
+ * Set the navigation preload strategy for the front end service worker.
+ * todo is ^ the best explanation of what's going on here?
+ */
+function set_navigation_caching_strategy() {
+ /*
+ * todo
+ *
+ * All of this needs to be understood deeper in order to know if it's the right way to achieve the goals
+ * of this project, and what the unintended side-effects may be.
+ *
+ * See https://developers.google.com/web/updates/2017/02/navigation-preload
+ *
+ * Should navigation preloading really be disabled? Maybe it's good to disable it since we're not using it?
+ * But then why is it enabled by default? Maybe the `pwa` plugin is using it?
+ *
+ * We need to clearly document what's going on here, and _why_.
+ *
+ * Are the chosen caching strategies and parameters appropriate in this context?
+ *
+ * Are there side-effects beyond the day-of template? If so, how should they be addressed?
+ *
+ * All of this needs to be tested to verify that it's working as intended.
+ * What's the best way to do that? Document it here if it's not obvious.
+ */
+
+ add_filter( 'wp_service_worker_navigation_preload', '__return_false' );
+
+ add_filter(
+ 'wp_service_worker_navigation_caching_strategy',
+ function() {
+ return WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST;
+ }
+ );
+
+ add_filter(
+ 'wp_service_worker_navigation_caching_strategy_args',
+ function( $args ) {
+ $args['cacheName'] = 'pages';
+ $args['plugins']['expiration']['maxEntries'] = 50;
+
+ return $args;
+ }
+ );
+};
</ins></span></pre>
</div>
</div>
</body>
</html>