@farm.js/pwa 0.1.0-beta.0 → 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # @farm.js/pwa
2
2
 
3
3
  Installable, offline-aware Progressive Web Apps for Farm.js. The plugin generates a route-aware
4
- service worker during the production build and registers it through Farm's browser plugin lifecycle.
4
+ service worker or copies a custom one during the production build, then registers it through Farm's
5
+ browser plugin lifecycle.
5
6
 
6
7
  Farm.js is currently in beta.
7
8
 
@@ -21,7 +22,7 @@ export default defineConfig({
21
22
  plugins: [
22
23
  pwa({
23
24
  offline: "/offline",
24
- cache: "recommended",
25
+ cache: "auto",
25
26
  }),
26
27
  ],
27
28
  });
@@ -33,15 +34,18 @@ does not duplicate it.
33
34
 
34
35
  ## Short cache configuration
35
36
 
36
- `recommended` precaches emitted static pages and uses SWR for public same-origin images:
37
+ `auto` precaches emitted static pages and uses SWR for public same-origin images. It is the default,
38
+ so you can omit `cache` when you want this behavior:
37
39
 
38
40
  ```ts
39
41
  pwa({
40
42
  offline: "/offline",
41
- cache: "recommended",
43
+ cache: "auto",
42
44
  });
43
45
  ```
44
46
 
47
+ The previous `recommended` value remains available as a compatibility alias for `auto`.
48
+
45
49
  The explicit short form is:
46
50
 
47
51
  ```ts
@@ -71,6 +75,25 @@ pwa({
71
75
  });
72
76
  ```
73
77
 
78
+ ## Bring your own service worker
79
+
80
+ Advanced applications can replace the generated worker with a prebuilt JavaScript file:
81
+
82
+ ```ts
83
+ pwa({
84
+ serviceWorker: {
85
+ source: "src/service-worker.js",
86
+ type: "module",
87
+ },
88
+ });
89
+ ```
90
+
91
+ The source path is relative to the Farm project root. Farm copies the file verbatim to the final
92
+ `sw.js` location and keeps its production registration and update lifecycle. A custom worker owns
93
+ all fetch, offline, and cache behavior, so `serviceWorker` cannot be combined with `offline` or
94
+ `cache`. It must handle the `FARM_PWA_SKIP_WAITING` message for Farm's update action to activate a
95
+ waiting worker.
96
+
74
97
  ## Updates
75
98
 
76
99
  `update: "prompt"` is the default. The plugin dispatches `farm:pwa:update-available` when a new
