Skip to content

feat(package_info_plus): implemented version caching based on serviceworkerversion #3549

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<script>
// The value below is injected by flutter build, do not touch.
const serviceWorkerVersion = null;
window.serviceWorkerVersion = serviceWorkerVersion;
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:js_interop';
import 'dart:ui_web';

import 'package:clock/clock.dart';
Expand All @@ -8,6 +9,24 @@ import 'package:package_info_plus_platform_interface/package_info_data.dart';
import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart';
import 'package:web/web.dart' as web;

/// Get the current service worker version.
/// This is used to get the application version that matches the current
/// service worker (even if there is a newer version available).
///
/// If no version is found, we fallback to the cachebuster
/// which will load the latest version of the file.
///
/// To get this working, you will need to add the following code to your
/// index.html:
/// ```javascript
/// // This line should already exist
/// var serviceWorkerVersion = {{SERVICE_WORKER_VERSION}};
/// // This line should be added
/// window.serviceWorkerVersion = serviceWorkerVersion;
/// ```
@JS('serviceWorkerVersion')
external String? get _serviceWorkerVersion;

/// The web implementation of [PackageInfoPlatform].
///
/// This class implements the `package:package_info_plus` functionality for the web.
Expand Down Expand Up @@ -46,9 +65,11 @@ class PackageInfoPlusWebPlugin extends PackageInfoPlatform {
final List<String> segments = [...uri.pathSegments]
..removeWhere((element) => element == '');

// Add file and cachebuster query
// Add file, serviceWorkerVersion and cachebuster query
return uri.replace(
query: 'cachebuster=$cacheBuster',
query: _serviceWorkerVersion != null
? 'serviceWorkerVersion=$_serviceWorkerVersion'
: 'cachebuster=$cacheBuster',
pathSegments: [...segments, 'version.json']);
}

Expand Down
Loading