Skip to content

Commit 3df97fb

Browse files
committed
make the project PWA; update Readme
1 parent ab50381 commit 3df97fb

File tree

5 files changed

+81
-23
lines changed

5 files changed

+81
-23
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
## About
1+
This project contains a minimal [Progressive Web App (PWA)](https://en.wikipedia.org/wiki/Progressive_web_application) using [`Yew`](https://github.com/yewstack/yew). It is based on the [`yew-wasm-pack-minimal`](https://github.com/yewstack/yew-wasm-pack-minimal) example and James Johnson's [Simple PWA Tutorial](https://medium.com/james-johnson/a-simple-progressive-web-app-tutorial-f9708e5f2605).
22

3-
This template demonstrates the minimum code and tooling necessary for a frontend web app with simple deployable artifacts consisting of one HTML file, one JavaScript file, and one WebAssembly file, using [`Yew`](https://github.com/yewstack/yew), [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen), and [`wasm-pack`](https://github.com/rustwasm/wasm-pack).
3+
Compared to [`yew-wasm-pack-minimal`](https://github.com/yewstack/yew-wasm-pack-minimal), the following changes were implemented:
4+
- Add a manifest file (`manifest.json`) describing the PWA.
5+
- Add a service worker (`sw.js`) that allows the app to work offline.
6+
- Add an icon (`icon-256.png`).
7+
- Register the service worker and manifest file in `index.html`.
48

5-
Note: [`yew-wasm-pack-template`](https://github.com/yewstack/yew-wasm-pack-template) is the full-featured counterpart to this template, integrating many common web technologies.
9+
## Build
610

7-
## Usage
11+
Follow the instructions at https://github.com/yewstack/yew-wasm-pack-minimal to build and bundle the project. Basically:
12+
```
13+
wasm-pack build --target web
14+
rollup ./main.js --format iife --file ./pkg/bundle.js
15+
```
816

9-
### 1) Install `Rust` and `wasm-pack`
17+
## Test / Deploy
1018

11-
Follow the instructions at https://www.rust-lang.org/tools/install and follow the `installation` link at [`wasm-pack`](https://github.com/rustwasm/wasm-pack).
19+
PWAs need to be accessed via https. If you don't have a https server set up already, the simplest way to test your PWA is to use Github Pages. Create a new project, activate Github Pages for the master branch and upload the following files to it:
1220

13-
### 2) Build
21+
```
22+
pkg/bundle.js
23+
pkg/yew_wasm_pack_minimal_bg.wasm
24+
icon-256.png
25+
index.html
26+
manifest.json
27+
sw.js
28+
```
1429

15-
Enter `wasm-pack build --target web` from your project's root directory.
30+
The website should become available under https://USERNAME.github.io/PROJECT_NAME after a couple of minutes. I've uploaded the deployment files to https://github.com/fkohlgrueber/yew-pwa-deploy. The PWA is available under https://fkohlgrueber.github.io/yew-pwa-deploy/.
1631

17-
### 3) [temporary] Bundle
18-
19-
Enter `rollup ./main.js --format iife --file ./pkg/bundle.js` from your project's root directory.
20-
21-
Note: Until `wasm-pack` [RFC #6](https://github.com/rustwasm/rfcs/blob/master/text/006-local-js-dependencies.md) is implemented there is no available option to [generate a single amalgamated JavaScript file](https://github.com/rustwasm/wasm-pack/issues/699). In the interim a bundler, such as [`Rollup`](https://rollupjs.org/guide/en/#quick-start), must be used.
22-
23-
### 4) [optional] Test Run
24-
25-
Run a webserver from your project's root directory, such as with the Python 3 command: `python -m http.server 8080`, and load http://localhost:8080/ in a browser to run the app.
26-
27-
Note: It's expected behavior for the browser console to display an error similar to "WebAssembly.instantiateStreaming failed. Assuming this is because your server does not serve wasm with application/wasm MIME type." Your production webserver should be configured to associate WebAssembly files with the `application/wasm` MIME type.
28-
29-
### 5) Deploy
30-
31-
Access your generated build artifacts, `bundle.js` and `yew_wasm_pack_minimal_bg.wasm`, in ./pkg from your project's root directory.
32+
Feel free to open issues if things don't work for you.

icon-256.png

47.2 KB
Loading

index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
<meta charset="utf-8" />
66
<title>Yew</title>
77
<script src="./pkg/bundle.js" defer></script>
8+
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
11+
<link rel="manifest" href="./manifest.json">
12+
13+
<script>
14+
// register ServiceWorker
15+
window.onload = () => {
16+
'use strict';
17+
18+
if ('serviceWorker' in navigator) {
19+
navigator.serviceWorker
20+
.register('./sw.js');
21+
}
22+
}
23+
</script>
24+
825
</head>
926

1027
<body>

manifest.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Yew Progressive Web App",
3+
"short_name": "Yew PWA",
4+
"icons": [{
5+
"src": "./icon-256.png",
6+
"sizes": "256x256",
7+
"type": "image/png"
8+
}],
9+
"lang": "en-US",
10+
"start_url": "./index.html",
11+
"display": "standalone",
12+
"background_color": "white",
13+
"theme_color": "white"
14+
}

sw.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var cacheName = 'yew-pwa';
2+
var filesToCache = [
3+
'./',
4+
'./index.html',
5+
'./pkg/bundle.js',
6+
'./pkg/yew_wasm_pack_minimal_bg.wasm'
7+
];
8+
9+
10+
/* Start the service worker and cache all of the app's content */
11+
self.addEventListener('install', function(e) {
12+
e.waitUntil(
13+
caches.open(cacheName).then(function(cache) {
14+
return cache.addAll(filesToCache);
15+
})
16+
);
17+
});
18+
19+
/* Serve cached content when offline */
20+
self.addEventListener('fetch', function(e) {
21+
e.respondWith(
22+
caches.match(e.request).then(function(response) {
23+
return response || fetch(e.request);
24+
})
25+
);
26+
});

0 commit comments

Comments
 (0)