Skip to content

Commit 69ecdd7

Browse files
authored
Merge pull request #20 from pyscript/mini-coi
Proper use of mini-coi.js within .html files
2 parents c383701 + 4c1131e commit 69ecdd7

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

index.html

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,7 @@
88
<meta charset="UTF-8">
99
<link rel="icon" type="image/x-icon" href="favicon.ico">
1010
<meta name="viewport" content="width=device-width,initial-scale=1">
11-
12-
<script>
13-
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
14-
/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */
15-
(({ document: d, navigator: { serviceWorker: s } }) => {
16-
if (d) {
17-
const { currentScript: c } = d;
18-
s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => {
19-
r.addEventListener('updatefound', () => location.reload());
20-
if (r.active && !s.controller) location.reload();
21-
});
22-
}
23-
else {
24-
addEventListener('install', () => skipWaiting());
25-
addEventListener('activate', e => e.waitUntil(clients.claim()));
26-
addEventListener('fetch', e => {
27-
const { request: r } = e;
28-
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return;
29-
e.respondWith(fetch(r).then(r => {
30-
const { body, status, statusText } = r;
31-
if (!status || status > 399) return r;
32-
const h = new Headers(r.headers);
33-
h.set('Cross-Origin-Opener-Policy', 'same-origin');
34-
h.set('Cross-Origin-Embedder-Policy', 'require-corp');
35-
h.set('Cross-Origin-Resource-Policy', 'cross-origin');
36-
return new Response(body, { status, statusText, headers: h });
37-
}));
38-
});
39-
}
40-
})(self);
41-
</script>
11+
<script src="mini-coi.js"></script>
4212

4313
<!-- Import PyScript - see kitchensink.js for activation -->
4414
<script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script>
@@ -48,15 +18,15 @@
4818
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
4919
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery-ui.min.js"></script>
5020
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.2/themes/base/jquery-ui.css"/>
51-
21+
5222
<!-- Codemirror Interactive Editor -->
5323
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.35.0/codemirror.js"></script>
5424
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/mode/python/python.min.js"></script>
5525
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css" >
5626

5727
<!-- Import for drawing arrows -->
5828
<script defer src="leader-line.min.js"></script>
59-
29+
6030
<!-- Import Styles for the kitchensink demo -->
6131
<link rel="stylesheet" href="kitchensink.css">
6232
</head>

mini-coi.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
2+
/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */
3+
(({ document: d, navigator: { serviceWorker: s } }) => {
4+
if (d) {
5+
const { currentScript: c } = d;
6+
s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => {
7+
r.addEventListener('updatefound', () => location.reload());
8+
if (r.active && !s.controller) location.reload();
9+
});
10+
}
11+
else {
12+
addEventListener('install', () => skipWaiting());
13+
addEventListener('activate', e => e.waitUntil(clients.claim()));
14+
addEventListener('fetch', e => {
15+
const { request: r } = e;
16+
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return;
17+
e.respondWith(fetch(r).then(r => {
18+
const { body, status, statusText } = r;
19+
if (!status || status > 399) return r;
20+
const h = new Headers(r.headers);
21+
h.set('Cross-Origin-Opener-Policy', 'same-origin');
22+
h.set('Cross-Origin-Embedder-Policy', 'require-corp');
23+
h.set('Cross-Origin-Resource-Policy', 'cross-origin');
24+
return new Response(body, { status, statusText, headers: h });
25+
}));
26+
});
27+
}
28+
})(self);

tutorial.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
<title>LTK on MicroPython</title>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width,initial-scale=1">
7-
7+
<script src="mini-coi.js"></script>
8+
89
<link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css">
910
<script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script>
10-
11+
1112
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
1213
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
1314
<script src=" https://cdnjs.cloudflare.com/ajax/libs/leader-line/1.0.3/leader-line.min.js"></script>
@@ -40,7 +41,7 @@
4041
]).run()
4142
except Exception as e:
4243
print(e)
43-
44+
4445
(
4546
ltk.VBox(
4647
ltk.Text("Tutorial Demo"),
@@ -50,6 +51,6 @@
5051
.appendTo(ltk.window.document.body)
5152
)
5253
</script>
53-
54+
5455
</body>
55-
</html>
56+
</html>

0 commit comments

Comments
 (0)