Progressive Web Apps 🚀 With Angular
Create Date: April 03, 2019 at 06:02 PM         | Tag: ANGULAR         | Author Name: Sun, Charles |
Brian Love @ Google Developer Group (GDG) Denver
Let your Angular app take off into the world of progressive web apps (PWA). We'll learn the basics of building progressive web apps, using an app shell, working with service workers, and how Angular makes it easy to build performant progressive web applications that your users can install on their mobile devices. We'll also look at browser support and capabilities for app-like behaviors including notifications, background requests and sync, and more. If you have any interest in the future of apps and the web, then this meetup is for you! No prior knowledge of PWAs is required, but a little bit of knowledge of Angular would be helpful.
Brian Love is a software engineer and Google Developer Expert in Angular with a passion for learning, writing, speaking, teaching and mentoring. Brian has been building web applications for over 20 years and has long been a fanboy of JavaScript. When not in front of his MacBook Pro, Brian is in the Rocky Mountains skiing or hiking.
prgressive web apps use modern web apis along with traditional progressive enahncement strategy to create cross-platform web applications.
1. reialble: minimal time to intractive when on s low 3g
2. engaging: same suer experience you would expect form a "native" app
3. paper: escaping the tab: progressive, not hybrid --lex Russell and Frances Beriman
how to accomplish it
1. app shell
2. service workers
3. web manifest
4. progressive
service workers enalbe:
- offline
- versioning
- notifications
- background sync (draft)
lifecyle:
some things to note:
- executes in the serviceWorkerGlobalScope
- no access to the DOM
- requires HTTPS/TLS
- no synchronous requests
CacheStorage:
- storage for Cache objects that are available
- avilable to a service worker vis ServeWorkerGlobalScope.caches
- CacheStorage.open() to access Cache
- CacheStorage.match() to determine if request is in any of the Cache objects
Cache:
Sorage mechanism for Request and Response object pairs
some theods include:
Register Service Worker:
const name = 'angular-pwa'
self.addEventListenner('fetch', funcgiton(e) {
e.responseWith(...)
}
1. check if resource exists in cache
web manifest
manifes.json:
{
"short_name":
"name":
"icons":
"start_url":
"backgraound_color":
"dispaly":
"scope": "/",
"theme_color":
}
index.html:
<!doctype html>
<html lang="en">
<ead>
<link rel="manifest" href="manifest.json">
</head>
<body>
<app-root></app-root>
<noscript>Please enable javascript to continue using this application.
</noscript>
</body>
</html>
angualr pwa:
- service worker module
- single page applictions
- code spliting
- lazy loading
- prefectching
add service worker module
ng add @angular/pwa
ng build --prod
http-server -p 8080 -c-l dist/<project_name>
what is cached?
- index.html
- favicon.ico
- dist/**/*.js
- dist/**/*.css
- asset/**
- resource urls specified in src/ngsw-config.json
app installation modes:
angualr has two installation mods:
1. prefectch - nesoures resources are available offline
2. lazy -resources are cacheed when requested
app versions:
1. serve app from cache
2. check for ner version
3. fetch new version resources in the background
4. launch new version nwhne app is loaded next, or immediatedly install and relaod app programmatically
app update modes: two modes
SwUpdae
class SwUpdate {
available: Oberservable
Date Reqquests:
- maxSize:
- maxAge: max age of cached resuqests
- timeout: max time to wait for requests
- strategy:
- performance - use cached version first
- freshnes - fetch and failover to cache after timeout
do not cache the verice worker
chache-Conrole: max-age = 0
workbox:
- extensive
- advanced routing and strategies
- CLI
- webpack
- google analytics
lighthouse:
- chrome devtools, chrome extension, and node cli
- integrate in cd/ci pipleine
- web app performance
- serive work checklist
PWA rocke: https://pwa.rocks/
angular docs: https://angular.io/guide/service-worker-intro
google develpers: https://developers.google.com/web/progressive-web-apps/
MDN web docs: https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps
New CommentLimit words in a textarea
Create Date: April 03, 2019 at 11:00 AM         | Tag: JAVASCRIPT         | Author Name: Sun, Charles |
Limit words in a textarea
I would probably use this:
var maxWords = 10;
function limitLengthInWords(field) {
var value = field.value,
wordCount = value.split(/\S+/).length - 1,
re = new RegExp("^\\s*\\S+(?:\\s+\\S+){0,"+(maxWords-1)+"}");
if (wordCount >= maxWords) {
field.value = value.match(re);
}
document.getElementById('description_count').innerHTML = maxWords - wordCount;
}
This will preserve the whitespace at the begin and between the words.
New CommentCreate searchable select using javascript
Create Date: March 22, 2019 at 02:05 PM         | Tag: JAVASCRIPT         | Author Name: Sun, Charles |
How search into options of select tag html without plugin
Yes you can, first, see it in action in this demo, if you like what you see, here's how to do it:
HTML
<input type="search" id="searchBox">
<select id="countries">
<option value="arg">Argentina</option>
<option value="usa">United States of America</option>
<option value="som">Somalia</option>
</select>
It's pretty straight forward, a search input and a select with a few options.
JavaScript
searchBox = document.querySelector("#searchBox");
countries = document.querySelector("#countries");
var when = "keyup"; //You can change this to keydown, keypress or change
searchBox.addEventListener("keyup", function (e) {
var text = e.target.value;
var options = countries.options;
for (var i = 0; i < options.length; i++) {
var option = options[i];
var optionText = option.text;
var lowerOptionText = optionText.toLowerCase();
var lowerText = text.toLowerCase();
var regex = new RegExp("^" + text, "i");
var match = optionText.match(regex);
var contains = lowerOptionText.indexOf(lowerText) != -1;
if (match || contains) {
option.selected = true;
return;
}
searchBox.selectedIndex = 0;
}
});
Explanation
First, the variables:
- searchBox : link to the
HTMLElement
search input. - countries : link to the
HTMLElement
select. - when : event type, I used "keyup" and that means the select will update when you press and lift a key in the searchBox.
- text, lowerText : The value of the searchBox (in other words, the input text). The second one equals the first but lowercased for case insensitive testing.
- options : The select options
objects
. - optionText, lowerOptionText : The text of the option
object
(ej. "Argentina") and the other one is the lower version for case insensitive testing (ej. "argentina") - regex : It's a
RegExp Object
, a regular expression, basically what it does is it tests (case insensitive, because of the 'i' in the second parameter) wether the some string begins with some value, in this case, the value would be the input text. - match : It executes the
RegExp Object
agains the option's text, that means it will test if the inputted text is the same as the beggining of the option's text. - contains : It checks if the option's text contains the inputted text.
Few, that was a lot, so, why do we need 2 tests? Because there are two possibilities for selection with searchBox, one is that when you start typing "Unit.." it should match "United States of America"(regexp), and the other one is that you just type "america" and it should also match "United States of America"(contains)
So, it checks for both tests, and if either one is true it will select that option. (It will also return so that it doesn't continue executing code)
By default, if no test is true, it will select the first element of the select.