Skip to content

Commit

Permalink
feat(dev): limited web support for dev
Browse files Browse the repository at this point in the history
chore: update web 3.22.
  • Loading branch information
PiTrem committed Jul 16, 2024
1 parent d341925 commit 68a362b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 64 deletions.
10 changes: 10 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@ flutter drive \
--target=integration_test/app_test.dart \
-d emu
```

## Run chrome device

Starts the app on chrome on a fixed port:


```
flutter run -d chrome --web-port=$DEF_PORT --web-browser-flag "--disable-web-security"
```

5 changes: 4 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:camera/camera.dart';
import 'package:chemobile/app.dart';
import 'package:flutter/material.dart';
Expand All @@ -12,7 +13,9 @@ void main() async {
cameras = await availableCameras();

final storage = await HydratedStorage.build(
storageDirectory: await getApplicationDocumentsDirectory(),
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
: await getApplicationDocumentsDirectory(),
);

HydratedBloc.storage = storage;
Expand Down
64 changes: 1 addition & 63 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,69 +36,7 @@
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
var serviceWorkerVersion = null;
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);
}
<script src="flutter_bootstrap.js" async></script>

if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('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 if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('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(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
</body>
</html>

0 comments on commit 68a362b

Please sign in to comment.