Skip to content

Commit

Permalink
TF-3256 Update index.html cache config
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Nov 15, 2024
1 parent 51a4a1b commit 0ee45a8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 61 deletions.
4 changes: 2 additions & 2 deletions docs/adr/0008-handle-heavy-task-with-isolate.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ isolateManager.execute(
- Communicate between two isolates
To see a visual representation of this process, take a look at the image below:

[communicate_between_two_isolate](../images/handle_task_with_isolate.png);
<img src="../images/handle_task_with_isolate.png" alt="communicate_between_two_isolate">

## Consequences

Expand All @@ -45,4 +45,4 @@ To see a visual representation of this process, take a look at the image below:

## Reference

- [worker_manager](https://pub.dev/packages/worker_manager)
- <img src="../images/handle_task_with_isolate.png" alt="worker_manager">
24 changes: 24 additions & 0 deletions docs/adr/0054-update-flutter-service-worker-initialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 54. Update Flutter Service Worker Initialization

Date: 2024-11-15

## Status

Accepted

## Context

- Since the new version of Flutter, old initialization of Flutter Service Worker does not work.
- Version is always null
- Twake Mail web resources is requested everytime the web is loaded, no cache is used <img src="../images/before-flutter-3.22.png">
- One hard-reload, one normal reload

## Decision

- We update the initialization of Flutter Service Worker according to the new version of Flutter
- More reference: https://docs.flutter.dev/platform-integration/web/initialization

## Consequences

- Twake Mail web use cache resources to improve performance <img src="../images/after-flutter-3.22.png">
- One hard-reload, many normal reload
Binary file added docs/images/after-flutter-3.22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/before-flutter-3.22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 14 additions & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
loadLanguageResources().finally(initialWorkerService);
{{flutter_js}}
{{flutter_build_config}}

loadLanguageResources().finally(initialTmailApp);

_flutter.loader.load({
serviceWorkerSettings: {
serviceWorkerVersion: {{flutter_service_worker_version}},
},
onEntrypointLoaded: async function(engineInitializer) {
const appRunner = await engineInitializer.initializeEngine();
await appRunner.runApp();
}
});
</script>
<script src="https://unpkg.com/pica/dist/pica.min.js" ></script>
</body>
Expand Down
59 changes: 1 addition & 58 deletions web/worker_service/worker_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,10 @@ const iosStore = 'https://apps.apple.com/app/twake-mail/id1587086189';
const iosPlatform = 'iOS';
const androidPlatform = 'android';
const otherPlatform = 'other';
const timeoutDuration = 4000;
var scriptLoaded = false;

function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}

function fetchServiceWorker() {
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
// Wait for registration to finish before dropping the <script>tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
navigator.serviceWorker.register('flutter_service_worker.js?v={{flutter_service_worker_version}}')
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('[TwakeMail] fetchServiceWorker(): Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing || reg.waiting);
} else {
// Existing service worker is still good.
console.log('[TwakeMail] fetchServiceWorker(): Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'[TwakeMail] fetchServiceWorker(): Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, timeoutDuration);
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
}

function handleContinueTwakeMailOnWeb() {
console.info('[TwakeMail] handleContinueTwakeMailOnWeb(): Continue on web.');
closeSmartBanner();
fetchServiceWorker();
}

function getPlatform() {
Expand All @@ -86,15 +30,14 @@ function handleOpenTwakeMailApp() {
}
}

function initialWorkerService() {
function initialTmailApp() {
const os = getPlatform();
const originInUrl = window.location;

console.info('[TwakeMail] initialWorkerService(): OriginInUrl:', originInUrl);

// For desktop, we don't show the open on app popup
if (os === otherPlatform || typeof window === 'undefined') {
fetchServiceWorker();
return;
}

Expand Down

0 comments on commit 0ee45a8

Please sign in to comment.