Skip to content

Commit b496c37

Browse files
committed
Configure saved-site export API deployment
1 parent a39cc68 commit b496c37

5 files changed

Lines changed: 33 additions & 18 deletions

File tree

packages/playground/remote/service-worker.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,22 @@
5252
* While this strategy enables fast load times and an offline experience, it also
5353
* creates a substantial challenge.
5454
*
55-
* When a new Playground version is deployed, all the clients will load an old
56-
* version of the `remote.html` file on their next visit. Unfortunately, that old
57-
* `remote.html` file contains hardcoded references to assets that may not be
58-
* cached and no longer exist in the new webapp build.
55+
* When a new Playground version is deployed, clients may load an old entry
56+
* document such as `remote.html` or `api.html`. That document contains
57+
* hardcoded references to assets that may no longer exist in the new build.
5958
*
60-
* To solve this problem, we use the **Network first** strategy when `remote.html`
61-
* is requested. This introduces a small network overhead, but it guarantees loading
62-
* the most recent version of `remote.html` and all the referenced assets.
59+
* To solve this problem, we use the **Network first** strategy for entry
60+
* documents. This introduces a small network overhead, but guarantees loading
61+
* the most recent document and all its referenced assets.
6362
*
6463
* Similarly, we use the **Network first** strategy for the `/` path. This is
6564
* useful in situations where the user didn't visit Playground in a while,
6665
* they have a stale version of the `/` route cached, and they open Playground.
6766
* If we loaded the cached version, they'd see the old Playground website on their
6867
* first visit and then the new Playground website only on their second visit.
6968
*
70-
* There's still a small window of time between loading the remote.html file and
71-
* fetching the new assets when a new deployment would break the application.
69+
* There's still a small window between loading an entry document and fetching
70+
* its assets when a new deployment would break the application.
7271
* This should be very rare, but when it happens we provide an error message asking
7372
* the user to reload the page.
7473
*
@@ -365,7 +364,8 @@ self.addEventListener('fetch', (event) => {
365364
}
366365

367366
/**
368-
* Always fetch the fresh version of `/remote.html` and `/` from the network.
367+
* Always fetch fresh versions of `/remote.html`, `/api.html`, and `/` from
368+
* the network.
369369
*
370370
* This is the secret sauce that enables seamless upgrades of the
371371
* running Playground clients when a new version is deployed on
@@ -374,13 +374,14 @@ self.addEventListener('fetch', (event) => {
374374
* ## The problem with deployments
375375
*
376376
* App deployments remove all the static assets associated with the
377-
* previous app version. Meanwhile, the remote.html file we've cached
378-
* for offline usage still holds references to those assets.
377+
* previous app version. Meanwhile, cached entry documents still hold
378+
* references to those assets.
379379
*
380-
* If we just loaded the cached remote.html file, the site would crash
380+
* If we just loaded a cached entry document, the client would crash
381381
* with seemingly random errors.
382382
*
383-
* Instead, we fetch the most recent version of remote.html from the network.
383+
* Instead, we fetch the most recent version of each entry document from
384+
* the network.
384385
* It references the static assets that are now available on the server and
385386
* should work just fine.
386387
*
@@ -392,7 +393,11 @@ self.addEventListener('fetch', (event) => {
392393
* https://github.com/WordPress/wordpress-playground/issues/1821 for more
393394
* details.
394395
*/
395-
if (url.pathname === '/remote.html' || url.pathname === '/') {
396+
if (
397+
url.pathname === '/remote.html' ||
398+
url.pathname === '/api.html' ||
399+
url.pathname === '/'
400+
) {
396401
event.respondWith(networkFirstFetch(event.request));
397402
return;
398403
}

packages/playground/website-deployment/custom-redirects-lib.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ function playground_get_custom_response_headers( $requested_path ) {
414414
);
415415
} elseif (
416416
'/' === $requested_path ||
417-
'/index.html' === $requested_path
417+
'/index.html' === $requested_path ||
418+
'/api.html' === $requested_path
418419
) {
419420
return array( 'Cache-Control: max-age=0, no-cache, no-store, must-revalidate' );
420421
} elseif (

packages/playground/website-deployment/tests.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ function () {
9292
'My WordPress relay endpoint should not be edge cached'
9393
);
9494

95+
$api_headers = playground_get_custom_response_headers( '/api.html' );
96+
assert_equal(
97+
true,
98+
in_array( 'Cache-Control: max-age=0, no-cache, no-store, must-revalidate', $api_headers, true ),
99+
'Playground API entry point should not be edge cached'
100+
);
101+
95102
$mywp_event_server_snapshot = $_SERVER;
96103

97104
$_SERVER['HTTP_HOST'] = 'my.wordpress.net';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Options +Multiviews
22
AddEncoding x-gzip .gz
33

4-
<FilesMatch "index\.html">
4+
<FilesMatch "index\.html|api\.html">
55
Header unset ETag
66
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
77
</FilesMatch>
88
<FilesMatch "index\.js|blueprint-schema\.json|logger.php|wp-cli.phar|wordpress-importer.zip|php-code-snippet\.js">
99
Header set Access-Control-Allow-Origin "*"
1010
Header unset ETag
1111
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
12-
</FilesMatch>
12+
</FilesMatch>

packages/vite-extensions/vite-list-assets-required-for-offline-mode.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ const patternsToNotCache = [
7878
/^\/assets\/optional\/.*/, // All optional assets (CodeMirror, language extensions, etc.)
7979
/^\/assets\/extensions\/.*/, // All extension assets (Intl, ICU, etc.)
8080
/^\/client\/.*/, // Client package files arent't used by the web version of Playground
81+
'/api.html', // The external API endpoint is not required by the website.
82+
/^\/assets\/api-.*\.js$/, // The API entry chunk is loaded on demand with api.html.
8183
'/php-playground.html', // The PHP playground is a separate page that is not part of the web version of Playground
8284
];
8385

0 commit comments

Comments
 (0)