質問編集履歴

2

追記

2019/07/05 15:48

投稿

makoto-n
makoto-n

スコア436

test CHANGED
File without changes
test CHANGED
@@ -324,184 +324,234 @@
324
324
 
325
325
  ![画像](660a3eacb12fa10d18d1f0ad1cce86ee.png)
326
326
 
327
+
328
+
329
+ 詳しい方、ご助力お願いします。
330
+
331
+
332
+
333
+
334
+
335
+ ---
336
+
337
+ 7/6追記
338
+
339
+ navigator.jsとservice-worker.jsをESLintにしたがい修正してみましたが、
340
+
341
+ ホーム画面通知が表示されません。
342
+
343
+ 記述します。おかしい点を教えてください。
344
+
345
+ 周りの人にはPWAに詳しい方がいないので学習に苦戦しています。
346
+
347
+
348
+
349
+ navigator.js
350
+
327
351
  ```js
328
352
 
353
+ // service workerが有効なら、service-worker.js を登録します
354
+
355
+ if ('serviceWorker' in navigator) {
356
+
357
+ navigator.serviceWorker
358
+
359
+ .register('./service-worker.js', { scope: '/sample/02/' })
360
+
361
+ .then((registration) => {
362
+
363
+ // 登録成功
364
+
365
+ console.log('ServiceWorker registration successful with scope: ', registration.scope)
366
+
367
+ }).catch((err) => {
368
+
369
+ // 登録失敗 :(
370
+
371
+ console.log('ServiceWorker registration failed: ', err)
372
+
373
+ })
374
+
375
+ }
376
+
377
+ ```
378
+
379
+
380
+
381
+ service-worker.js
382
+
383
+ ```js
384
+
329
- const CACHE_NAME = 'cache-v1';
385
+ const CACHE_NAME = 'cache-v1'
330
386
 
331
387
  const urlsToCache = [
332
388
 
333
- '/',
389
+ '/',
334
-
390
+
335
- '/index.html',
391
+ '/index.html',
336
-
392
+
337
- '/about.html',
393
+ '/about.html',
338
-
394
+
339
- '/contact.html',
395
+ '/contact.html',
340
-
396
+
341
- '/css/style.css',
397
+ '/css/style.css',
342
-
398
+
343
- '//stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
399
+ '//stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
344
-
400
+
345
- '/img/cafe--coffe01.jpg',
401
+ '/img/cafe--coffe01.jpg',
346
-
402
+
347
- '/img/cafe--coffe02.jpg',
403
+ '/img/cafe--coffe02.jpg',
348
-
404
+
349
- '/img/cafe--coffe03.jpg',
405
+ '/img/cafe--coffe03.jpg',
350
-
406
+
351
- '/img/cafe--coffe04.jpg',
407
+ '/img/cafe--coffe04.jpg',
352
-
408
+
353
- '/img/cafe--farmer01.jpg',
409
+ '/img/cafe--farmer01.jpg',
354
-
410
+
355
- '//code.jquery.com/jquery-3.3.1.slim.min.js',
411
+ '//code.jquery.com/jquery-3.3.1.slim.min.js',
356
-
412
+
357
- '//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
413
+ '//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
358
-
414
+
359
- '//stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
415
+ '//stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
360
-
416
+
361
- '/js/index.js'
417
+ '/js/index.js'
362
-
418
+
363
- ];
419
+ ]
364
420
 
365
421
 
366
422
 
367
423
  // install-event
368
424
 
369
- self.addEventListener('install', function(event) {
425
+ self.addEventListener('install', (event) => {
370
-
426
+
371
- event.waitUntil(
427
+ event.waitUntil(
372
-
428
+
373
- caches.open(CACHE_NAME)
429
+ caches.open(CACHE_NAME)
374
-
430
+
375
- .then(function(cache) {
431
+ .then((cache) => {
376
-
432
+
377
- console.log('Opened cache');
433
+ console.log('Opened cache')
378
-
379
-
380
-
434
+
435
+
436
+
381
- // 指定されたリソースをキャッシュに追加する
437
+ // 指定されたリソースをキャッシュに追加する
382
-
438
+
383
- return cache.addAll(urlsToCache);
439
+ return cache.addAll(urlsToCache)
384
-
440
+
385
- })
441
+ })
386
-
442
+
387
- );
443
+ )
388
-
444
+
389
- });
445
+ })
390
446
 
391
447
 
392
448
 
393
449
  // activate-event
394
450
 
395
- self.addEventListener('activate', function(event) {
451
+ self.addEventListener('activate', (event) => {
396
-
452
+
397
- var cacheWhitelist = [CACHE_NAME];
453
+ var cacheWhitelist = [CACHE_NAME]
398
-
454
+
399
- event.waitUntil(
455
+ event.waitUntil(
400
-
456
+
401
- caches.keys().then(function(cacheNames) {
457
+ caches.keys().then((cacheNames) => {
402
-
458
+
403
- return Promise.all(
459
+ return Promise.all(
404
-
460
+
405
- cacheNames.map(function(cacheName) {
461
+ cacheNames.map((cacheName) => {
406
-
462
+
407
- // ホワイトリストにないキャッシュ(古いキャッシュ)は削除する
463
+ // ホワイトリストにないキャッシュ(古いキャッシュ)は削除する
408
-
464
+
409
- if (cacheWhitelist.indexOf(cacheName) === -1) {
465
+ if (cacheWhitelist.indexOf(cacheName) === -1) {
410
-
466
+
411
- return caches.delete(cacheName);
467
+ return caches.delete(cacheName)
412
-
468
+
413
- }
469
+ }
414
-
415
- })
416
-
417
- );
418
470
 
419
471
  })
420
472
 
421
- );
473
+ )
422
-
474
+
423
- });
475
+ })
476
+
477
+ )
478
+
479
+ })
424
480
 
425
481
 
426
482
 
427
483
  // fetch-event
428
484
 
429
- self.addEventListener('fetch', function(event) {
430
-
431
- event.respondWith(
432
-
433
- caches.match(event.request)
434
-
435
- .then(function(response) {
436
-
437
- if (response) {
438
-
439
- return response;
440
-
441
- }
442
-
443
-
444
-
445
- // 重要:リクエストを clone する。リクエストは Stream なので
446
-
447
- // 一度しか処理できない。ここではキャッシュ用、fetch 用と2回
448
-
449
- // 必要なので、リクエストは clone しないといけない
450
-
451
- let fetchRequest = event.request.clone();
452
-
453
-
454
-
455
- return fetch(fetchRequest)
456
-
457
- .then(function(response) {
458
-
459
- if (!response || response.status !== 200 || response.type !== 'basic') {
460
-
461
- return response;
462
-
463
- }
464
-
465
-
466
-
467
- // 重要:レスポンスを clone する。レスポンスは Stream で
468
-
469
- // ブラウザ用とキャッシュ用の2回必要。なので clone して
470
-
471
- // 2つの Stream があるようにする
472
-
473
- let responseToCache = response.clone();
474
-
475
-
476
-
477
- caches.open(CACHE_NAME)
478
-
479
- .then(function(cache) {
480
-
481
- cache.put(event.request, responseToCache);
482
-
483
- });
484
-
485
-
486
-
487
- return response;
488
-
489
- });
490
-
491
- })
492
-
493
- );
494
-
495
- });
496
-
497
-
498
-
499
- // 現状では、この処理を書かないとService Workerが有効と判定されないようです
500
-
501
- self.addEventListener('fetch', function(event) {});
485
+ self.addEventListener('fetch', (event) => {
486
+
487
+ event.respondWith(
488
+
489
+ caches.match(event.request)
490
+
491
+ .then((response) => {
492
+
493
+ if (response) {
494
+
495
+ return response
496
+
497
+ }
498
+
499
+
500
+
501
+ // 重要:リクエストを clone する。リクエストは Stream なので
502
+
503
+ // 一度しか処理できない。ここではキャッシュ用、fetch 用と2回
504
+
505
+ // 必要なので、リクエストは clone しないといけない
506
+
507
+ let fetchRequest = event.request.clone()
508
+
509
+
510
+
511
+ return fetch(fetchRequest)
512
+
513
+ .then((response) => {
514
+
515
+ if (!response || response.status !== 200 || response.type !== 'basic') {
516
+
517
+ return response
518
+
519
+ }
520
+
521
+
522
+
523
+ // 重要:レスポンスを clone する。レスポンスは Stream で
524
+
525
+ // ブラウザ用とキャッシュ用の2回必要。なので clone して
526
+
527
+ // 2つの Stream があるようにする
528
+
529
+ let responseToCache = response.clone()
530
+
531
+
532
+
533
+ caches.open(CACHE_NAME)
534
+
535
+ .then((cache) => {
536
+
537
+ cache.put(event.request, responseToCache)
538
+
539
+ })
540
+
541
+
542
+
543
+ return response
544
+
545
+ })
546
+
547
+ })
548
+
549
+ )
550
+
551
+ })
552
+
553
+
554
+
555
+ self.addEventListener('fetch', () => {})
502
556
 
503
557
  ```
