@serwist/turbopack 9.3.0 → 9.4.0

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.
@@ -1,8 +1,11 @@
1
1
  import { Serwist } from "@serwist/window";
2
2
  import { type ReactNode } from "react";
3
+ import { useSerwist } from "./lib/context.js";
3
4
  export interface SerwistProviderProps {
4
5
  swUrl: string;
6
+ disable?: boolean;
5
7
  register?: boolean;
8
+ cacheOnNavigation?: boolean;
6
9
  reloadOnOnline?: boolean;
7
10
  options?: RegistrationOptions;
8
11
  children?: ReactNode;
@@ -12,5 +15,11 @@ declare global {
12
15
  serwist: Serwist;
13
16
  }
14
17
  }
15
- export declare function SerwistProvider({ swUrl, register, reloadOnOnline, options, children }: SerwistProviderProps): import("react").JSX.Element;
18
+ /**
19
+ * `@serwist/window` provider for Next.js apps.
20
+ * @param options
21
+ * @returns
22
+ */
23
+ export declare function SerwistProvider({ swUrl, disable, register, cacheOnNavigation, reloadOnOnline, options, children, }: SerwistProviderProps): import("react").JSX.Element;
24
+ export { useSerwist };
16
25
  //# sourceMappingURL=index.react.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.react.d.ts","sourceRoot":"","sources":["../src/index.react.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,KAAK,SAAS,EAAuB,MAAM,OAAO,CAAC;AAG5D,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,OAAO,EAAE,OAAO,CAAC;KAClB;CACF;AAED,wBAAgB,eAAe,CAAC,EAAE,KAAK,EAAE,QAAe,EAAE,cAAqB,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,oBAAoB,+BAwBzH"}
