[next-pwa] custom 서비스워커 적용하기

Rachaen·2023년 4월 24일
0

문제

  • push 알람 설정을 어떻게 해야하지?
  • 커스텀을 해야하는데 next-pwa를 사용하면 sw.js가 자동으로 생성되어서 커스텀파일을 어디에 해야할 지 모르겠었다.
  • 어떻게 해결해야하는거지?!ㅠㅠ

next-pwa - custom worker

// worker/index.js
// 서비스 워커에게 푸시 이벤트를 수신하도록 지시
self.addEventListener('push', (event) => {
  console.log('[Service Worker] Push Received.', event.data.text());
  const { title, body } = event.data.json();
  event.waitUntil(self.registration.showNotification(title, { body }));
});
  • custom을 해주기 위해서 따로 파일을 만들어준다.
/** @type {import('next').NextConfig} */
const runtimeCaching = require('next-pwa/cache');
const withPWA = require('next-pwa')({
  dest: 'public',
  register: true,
  skipWaiting: true,
  customWorkerDir: 'worker',
  runtimeCaching,
});

const nextConfig = withPWA({
  // next config
});
module.exports = nextConfig;
  • next.config.js에서 customWorkerDir를 폴더이름으로 해주기.

custom-worker

profile
개발을 잘하자!

0개의 댓글