504
-
505
-
506
-
507
- 詳しい方、ご助力お願いします。

1

追記

2019/07/05 15:48

投稿

makoto-n
makoto-n

スコア436

test CHANGED
File without changes
test CHANGED
@@ -303,3 +303,205 @@
303
303
  self.addEventListener('fetch', function(event) {});
304
304
 
305
305
  ```
306
+
307
+
308
+
309
+
310
+
311
+ ---
312
+
313
+ 6/25追記
314
+
315
+
316
+
317
+ おかげで機内モードでの表示は可能になりました。
318
+
319
+ キャッシュで表示可能になったと思います。
320
+
321
+ が、プッシュ通知がなんど試しても表示されません。
322
+
323
+ service-workerでは2つエラーが出ているようですが、箇所が特定できません。
324
+
325
+ ![画像](660a3eacb12fa10d18d1f0ad1cce86ee.png)
326
+
327
+ ```js
328
+
329
+ const CACHE_NAME = 'cache-v1';
330
+
331
+ const urlsToCache = [
332
+
333
+ '/',
334
+
335
+ '/index.html',
336
+
337
+ '/about.html',
338
+
339
+ '/contact.html',
340
+
341
+ '/css/style.css',
342
+
343
+ '//stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
344
+
345
+ '/img/cafe--coffe01.jpg',
346
+
347
+ '/img/cafe--coffe02.jpg',
348
+
349
+ '/img/cafe--coffe03.jpg',
350
+
351
+ '/img/cafe--coffe04.jpg',
352
+
353
+ '/img/cafe--farmer01.jpg',
354
+
355
+ '//code.jquery.com/jquery-3.3.1.slim.min.js',
356
+
357
+ '//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
358
+
359
+ '//stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
360
+
361
+ '/js/index.js'
362
+
363
+ ];
364
+
365
+
366
+
367
+ // install-event
368
+
369
+ self.addEventListener('install', function(event) {
370
+
371
+ event.waitUntil(
372
+
373
+ caches.open(CACHE_NAME)
374
+
375
+ .then(function(cache) {
376
+
377
+ console.log('Opened cache');
378
+
379
+
380
+
381
+ // 指定されたリソースをキャッシュに追加する
382
+
383
+ return cache.addAll(urlsToCache);
384
+
385
+ })
386
+
387
+ );
388
+
389
+ });
390
+
391
+
392
+
393
+ // activate-event
394
+
395
+ self.addEventListener('activate', function(event) {
396
+
397
+ var cacheWhitelist = [CACHE_NAME];
398
+
399
+ event.waitUntil(
400
+
401
+ caches.keys().then(function(cacheNames) {
402
+
403
+ return Promise.all(
404
+
405
+ cacheNames.map(function(cacheName) {
406
+
407
+ // ホワイトリストにないキャッシュ(古いキャッシュ)は削除する
408
+
409
+ if (cacheWhitelist.indexOf(cacheName) === -1) {
410
+
411
+ return caches.delete(cacheName);
412
+
413
+ }
414
+
415
+ })
416
+
417
+ );
418
+
419
+ })
420
+
421
+ );
422
+
423
+ });
424
+
425
+
426
+
427
+ // fetch-event
428
+
429
+ self.addEventListener('fetch', function(event) {
430
+
431
+ event.respondWith(
432
+
433
+ caches.match(event.request)
434
+
435
+ .then(function(response) {
436
+
437
+ if (response) {
438
+
439
+ return response;
440
+
441
+ }
442
+
443
+
444
+
445
+ // 重要:リクエストを clone する。リクエストは Stream なので
446
+
447
+ // 一度しか処理できない。ここではキャッシュ用、fetch 用と2回
448
+
449
+ // 必要なので、リクエストは clone しないといけない
450
+
451
+ let fetchRequest = event.request.clone();
452
+
453
+
454
+
455
+ return fetch(fetchRequest)
456
+
457
+ .then(function(response) {
458
+
459
+ if (!response || response.status !== 200 || response.type !== 'basic') {
460
+
461
+ return response;
462
+
463
+ }
464
+
465
+
466
+
467
+ // 重要:レスポンスを clone する。レスポンスは Stream で
468
+
469
+ // ブラウザ用とキャッシュ用の2回必要。なので clone して
470
+
471
+ // 2つの Stream があるようにする
472
+
473
+ let responseToCache = response.clone();
474
+
475
+
476
+
477
+ caches.open(CACHE_NAME)
478
+
479
+ .then(function(cache) {
480
+
481
+ cache.put(event.request, responseToCache);
482
+
483
+ });
484
+
485
+
486
+
487
+ return response;
488
+
489
+ });
490
+
491
+ })
492
+
493
+ );
494
+
495
+ });
496
+
497
+
498
+
499
+ // 現状では、この処理を書かないとService Workerが有効と判定されないようです
500
+
501
+ self.addEventListener('fetch', function(event) {});
502
+
503
+ ```
504
+
505
+
506
+
507
+ 詳しい方、ご助力お願いします。