Workbox - team-yaza/mozi-client GitHub Wiki

๊ฐœ์š”

Next PWA๋Š” Workbox๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด Workbox์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

serviceWorker๋ฅผ ๋‹ค๋ฃจ๋ฉด์„œ ๋Š๋‚€ ๊ฒƒ์€ '์ •๋ง ๋‹ค๋ฃจ๊ธฐ ์กฐ์žกํ•˜๋‹ค'๋ผ๋Š” ๋Š๋‚Œ์ด์—ˆ๋‹ค. API๋Š” ๋‹ค๋ฃจ๊ธฐ ์กฐ์žกํ•œ ๊ฒƒ๋“ค์„ ํŽธํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ฒŒ ํ•ด์ค€๋‹ค. Workbox๊ฐ€ ๊ทธ๋ ‡๋‹ค.

Workbox is a set of modules that simplify common service worker routing and caching.

Workbox์˜ ๋ชฉํ‘œ๋Š” ์„œ๋น„์Šค์›Œ์ปค ์‚ฌ์šฉ์„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•จ์— ์žˆ๊ณ  ๋™์‹œ์— ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋ณต์žกํ•œ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœํ•œ๋‹ค.

๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋กœ workbox-build๋Š” ์ง€์ •๋œ asset์„ ๋ฏธ๋ฆฌ ์บ์‹œํ•˜๋Š” ์„œ๋น„์Šค ์ž‘์—…์ž๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

๋ชจ๋“ˆ๋“ค์„ ๋ช‡๊ฐœ ๋ณด์ž.

  • workbox-routing: for request matching
  • workbox-strategies: for caching strategies
  • workbox-precaching: for precaching
  • workbox-expiration: for managing caches
  • workbox-window: registering a service worker and handling updates in the window context

์ด๋Ÿฌํ•œ ๋ชจ๋“ˆ๋“ค์€ ์„œ๋น„์Šค ์›Œ์ปค API๋ฅผ ์ง์ ‘์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ฝ๊ณ  ์œ ์ง€ ๊ด€๋ฆฌํ•˜๊ธฐ ์‰ฌ์šด ์„ ์–ธ์  ๋ฐฉ์‹์œผ๋กœ ์„œ๋น„์Šค ์›Œ์ปค ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ ๋„์›€์ด ๋œ๋‹ค.

์•„๋ž˜๋Š” ์„ ์–ธ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ด๋‹ค.

import { skipWaiting, clientsClaim } from 'worbox-core';

skipWaiting()
clientsClaim()

skipWaiting๊ณผ clientsClaim์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ณด์ธ๋‹ค. ์„œ๋น„์Šค ์›Œ์ปค๋Š” ์ตœ์ดˆ ๋“ฑ๋ก๋œ ํ›„ ๋‹ค์Œ ๋กœ๋“œ์‹œ๊นŒ์ง€ ์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ skipWaiting๊ณผ clientsClaim์„ ์ ์šฉํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ํŽ˜์ด์ง€๋ฅผ ์ฆ‰์‹œ ์ œ์–ดํ•œ๋‹ค.

skipWaiting์€ workbox์—์„œ ์‚ฌ๋ผ์งˆ ๊ฒƒ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋Œ€์‹  ์„œ๋น„์Šค์›Œ์ปค์—์„œ self.skipWaiting()์„ ์‚ฌ์šฉํ•œ๋‹ค.

Precaching with Workbox

Precaching์€ ์„œ๋น„์Šค ์›Œ์ปค์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ž‘์—… ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ Workbox๋Š” ์ด์— ๋Œ€ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

์šฐ์„  ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • generateSW
  • injectManifest

Precaching with generateSW

generateSW๋Š” Workbox์— ์บ์‹ฑํ•˜๊ธฐ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ด๋‹ค.

generateSW์— ๋Œ€ํ•ด ๊ธฐ์–ตํ•ด์•ผ ํ•  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ ์„œ๋น„์Šค์›Œ์ปค๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Workbox์— ์„œ๋น„์Šค ์›Œ์ปค๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ์š”์ฒญํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ, ๋‹ค์–‘ํ•œ ์˜ต์…˜์„ ํ†ตํ•ด ๋™์ž‘์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ workbox-webpack-plugin์€ ์›นํŒฉ์ด ์ข…์†์„ฑ ๊ทธ๋ž˜ํ”„์— ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ์„ ๋ฏธ๋ฆฌ ์บ์‹ฑํ•˜๊ณ  service-worker.js๋ผ๋Š” ์„œ๋น„์Šค ์›Œ์ปค๋ฅผ output.path์— ์ €์žฅ๋œ ๋””๋ ‰ํ† ๋ฆฌ์— ์“ด๋‹ค.

์œ„์™€ ๊ฐ™์ด ์ด๋ฏธ workbox ๊ด€๋ จ๋œ ํ”Œ๋Ÿฌ๊ทธ์ธ์ด next-pwa๋ฅผ ์„ค์น˜ํ•˜๋ฉด์„œ ๊น”๋ฆฐ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Warning: If too many assets in your project are precached with the default settings, use one of the glob options to exclude resources in the generateSW configuration. When using workbox-webpack-plugin, consult the plugin's GenerateSW documentation to find out how to prevent unwanted assets from being precached, as its configuration differs from generateSW.

์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

  • Precaching: ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ ‘์†ํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ์บ์‹ฑํ•ด๋†“๋Š” ์บ์‹ฑ ๋ฐฉ๋ฒ•.
  • Runtime Caching: ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋™์ž‘ ์‹œํ‚ฌ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์š”์ฒญ์— ๋Œ€ํ•ด ์„œ๋ฒ„ ์‘๋‹ต์„ ์บ์‹œ ์Šคํ† ๋ฆฌ์ง€์— ์ €์žฅํ•˜๋Š” ์บ์‹ฑ ๋ฐฉ๋ฒ•. ๋ณดํ†ต ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค๋Š” ํŒŒ์ผ ์šฉ๋Ÿ‰์ด ์ปค์„œ ์ฒ˜์Œ ํŽ˜์ด์ง€ ์ ‘์†ํ•  ๋•Œ ์„œ๋น„์Šค ์›Œ์ปค ์„ค์น˜ ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๋ฏ€๋กœ ๋Ÿฐํƒ€์ž„ ์บ์‹ฑ์œผ๋กœ ํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค.
  • Strategies
  • Request Routing
  • Background Sync
  • Helpful debugging

์ฐธ๊ณ