package/dist/build.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ResolvedPwaOptions } from "./config.js";
2
2
  export interface PwaBuildInput {
3
+ root?: string;
3
4
  outputDir: string;
4
5
  publicDir?: string;
5
6
  preset: string;
@@ -7,6 +8,7 @@ export interface PwaBuildInput {
7
8
  options: ResolvedPwaOptions;
8
9
  }
9
10
  export interface PwaBuildResult {
11
+ mode: "generated" | "custom";
10
12
  workerPath: string;
11
13
  workerUrl: string;
12
14
  precacheUrls: string[];
@@ -1 +1 @@
1
- {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,kBAAkB,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,OAAO,EAAE,MAAM,CAAC;CACjB;AAcD,wBAAsB,sBAAsB,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC,CA0E1F;AAED,MAAM,WAAW,4BAA4B;IAC3C,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,YAAY,EAAE,MAAM,GAAG,KAAK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AAE,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,YAAY,EAAE,MAAM,GAAG,KAAK,CAAC;IAC7B,MAAM,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC1B,MAAM,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC;CAC/C;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,4BAA4B,GAAG,MAAM,CAkJnF;AAED,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAK7E;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAGnE;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAQpE"}
package/dist/build.js CHANGED
@@ -14,10 +14,31 @@ const PRECACHE_EXTENSIONS = new Set([
14
14
  ]);
15
15
  export async function writePwaBuildArtifacts(input) {
16
16
  const publicDir = input.publicDir ?? resolvePwaPublicDir(input.outputDir, input.preset);
17
- const files = await walkFiles(publicDir);
18
- const staticRouteFiles = resolveStaticRouteFiles(files);
19
17
  const basePath = normalizeBasePath(input.basePath);
20
18
  const workerRelativePath = path.posix.join(basePath.replace(/^\//, ""), "sw.js");
19
+ const workerPath = path.join(publicDir, ...workerRelativePath.split("/"));
20
+ if (input.options.serviceWorker) {
21
+ const sourcePath = path.resolve(input.root ?? process.cwd(), input.options.serviceWorker.source);
22
+ let source;
23
+ try {
24
+ source = await readFile(sourcePath);
25
+ }
26
+ catch (cause) {
27
+ throw new Error(`[farm:pwa] Custom service worker ${JSON.stringify(input.options.serviceWorker.source)} could not be read.`, { cause });
28
+ }
29
+ await mkdir(path.dirname(workerPath), { recursive: true });
30
+ await writeFile(workerPath, source);
31
+ return {
32
+ mode: "custom",
33
+ workerPath,
34
+ workerUrl: withBasePath("/sw.js", basePath),
35
+ precacheUrls: [],
36
+ staticRoutes: {},
37
+ cacheId: createHash("sha256").update(source).digest("hex").slice(0, 16),
38
+ };
39
+ }
40
+ const files = await walkFiles(publicDir);
41
+ const staticRouteFiles = resolveStaticRouteFiles(files);
21
42
  const selectedRoutes = selectStaticRoutes(staticRouteFiles, input.options.cache.staticRoutes, basePath);
22
43
  const configuredOfflineRoute = input.options.offline;
23
44
  const offlineRoute = configuredOfflineRoute
@@ -48,7 +69,6 @@ export async function writePwaBuildArtifacts(input) {
48
69
  }))
49
70
  .digest("hex")
50
71
  .slice(0, 16);
51
- const workerPath = path.join(publicDir, ...workerRelativePath.split("/"));
52
72
  await mkdir(path.dirname(workerPath), { recursive: true });
53
73
  await writeFile(workerPath, generateServiceWorker({
54
74
  cacheId,
@@ -59,6 +79,7 @@ export async function writePwaBuildArtifacts(input) {
59
79
  images: input.options.cache.images,
60
80
  }));
61
81
  return {
82
+ mode: "generated",
62
83
  workerPath,
63
84
  workerUrl: withBasePath("/sw.js", basePath),
64
85
  precacheUrls,
package/dist/config.d.ts CHANGED
@@ -14,6 +14,12 @@ export interface PwaCacheOptions {
14
14
  /** Opt in to same-origin public image caching. */
15
15
  images?: PwaImageCache;
16
16
  }
17
+ export interface PwaServiceWorkerOptions {
18
+ /** JavaScript file copied verbatim into the production output as sw.js. */
19
+ source: string;
20
+ /** Registration type for the custom worker. */
21
+ type?: "classic" | "module";
22
+ }
17
23
  export interface PwaPluginOptions {
18
24
  /** Set false to keep the plugin registered without emitting or registering a worker. */
19
25
  enabled?: boolean;
@@ -21,8 +27,10 @@ export interface PwaPluginOptions {
21
27
  offline?: string | false;
22
28
  /** How a waiting service worker becomes active. */
23
29
  update?: "prompt" | "auto";
24
- /** Recommended caching, custom caching, or build assets only. */
25
- cache?: "recommended" | PwaCacheOptions | false;
30
+ /** Generated-worker caching, a custom cache policy, or build assets only. */
31
+ cache?: "auto" | "recommended" | PwaCacheOptions | false;
32
+ /** Copy and register a prebuilt service worker instead of generating one. */
33
+ serviceWorker?: PwaServiceWorkerOptions;
26
34
  }
27
35
  export interface ResolvedPwaImageCacheOptions {
28
36
  strategy: "swr";
@@ -33,6 +41,7 @@ export interface ResolvedPwaOptions {
33
41
  enabled: boolean;
34
42
  offline: string | false;
35
43
  update: "prompt" | "auto";
44
+ serviceWorker: Required<PwaServiceWorkerOptions> | false;
36
45
  cache: {
37
46
  staticRoutes: boolean | string[];
38
47
  images: ResolvedPwaImageCacheOptions | false;
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,GAAG,MAAM,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,EAAE,CAAC;AAE7E,MAAM,WAAW,oBAAoB;IACnC,0EAA0E;IAC1E,QAAQ,EAAE,KAAK,CAAC;IAChB,0EAA0E;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,GAAG,CAAC,EAAE,WAAW,CAAC;CACnB;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,KAAK,GAAG,oBAAoB,CAAC;AAEnE,MAAM,WAAW,eAAe;IAC9B,+EAA+E;IAC/E,YAAY,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IAClC,kDAAkD;IAClD,MAAM,CAAC,EAAE,aAAa,CAAC;CACxB;AAED,MAAM,WAAW,gBAAgB;IAC/B,wFAAwF;IACxF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,mDAAmD;IACnD,MAAM,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC3B,iEAAiE;IACjE,KAAK,CAAC,EAAE,aAAa,GAAG,eAAe,GAAG,KAAK,CAAC;CACjD;AAED,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,E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AAM,WAAW,gBAAgB;IAC/B,wFAAwF;IACxF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,mDAAmD;IACnD,MAAM,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC3B,6EAA6E;IAC7E,KAAK,CAAC,EAAE,MAAM,GAAG,aAAa,GAAG,eAAe,GAAG,KAAK,CAAC;IACzD,6EAA6E;IAC7E,aAAa,CAAC,EAAE,uBAAuB,CAAC;CACzC;AAED,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,EAAE,KAAK,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC;IACxB,MAAM,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC1B,aAAa,EAAE,QAAQ,CAAC,uBAAuB,CAAC,GAAG,KAAK,CAAC;IACzD,KAAK,EAAE;QACL,YAAY,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;QACjC,MAAM,EAAE,4BAA4B,GAAG,KAAK,CAAC;KAC9C,CAAC;CACH;AAKD,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,gBAAqB,GAAG,kBAAkB,CAkCpF;AAcD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CA0B3D"}
package/dist/config.js CHANGED
@@ -1,26 +1,47 @@
1
1
  const DEFAULT_IMAGE_LIMIT = 100;
2
2
  const DEFAULT_IMAGE_TTL = "30d";
3
3
  export function resolvePwaOptions(options = {}) {
4
- const cache = options.cache ?? "recommended";
4
+ const serviceWorker = resolveServiceWorker(options.serviceWorker);
5
+ if (serviceWorker && (options.offline !== undefined || options.cache !== undefined)) {
6
+ throw new TypeError("PWA serviceWorker cannot be combined with offline or cache because the custom worker owns those behaviors");
7
+ }
8
+ const cache = options.cache ?? "auto";
9
+ const usesAutomaticCache = cache === "auto" || cache === "recommended";
5
10
  const customCache = typeof cache === "object" ? cache : undefined;
6
11
  return {
7
12
  enabled: options.enabled !== false,
8
- offline: normalizeRoute(options.offline ?? false, "offline"),
13
+ offline: serviceWorker ? false : normalizeRoute(options.offline ?? false, "offline"),
9
14
  update: options.update ?? "prompt",
15
+ serviceWorker,
10
16
  cache: {
11
- staticRoutes: cache === "recommended"
12
- ? true
13
- : cache === false
14
- ? false
15
- : normalizeStaticRoutes(customCache?.staticRoutes ?? false),
16
- images: cache === "recommended"
17
- ? resolveImageCache("swr")
18
- : cache === false
19
- ? false
20
- : resolveImageCache(customCache?.images ?? false),
17
+ staticRoutes: serviceWorker
18
+ ? false
19
+ : usesAutomaticCache
20
+ ? true
21
+ : cache === false
22
+ ? false
23
+ : normalizeStaticRoutes(customCache?.staticRoutes ?? false),
24
+ images: serviceWorker
25
+ ? false
26
+ : usesAutomaticCache
27
+ ? resolveImageCache("swr")
28
+ : cache === false
29
+ ? false
30
+ : resolveImageCache(customCache?.images ?? false),
21
31
  },
22
32
  };
23
33
  }
34
+ function resolveServiceWorker(value) {
35
+ if (!value)
36
+ return false;
37
+ const source = value.source.trim();
38
+ if (!source)
39
+ throw new TypeError("PWA serviceWorker source must be a non-empty path");
40
+ if (value.type !== undefined && value.type !== "classic" && value.type !== "module") {
41
+ throw new TypeError('PWA serviceWorker type must be "classic" or "module"');
42
+ }
43
+ return { source, type: value.type ?? "classic" };
44
+ }
24
45
  export function parsePwaDuration(value) {
25
46
  if (typeof value === "number") {
26
47
  if (!Number.isFinite(value) || value <= 0) {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { type PwaCacheOptions, type PwaDuration, type PwaImageCache, type PwaImageCacheOptions, type PwaPluginOptions } from "./config.js";
2
- export type { PwaCacheOptions, PwaDuration, PwaImageCache, PwaImageCacheOptions, PwaPluginOptions };
1
+ import { type PwaCacheOptions, type PwaDuration, type PwaImageCache, type PwaImageCacheOptions, type PwaPluginOptions, type PwaServiceWorkerOptions } from "./config.js";
2
+ export type { PwaCacheOptions, PwaDuration, PwaImageCache, PwaImageCacheOptions, PwaPluginOptions, PwaServiceWorkerOptions, };
3
3
  export interface FarmPwaBrowserRuntime {
4
4
  registration: ServiceWorkerRegistration;
5
5
  applyUpdate(): void;
@@ -15,9 +15,9 @@ declare global {
15
15
  }
16
16
  }
17
17
  /**
18
- * Generate and register a route-aware service worker for a Farm application.
19
- * Build assets are always precached. Optional page and image caching stays
20
- * constrained to safe GET navigation and same-origin image requests.
18
+ * Generate or copy and then register a service worker for a Farm application.
19
+ * In generated mode, build assets are always precached while optional page and
20
+ * image caching stays constrained to safe GET navigation and same-origin images.
21
21
  */
22
22
  export declare function pwa(options?: PwaPluginOptions): import("@farm.js/core/plugin").FarmPlugin<undefined, Record<string, never>, {
23
23
  cleanup(): void;
@@ -26,5 +26,6 @@ export declare function pwa(options?: PwaPluginOptions): import("@farm.js/core/p
26
26
  workerUrl: string;
27
27
  scope: string;
28
28
  update: "prompt" | "auto";
29
+ workerType: "classic" | "module";
29
30
  }, unknown, false>;
30
31
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,eAAe,EAAE,WAAW,EAAE,aAAa,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,CAAC;AAEpG,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,yBAAyB,CAAC;IACxC,WAAW,IAAI,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,wBAAwB;IACvC,YAAY,EAAE,yBAAyB,CAAC;IACxC,WAAW,IAAI,IAAI,CAAC;CACrB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,mDAAmD;QACnD,YAAY,CAAC,EAAE,qBAAqB,CAAC;KACtC;CACF;AAED;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,OAAO,GAAE,gBAAqB;;;;;;;mBAoIjD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,uBAAuB,EAC7B,MAAM,aAAa,CAAC;AAErB,YAAY,EACV,eAAe,EACf,WAAW,EACX,aAAa,EACb,oBAAoB,EACpB,gBAAgB,EAChB,uBAAuB,GACxB,CAAC;AAEF,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,yBAAyB,CAAC;IACxC,WAAW,IAAI,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,wBAAwB;IACvC,YAAY,EAAE,yBAAyB,CAAC;IACxC,WAAW,IAAI,IAAI,CAAC;CACrB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,mDAAmD;QACnD,YAAY,CAAC,EAAE,qBAAqB,CAAC;KACtC;CACF;AAED;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,OAAO,GAAE,gBAAqB;;;;;;;;mBA6IjD"}
package/dist/index.js CHANGED
@@ -2,12 +2,13 @@ import { definePlugin } from "@farm.js/core/plugin";
2
2
  import { normalizeBasePath, withBasePath, writePwaBuildArtifacts } from "./build.js";
3
3
  import { resolvePwaOptions, } from "./config.js";
4
4
  /**
5
- * Generate and register a route-aware service worker for a Farm application.
6
- * Build assets are always precached. Optional page and image caching stays
7
- * constrained to safe GET navigation and same-origin image requests.
5
+ * Generate or copy and then register a service worker for a Farm application.
6
+ * In generated mode, build assets are always precached while optional page and
7
+ * image caching stays constrained to safe GET navigation and same-origin images.
8
8
  */
9
9
  export function pwa(options = {}) {
10
10
  const resolved = resolvePwaOptions(options);
11
+ let configuredRoot = ".";
11
12
  let configuredBasePath = "/";
12
13
  let configuredOutputDir = ".farm/.output";
13
14
  const publicConfig = {
@@ -15,10 +16,12 @@ export function pwa(options = {}) {
15
16
  workerUrl: "/sw.js",
16
17
  scope: "/",
17
18
  update: resolved.update,
19
+ workerType: resolved.serviceWorker ? resolved.serviceWorker.type : "classic",
18
20
  };
19
21
  return definePlugin({
20
22
  name: "farm:pwa",
21
23
  configure(config) {
24
+ configuredRoot = config.root ?? ".";
22
25
  const basePath = normalizeBasePath(config.basePath);
23
26
  configuredBasePath = basePath;
24
27
  configuredOutputDir = `${config.root ?? "."}/.farm/.output`;
@@ -37,6 +40,7 @@ export function pwa(options = {}) {
37
40
  if (generated)
38
41
  return;
39
42
  const built = await writePwaBuildArtifacts({
43
+ root: configuredRoot,
40
44
  outputDir: nitroConfig.output?.dir ?? configuredOutputDir,
41
45
  publicDir: nitroConfig.output?.publicDir,
42
46
  preset: nitroConfig.preset ?? "node-server",
@@ -44,7 +48,9 @@ export function pwa(options = {}) {
44
48
  options: resolved,
45
49
  });
46
50
  generated = true;
47
- console.info(`[farm:pwa] Generated ${built.workerUrl} with ${built.precacheUrls.length} precached files`);
51
+ console.info(built.mode === "custom"
52
+ ? `[farm:pwa] Copied custom service worker to ${built.workerUrl}`
53
+ : `[farm:pwa] Generated ${built.workerUrl} with ${built.precacheUrls.length} precached files`);
48
54
  };
49
55
  return {
50
56
  ...nitroConfig,
@@ -69,9 +75,9 @@ export function pwa(options = {}) {
69
75
  async setup({ public: config, isProd }) {
70
76
  if (!config.enabled || !isProd || !("serviceWorker" in navigator))
71
77
  return undefined;
72
- const registration = await navigator.serviceWorker.register(config.workerUrl, {
73
- scope: config.scope,
74
- });
78
+ const registration = await navigator.serviceWorker.register(config.workerUrl, config.workerType === "module"
79
+ ? { scope: config.scope, type: "module" }
80
+ : { scope: config.scope });
75
81
  const hadControllerAtSetup = Boolean(navigator.serviceWorker.controller);
76
82
  let applyingUpdate = false;
77
83
  let reloading = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farm.js/pwa",
3
- "version": "0.1.0-beta.0",
3
+ "version": "0.1.0-beta.1",
4
4
  "description": "Installable, offline-aware Progressive Web Apps for Farm.js",
5
5
  "keywords": [
6
6
  "farm.js",
@@ -32,7 +32,7 @@
32
32
  "access": "public"
33
33
  },
34
34
  "dependencies": {
35
- "@farm.js/core": "0.1.0-beta.68"
35
+ "@farm.js/core": "0.1.0-beta.69"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^20.10.5",