@serwist/turbopack 9.3.0 → 9.3.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.
@@ -1,8 +1,10 @@
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;
5
6
  register?: boolean;
7
+ cacheOnNavigation?: boolean;
6
8
  reloadOnOnline?: boolean;
7
9
  options?: RegistrationOptions;
8
10
  children?: ReactNode;
@@ -12,5 +14,6 @@ declare global {
12
14
  serwist: Serwist;
13
15
  }
14
16
  }
15
- export declare function SerwistProvider({ swUrl, register, reloadOnOnline, options, children }: SerwistProviderProps): import("react").JSX.Element;
17
+ export declare function SerwistProvider({ swUrl, register, cacheOnNavigation, reloadOnOnline, options, children, }: SerwistProviderProps): import("react").JSX.Element;
18
+ export { useSerwist };
16
19
  //# 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,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,wBAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,QAAe,EACf,iBAAwB,EACxB,cAAqB,EACrB,OAAO,EACP,QAAQ,GACT,EAAE,oBAAoB,+BAwDtB;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -1,11 +1,18 @@
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, register = true, cacheOnNavigation = true, reloadOnOnline = true, options, children }) {
9
16
  const [serwist] = useState(()=>{
10
17
  if (typeof window === "undefined") return null;
11
18
  if (!(window.serwist && window.serwist instanceof Serwist) && "serviceWorker" in navigator) {
@@ -27,6 +34,43 @@ function SerwistProvider({ swUrl, register = true, reloadOnOnline = true, option
27
34
  options?.scope,
28
35
  serwist?.register
29
36
  ]);
37
+ useEffect(()=>{
38
+ const cacheUrls = async (url)=>{
39
+ if (!window.navigator.onLine || !url) {
40
+ return;
41
+ }
42
+ serwist?.messageSW({
43
+ type: "CACHE_URLS",
44
+ payload: {
45
+ urlsToCache: [
46
+ url
47
+ ]
48
+ }
49
+ });
50
+ };
51
+ const cacheCurrentPathname = ()=>cacheUrls(window.location.pathname);
52
+ const pushState = history.pushState;
53
+ const replaceState = history.replaceState;
54
+ if (cacheOnNavigation) {
55
+ history.pushState = (...args)=>{
56
+ pushState.apply(history, args);
57
+ cacheUrls(args[2]);
58
+ };
59
+ history.replaceState = (...args)=>{
60
+ replaceState.apply(history, args);
61
+ cacheUrls(args[2]);
62
+ };
63
+ window.addEventListener("online", cacheCurrentPathname);
64
+ }
65
+ return ()=>{
66
+ history.pushState = pushState;
67
+ history.replaceState = replaceState;
68
+ window.removeEventListener("online", cacheCurrentPathname);
69
+ };
70
+ }, [
71
+ serwist?.messageSW,
72
+ cacheOnNavigation
73
+ ]);
30
74
  useEffect(()=>{
31
75
  const reload = ()=>location.reload();
32
76
  if (reloadOnOnline) {
@@ -46,4 +90,4 @@ function SerwistProvider({ swUrl, register = true, reloadOnOnline = true, option
46
90
  });
47
91
  }
48
92
 
49
- export { SerwistProvider };
93
+ 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;AAEzE,MAAM,WAAW,UAAW,SAAQ,IAAI,CAAC,kBAAkB,EAAE,UAAU,GAAG,SAAS,CAAC;IAClF;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;;;;OAOG;IACH,UAAU,EAAE,QAAQ,CAAC,UAAU,CAA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U,GAAG,SAAS,CAAC;IAClF;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;;;;OAOG;IACH,UAAU,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;IACjC;;;OAGG;IACH,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;AAED,MAAM,WAAW,aAAc,SAAQ,OAAO,CAAC,YAAY,EAAE,KAAK,GAAG,gBAAgB,CAAC;IACpF,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,CAAC,CAAC;CACzD;AAED,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAC1C,IAAI,CAAC,WAAW,GAAG,WAAW,GAAG,aAAa,GAAG,4BAA4B,GAAG,YAAY,EAAE,yBAAyB,CAAC,CACzH,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG,QAAQ,CAClD,IAAI,CACF,OAAO,CAAC,YAAY,EAAE,2BAA2B,CAAC,GAAG,YAAY,GAAG,cAAc,GAAG,6BAA6B,GAAG,aAAa,EAClI,yBAAyB,CAC1B,CACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/turbopack",
3
- "version": "9.3.0",
3
+ "version": "9.3.1",
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/window": "9.3.1",
71
+ "serwist": "9.3.1",
72
+ "@serwist/build": "9.3.1"
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/configs": "9.3.1",
86
+ "@serwist/utils": "9.3.1"
87
87
  },
88
88
  "peerDependencies": {
89
89
  "esbuild-wasm": ">=0.25.0 <1.0.0",
@@ -1,11 +1,12 @@
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
8
  register?: boolean;
9
+ cacheOnNavigation?: boolean;
9
10
  reloadOnOnline?: boolean;
10
11
  options?: RegistrationOptions;
11
12
  children?: ReactNode;
@@ -17,7 +18,14 @@ declare global {
17
18
  }
18
19
  }
19
20
 
20
- export function SerwistProvider({ swUrl, register = true, reloadOnOnline = true, options, children }: SerwistProviderProps) {
21
+ export function SerwistProvider({
22
+ swUrl,
23
+ register = true,
24
+ cacheOnNavigation = true,
25
+ reloadOnOnline = true,
26
+ options,
27
+ children,
28
+ }: SerwistProviderProps) {
21
29
  const [serwist] = useState(() => {
22
30
  if (typeof window === "undefined") return null;
23
31
  if (!(window.serwist && window.serwist instanceof Serwist) && "serviceWorker" in navigator) {
@@ -31,6 +39,38 @@ export function SerwistProvider({ swUrl, register = true, reloadOnOnline = true,
31
39
  void serwist?.register();
32
40
  }
33
41
  }, [register, options?.scope, serwist?.register]);
42
+ useEffect(() => {
43
+ const cacheUrls = async (url?: string | URL | null | undefined) => {
44
+ if (!window.navigator.onLine || !url) {
45
+ return;
46
+ }
47
+ serwist?.messageSW({
48
+ type: "CACHE_URLS",
49
+ payload: { urlsToCache: [url] },
50
+ });
51
+ };
52
+ const cacheCurrentPathname = () => cacheUrls(window.location.pathname);
53
+ const pushState = history.pushState;
54
+ const replaceState = history.replaceState;
55
+
56
+ if (cacheOnNavigation) {
57
+ history.pushState = (...args) => {
58
+ pushState.apply(history, args);
59
+ cacheUrls(args[2]);
60
+ };
61
+ history.replaceState = (...args) => {
62
+ replaceState.apply(history, args);
63
+ cacheUrls(args[2]);
64
+ };
65
+ window.addEventListener("online", cacheCurrentPathname);
66
+ }
67
+
68
+ return () => {
69
+ history.pushState = pushState;
70
+ history.replaceState = replaceState;
71
+ window.removeEventListener("online", cacheCurrentPathname);
72
+ };
73
+ }, [serwist?.messageSW, cacheOnNavigation]);
34
74
  useEffect(() => {
35
75
  const reload = () => location.reload();
36
76
  if (reloadOnOnline) {
@@ -42,3 +82,5 @@ export function SerwistProvider({ swUrl, register = true, reloadOnOnline = true,
42
82
  }, [reloadOnOnline]);
43
83
  return <SerwistContext.Provider value={{ serwist }}>{children}</SerwistContext.Provider>;
44
84
  }
85
+
86
+ 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