1
+ {"version":3,"file":"index.react.d.ts","sourceRoot":"","sources":["../src/index.react.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,KAAK,SAAS,EAAuB,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAkB,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9D,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,OAAO,EAAE,OAAO,CAAC;KAClB;CACF;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,OAAe,EACf,QAAe,EACf,iBAAwB,EACxB,cAAqB,EACrB,OAAO,EACP,QAAQ,GACT,EAAE,oBAAoB,+BAuDtB;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -1,31 +1,70 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
2
  import { Serwist } from '@serwist/window';
3
3
  import { isCurrentPageOutOfScope } from '@serwist/window/internal';
4
- import { createContext, useState, useEffect } from 'react';
4
+ import { createContext, useContext, useState, useEffect } from 'react';
5
5
 
6
6
  const SerwistContext = createContext(null);
7
+ const useSerwist = ()=>{
8
+ const context = useContext(SerwistContext);
9
+ if (!context) {
10
+ throw new Error("[useSerwist]: 'SerwistContext' is not available.");
11
+ }
12
+ return context;
13
+ };
7
14
 
8
- function SerwistProvider({ swUrl, register = true, reloadOnOnline = true, options, children }) {
15
+ function SerwistProvider({ swUrl, disable = false, register = true, cacheOnNavigation = true, reloadOnOnline = true, options, children }) {
9
16
  const [serwist] = useState(()=>{
10
17
  if (typeof window === "undefined") return null;
18
+ if (disable) return null;
19
+ const scope = options?.scope || "/";
11
20
  if (!(window.serwist && window.serwist instanceof Serwist) && "serviceWorker" in navigator) {
12
21
  window.serwist = new Serwist(swUrl, {
13
22
  ...options,
14
- type: options?.type || "module",
15
- scope: options?.scope || "/"
23
+ scope,
24
+ type: options?.type || "module"
16
25
  });
26
+ if (register && !isCurrentPageOutOfScope(scope)) {
27
+ void window.serwist.register();
28
+ }
17
29
  }
18
30
  return window.serwist ?? null;
19
31
  });
20
32
  useEffect(()=>{
21
- const scope = options?.scope || "/";
22
- if (register && !isCurrentPageOutOfScope(scope)) {
23
- void serwist?.register();
33
+ const cacheUrls = async (url)=>{
34
+ if (!window.navigator.onLine || !url) {
35
+ return;
36
+ }
37
+ serwist?.messageSW({
38
+ type: "CACHE_URLS",
39
+ payload: {
40
+ urlsToCache: [
41
+ url
42
+ ]
43
+ }
44
+ });
45
+ };
46
+ const cacheCurrentPathname = ()=>cacheUrls(window.location.pathname);
47
+ const pushState = history.pushState;
48
+ const replaceState = history.replaceState;
49
+ if (cacheOnNavigation) {
50
+ history.pushState = (...args)=>{
51
+ pushState.apply(history, args);
52
+ cacheUrls(args[2]);
53
+ };
54
+ history.replaceState = (...args)=>{
55
+ replaceState.apply(history, args);
56
+ cacheUrls(args[2]);
57
+ };
58
+ window.addEventListener("online", cacheCurrentPathname);
24
59
  }
60
+ return ()=>{
61
+ history.pushState = pushState;
62
+ history.replaceState = replaceState;
63
+ window.removeEventListener("online", cacheCurrentPathname);
64
+ };
25
65
  }, [
26
- register,
27
- options?.scope,
28
- serwist?.register
66
+ serwist,
67
+ cacheOnNavigation
29
68
  ]);
30
69
  useEffect(()=>{
31
70
  const reload = ()=>location.reload();
@@ -46,4 +85,4 @@ function SerwistProvider({ swUrl, register = true, reloadOnOnline = true, option
46
85
  });
47
86
  }
48
87
 
49
- export { SerwistProvider };
88
+ export { SerwistProvider, useSerwist };
package/dist/types.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { BasePartial, BaseResolved, GlobPartial, GlobResolved, InjectPartial, InjectResolved, OptionalGlobDirectoryPartial, RequiredGlobDirectoryResolved } from "@serwist/build";
2
2
  import type { Prettify, Require } from "@serwist/utils";
3
3
  import type { BuildOptions } from "esbuild-wasm";
4
- import type { SUPPORTED_ESBUILD_OPTIONS } from "./lib/constants.js";
5
4
  import type { NextConfig as CompleteNextConfig } from "next";
5
+ import type { SUPPORTED_ESBUILD_OPTIONS } from "./lib/constants.js";
6
6
  export type EsbuildSupportedOptions = (typeof SUPPORTED_ESBUILD_OPTIONS)[number];
7
7
  export type EsbuildOptions = Pick<BuildOptions, EsbuildSupportedOptions>;
8
8
  export interface NextConfig extends Pick<CompleteNextConfig, "basePath" | "distDir"> {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,cAAc,EACd,4BAA4B,EAC5B,6BAA6B,EAC9B,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,KAAK,EAAE,UAAU,IAAI,kBAAkB,EAAE,MAAM,MAAM,CAAC;AAE7D,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,yBAAyB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjF,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,YAAY,EAAE,uBAAuB,CAAC,CAAC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,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/turbopack",
3
- "version": "9.3.0",
3
+ "version": "9.4.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "A module that integrates Serwist into your Next.js / Turbopack application.",
@@ -67,9 +67,9 @@
67
67
  "kolorist": "1.8.0",
68
68
  "semver": "7.7.3",
69
69
  "zod": "4.2.1",
70
- "@serwist/build": "9.3.0",
71
- "@serwist/window": "9.3.0",
72
- "serwist": "9.3.0"
70
+ "@serwist/build": "9.4.0",
71
+ "@serwist/window": "9.4.0",
72
+ "serwist": "9.4.0"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@types/node": "25.0.3",
@@ -82,8 +82,8 @@
82
82
  "rollup": "4.54.0",
83
83
  "type-fest": "5.3.1",
84
84
  "typescript": "5.9.3",
85
- "@serwist/configs": "9.3.0",
86
- "@serwist/utils": "9.3.0"
85
+ "@serwist/utils": "9.4.0",
86
+ "@serwist/configs": "9.4.0"
87
87
  },
88
88
  "peerDependencies": {
89
89
  "esbuild-wasm": ">=0.25.0 <1.0.0",
@@ -1,11 +1,13 @@
1
1
  import { Serwist } from "@serwist/window";
2
2
  import { isCurrentPageOutOfScope } from "@serwist/window/internal";
3
3
  import { type ReactNode, useEffect, useState } from "react";
4
- import { SerwistContext } from "./lib/context.js";
4
+ import { SerwistContext, useSerwist } from "./lib/context.js";
5
5
 
6
6
  export interface SerwistProviderProps {
7
7
  swUrl: string;
8
+ disable?: boolean;
8
9
  register?: boolean;
10
+ cacheOnNavigation?: boolean;
9
11
  reloadOnOnline?: boolean;
10
12
  options?: RegistrationOptions;
11
13
  children?: ReactNode;
@@ -17,20 +19,64 @@ declare global {
17
19
  }
18
20
  }
19
21
 
20
- export function SerwistProvider({ swUrl, register = true, reloadOnOnline = true, options, children }: SerwistProviderProps) {
22
+ /**
23
+ * `@serwist/window` provider for Next.js apps.
24
+ * @param options
25
+ * @returns
26
+ */
27
+ export function SerwistProvider({
28
+ swUrl,
29
+ disable = false,
30
+ register = true,
31
+ cacheOnNavigation = true,
32
+ reloadOnOnline = true,
33
+ options,
34
+ children,
35
+ }: SerwistProviderProps) {
21
36
  const [serwist] = useState(() => {
22
37
  if (typeof window === "undefined") return null;
38
+ if (disable) return null;
39
+ const scope = options?.scope || "/";
23
40
  if (!(window.serwist && window.serwist instanceof Serwist) && "serviceWorker" in navigator) {
24
- window.serwist = new Serwist(swUrl, { ...options, type: options?.type || "module", scope: options?.scope || "/" });
41
+ window.serwist = new Serwist(swUrl, { ...options, scope, type: options?.type || "module" });
42
+ if (register && !isCurrentPageOutOfScope(scope)) {
43
+ void window.serwist.register();
44
+ }
25
45
  }
26
46
  return window.serwist ?? null;
27
47
  });
28
48
  useEffect(() => {
29
- const scope = options?.scope || "/";
30
- if (register && !isCurrentPageOutOfScope(scope)) {
31
- void serwist?.register();
49
+ const cacheUrls = async (url?: string | URL | null | undefined) => {
50
+ if (!window.navigator.onLine || !url) {
51
+ return;
52
+ }
53
+ serwist?.messageSW({
54
+ type: "CACHE_URLS",
55
+ payload: { urlsToCache: [url] },
56
+ });
57
+ };
58
+ const cacheCurrentPathname = () => cacheUrls(window.location.pathname);
59
+ const pushState = history.pushState;
60
+ const replaceState = history.replaceState;
61
+
62
+ if (cacheOnNavigation) {
63
+ history.pushState = (...args) => {
64
+ pushState.apply(history, args);
65
+ cacheUrls(args[2]);
66
+ };
67
+ history.replaceState = (...args) => {
68
+ replaceState.apply(history, args);
69
+ cacheUrls(args[2]);
70
+ };
71
+ window.addEventListener("online", cacheCurrentPathname);
32
72
  }
33
- }, [register, options?.scope, serwist?.register]);
73
+
74
+ return () => {
75
+ history.pushState = pushState;
76
+ history.replaceState = replaceState;
77
+ window.removeEventListener("online", cacheCurrentPathname);
78
+ };
79
+ }, [serwist, cacheOnNavigation]);
34
80
  useEffect(() => {
35
81
  const reload = () => location.reload();
36
82
  if (reloadOnOnline) {
@@ -42,3 +88,5 @@ export function SerwistProvider({ swUrl, register = true, reloadOnOnline = true,
42
88
  }, [reloadOnOnline]);
43
89
  return <SerwistContext.Provider value={{ serwist }}>{children}</SerwistContext.Provider>;
44
90
  }
91
+
92
+ export { useSerwist };
@@ -1,5 +1,5 @@
1
1
  import path from "node:path";
2
- import { assertType, type Equals, basePartial, globPartial, injectPartial } from "@serwist/build/schema";
2
+ import { assertType, basePartial, type Equals, globPartial, injectPartial } from "@serwist/build/schema";
3
3
  import z from "zod";
4
4
  import { SUPPORTED_ESBUILD_OPTIONS } from "./lib/constants.js";
5
5
  import { generateGlobPatterns } from "./lib/utils.js";
package/src/types.ts CHANGED
@@ -10,8 +10,8 @@ import type {
10
10
  } from "@serwist/build";
11
11
  import type { Prettify, Require } from "@serwist/utils";
12
12
  import type { BuildOptions } from "esbuild-wasm";
13
- import type { SUPPORTED_ESBUILD_OPTIONS } from "./lib/constants.js";
14
13
  import type { NextConfig as CompleteNextConfig } from "next";
14
+ import type { SUPPORTED_ESBUILD_OPTIONS } from "./lib/constants.js";
15
15
 
16
16
  export type EsbuildSupportedOptions = (typeof SUPPORTED_ESBUILD_OPTIONS)[number];
17
17