Open
Conversation
This commit introduces a service worker to enable offline functionality for the game. Key changes: - Added `src/service-worker.js`: This file contains the service worker logic, which caches all static assets and application files. It employs a "network falling back to cache" strategy to prioritize updates when an internet connection is available. - Registered the service worker in `src/routes/+layout.js`: The service worker is now registered on window load. - Updated `vite.config.ts`: Configured the build process to include the service worker in the `docs` output directory using `rollup-plugin-copy`. - Added `rollup-plugin-copy` as a dev dependency. The application assets are now cached, allowing the game to be played even when you are offline. When online, the service worker will attempt to fetch the latest version of the files.
This commit refactors the PWA implementation to use `vite-plugin-pwa`, replacing the previous manual service worker setup. This provides a more robust, maintainable, and automated solution for offline capabilities and asset caching.
Key changes:
- Removed the manually created `src/service-worker.js` and its registration in `src/routes/+layout.js`.
- Uninstalled `rollup-plugin-copy` and removed its configuration from `vite.config.ts`.
- Installed `vite-plugin-pwa` as a dev dependency.
- Configured `vite-plugin-pwa` in `vite.config.ts`:
- Enables `autoUpdate` for the service worker.
- Automatically injects the service worker registration.
- Defines Workbox `globPatterns` for comprehensive precaching of application assets.
- Implements runtime caching strategies:
- `NetworkFirst` for navigation requests to prioritize fresh content.
- `CacheFirst` for static assets (images, fonts).
- `StaleWhileRevalidate` for JS and CSS files.
- Configures a PWA manifest (`manifest.webmanifest`) with application details and icons.
- Enables PWA functionality in the development environment for easier testing.
This approach ensures that the service worker is automatically generated and updated with each build, correctly caching application assets for offline use and providing a better update experience for you.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This commit introduces a service worker to enable offline functionality for the game.
Key changes:
src/service-worker.js: This file contains the service worker logic, which caches all static assets and application files. It employs a "network falling back to cache" strategy to prioritize updates when an internet connection is available.src/routes/+layout.js: The service worker is now registered on window load.vite.config.ts: Configured the build process to include the service worker in thedocsoutput directory usingrollup-plugin-copy.rollup-plugin-copyas a dev dependency.The application assets are now cached, allowing the game to be played even when you are offline. When online, the service worker will attempt to fetch the latest version of the files.