-
Notifications
You must be signed in to change notification settings - Fork 298
Cache Playground assets to enable offline support #1535
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
134 commits
Select commit
Hold shift + click to select a range
6d9e165
ZIP WP assets
bgrgicak 75bc2bb
Add afterWordPressInstall hook to boot
bgrgicak 6c2b0ad
Download assets after WordPress is installed
bgrgicak 57daebb
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak 42be4fc
Use getWordPressVersionFromPhp
bgrgicak be0aa0e
Add offline testing instructions
bgrgicak 19da646
Add fetch caching
bgrgicak 1170ab5
Fix manifests
bgrgicak 9df228d
Remove caching from remote
bgrgicak fd8dc13
PHP-wasm caching TODO clean up code
bgrgicak 8beee0c
Remove bootWordPress hook
bgrgicak 6cf5fef
Use wordPressSiteUrl
bgrgicak 1f2347e
Stream static asset response instead of awaiting
bgrgicak f1e2784
Use request handler to get document root
bgrgicak dbb43aa
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak 1ec9d62
Preload worker
bgrgicak df506d1
Prevent worker update error when offline
bgrgicak 3c98912
Add warning if assets weren't downloaded
bgrgicak e703eff
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak 2e3910c
Async fetch
bgrgicak d2ca9c2
Update error messages
bgrgicak 4a1f8d0
Use wpVersionToStaticAssetsDirectory
bgrgicak efb4b57
Use getLoadedWordPressVersion
bgrgicak b2ba22b
downloadWordPressAssets on load
bgrgicak 1aad0d0
Remove logs
bgrgicak db8c25f
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak d7d10fb
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak de6e819
Preload assets when offline
bgrgicak 2ac8e60
Update progress bar
bgrgicak 782d222
Add cache versioning
bgrgicak c34fd37
Don't assume that referrer is a valid url
bgrgicak 5e4d336
Remove cache full check
bgrgicak aa68518
Precache all assets
bgrgicak e5993f8
Add missing resources to precache
bgrgicak 149cd34
Fix linter issue
bgrgicak d0254b8
Preload remote assets
bgrgicak 67c3893
Move cache to one file
bgrgicak de206a1
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak 265088c
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak a6f20bd
Empty remote asset paths after done instead of deleting it, update co…
bgrgicak 06c4a2f
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak 42e2913
Linter errors
bgrgicak e568b9e
Use cache version
bgrgicak 2b09fad
Update method name to backfillStaticFilesRemovedFromMinifiedBuild
bgrgicak d4dd04c
Move cache file generation to plugin
bgrgicak ebfa224
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak 44c2d8e
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak 8ecc3ad
Add preload paths TODO static asset path
bgrgicak 37c0b91
Update packages/playground/remote/src/lib/worker-thread.ts
bgrgicak e1961cc
Fix static asset regex
bgrgicak ee044ae
Linter fixes
bgrgicak a01cb3b
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak f8dab44
Build static assets
bgrgicak ff10c52
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak 5a6b574
Linter fix
bgrgicak 6f48337
Use unzipFile
bgrgicak d34f066
Fix dependencies
bgrgicak cad7fb0
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak ba75171
Use request as cache key and ignoreSearch when matching
bgrgicak b9ec8c3
Send startup options as message
bgrgicak 1feb41a
Update static asset zips
bgrgicak 03b0138
Merge branch 'add/wp-static-asset-zip' into add/fetch-caching
bgrgicak a2a8d86
Load service worker after startup options are sent
bgrgicak f3fe08d
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak cca0db5
Linter fixes
bgrgicak 966f7bf
Generate a list assets to cache for offline support
bgrgicak eba73a6
Merge branch 'add/vite-cache-paths-plugin' into add/fetch-caching
bgrgicak 14f3eb1
Send startup options as message
bgrgicak 24f2eeb
Load service worker after startup options are sent
bgrgicak 1182acb
Merge branch 'update/set-startup-options-with-messages' into add/fetc…
bgrgicak 15717aa
Linter fix
bgrgicak 83d4c5d
Merge branch 'update/set-startup-options-with-messages' into add/fetc…
bgrgicak da7fad9
Remove comment
bgrgicak 7c69b09
Await for params in worker-utils
bgrgicak 9aff520
Merge branch 'update/set-startup-options-with-messages' into add/fetc…
bgrgicak 97d95f3
Set web worker startup options with messages instead of query strings…
bgrgicak f0378d0
Remove unused zip delete
bgrgicak 7c205fa
Update remote asset PR comment
bgrgicak e03c9ad
Merge branch 'trunk' into add/wp-static-asset-zip
bgrgicak 9f2d128
Merge branch 'add/wp-static-asset-zip' into add/vite-cache-paths-plugin
bgrgicak 9f1ad7c
Remove unused manifest
bgrgicak 7c886d4
Remove worker url params
bgrgicak e23cbb8
Merge branch 'trunk' into add/vite-cache-paths-plugin
bgrgicak 946a6d7
Merge branch 'add/vite-cache-paths-plugin' into add/fetch-caching
bgrgicak a746657
Update filter to exclude files instead include
bgrgicak 33dc880
Merge branch 'add/vite-cache-paths-plugin' into add/fetch-caching
bgrgicak 2567903
Apply suggestions from code review
bgrgicak 29f7868
Revert "Set web worker startup options with messages instead of query…
bgrgicak bdb6d41
Reset spawnPHPWorkerThread
bgrgicak 194f7e9
Update patternsToNotCache list and document why files are excluded
bgrgicak da4b2d1
Apply suggestions from code review
bgrgicak 0746125
Rename plugin and make file name configurable
bgrgicak 2110676
Add distDirectoriesToList
bgrgicak 83499b5
Rename to absoluteDirPath
bgrgicak 9657274
Merge branch 'trunk' into add/vite-cache-paths-plugin
bgrgicak 733ab4a
Merge branch 'add/vite-cache-paths-plugin' into add/fetch-caching
bgrgicak 8f858d1
Remove backfill code
bgrgicak e7d7f81
Fix small bugs
bgrgicak c50cc9e
Merge branch 'trunk' into add/fetch-caching
bgrgicak b37f835
Merge branch 'trunk' into add/fetch-caching
bgrgicak d5b8198
Update asset list
bgrgicak 973e88d
Remove manifest changes
bgrgicak 34b481c
Fix manifest paths
bgrgicak 4adaffa
Fix dev server manifest
bgrgicak 7eb35d7
Add development mode to wasm-wordpress-net build
bgrgicak 6fecfc1
Merge branch 'trunk' into fix/manifest-json-domains
bgrgicak f1e34ec
Move to a plugin
bgrgicak 038982f
Update readme
bgrgicak b321514
Get url from env variable
bgrgicak 1245978
Reset project.json
bgrgicak ec9d917
Run generateManifestJson only when needed
bgrgicak 61d4d23
Update docblock
bgrgicak 13ee2f0
Update packages/vite-extensions/vite-manifest.ts
bgrgicak 582b722
Merge branch 'trunk' into fix/manifest-json-domains
bgrgicak a6a1555
Merge branch 'fix/manifest-json-domains' into add/fetch-caching
bgrgicak ef533dc
Update README.md
bgrgicak fbe1c65
Move caching to Remote
bgrgicak cb3e05c
Remove cacheVersion
bgrgicak ec6a38d
Update packages/vite-extensions/vite-manifest.ts
bgrgicak 7e82e58
Document caching
bgrgicak b316de4
Update README.md
bgrgicak ac95c5e
Merge branch 'fix/manifest-json-domains' into add/fetch-caching
bgrgicak 353522c
Merge branch 'trunk' into add/fetch-caching
bgrgicak 5eee778
Merge branch 'trunk' into add/fetch-caching
bgrgicak b888b97
Cache the current hostname
bgrgicak a6462e0
Merge branch 'trunk' into add/fetch-caching
bgrgicak c8796d6
Rename method to shouldCacheUrl
bgrgicak f0d00e8
Merge branch 'trunk' into add/fetch-caching
bgrgicak 8003609
Rename OfflineModeCache class and update warning
bgrgicak 60c8c60
Fix sw update warning
bgrgicak c4533d9
Swallow the registration.update() error, add a few comments
adamziel 77a813f
Address feedback
bgrgicak 26abb6b
Support offline mode after the first Playground page load (#1643)
adamziel fa8c800
Merge branch 'trunk' into add/fetch-caching
bgrgicak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/php-wasm/web-service-worker/src/worker-caching.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// import { logger } from '@php-wasm/logger'; | ||
import { isURLScoped } from '@php-wasm/scopes'; | ||
|
||
const wpCacheKey = 'playground-cache'; | ||
|
||
const isCacheFull = async () => { | ||
if (navigator.storage && navigator.storage.estimate) { | ||
const estimatedStorage = await navigator.storage.estimate(); | ||
if (estimatedStorage.usage && estimatedStorage.quota) { | ||
return estimatedStorage.usage >= estimatedStorage.quota; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
export const addCache = async (key: string, response: Response) => { | ||
if (await isCacheFull()) { | ||
bgrgicak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.warn('Cache is full, not caching response'); | ||
return; | ||
} | ||
try { | ||
const clonedResponse = response.clone(); | ||
const cache = await caches.open(wpCacheKey); | ||
await cache.put(key, clonedResponse); | ||
} catch (e) { | ||
console.warn('Failed to cache response', e); | ||
} | ||
}; | ||
|
||
export const getCache = async (key: string) => { | ||
const cache = caches.open(wpCacheKey); | ||
return await cache.then((c) => c.match(key)); | ||
}; | ||
|
||
const validHostnames = ['playground.wordpress.net', '127.0.0.1', 'localhost']; | ||
bgrgicak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const isValidHostname = (url: URL) => { | ||
/** | ||
* The development environment uses Vite which doesn't work offline because it dynamically generates assets. | ||
* Check the README for offline development instructions. | ||
*/ | ||
if ( | ||
url.href.startsWith('http://127.0.0.1:5400/') || | ||
url.pathname.startsWith('/website-server/') | ||
) { | ||
return false; | ||
} | ||
if (isURLScoped(url)) { | ||
return false; | ||
} | ||
return validHostnames.includes(url.hostname); | ||
}; | ||
|
||
export const cachedFetch = async (request: Request): Promise<Response> => { | ||
const url = new URL(request.url); | ||
if (!isValidHostname(url)) { | ||
try { | ||
return await fetch(request); | ||
} catch (e) { | ||
console.warn('Failed to fetch', request.url, e); | ||
return new Response('Failed to fetch', { status: 500 }); | ||
} | ||
} | ||
const cacheKey = url.href; | ||
const cache = await getCache(cacheKey); | ||
if (cache) { | ||
console.log('Cache hit', cacheKey); | ||
return cache; | ||
} else { | ||
console.warn('Cache miss', cacheKey); | ||
} | ||
const response = await fetch(request); | ||
await addCache(cacheKey, response); | ||
return response; | ||
}; | ||
|
||
const preCachedResources: RequestInfo[] = ['/', '/index.html', '/sw.js']; | ||
|
||
export const preCacheResources = async (): Promise<any> => { | ||
if (!isValidHostname(new URL(location.href))) { | ||
return; | ||
} | ||
return caches | ||
.open(wpCacheKey) | ||
.then((cache) => cache.addAll(preCachedResources)); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.