@serwist/next 8.0.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.
@@ -0,0 +1,10 @@
1
+ export type SerwistNextOptionsKey = "self.__SERWIST_SW_ENTRY";
2
+ export interface SerwistNextOptions {
3
+ sw: string;
4
+ scope: string;
5
+ cacheOnFrontEndNav: boolean;
6
+ register: boolean;
7
+ reloadOnOnline: boolean;
8
+ swEntryWorker: string | undefined;
9
+ }
10
+ //# sourceMappingURL=internal-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal-types.d.ts","sourceRoot":"","sources":["../src/internal-types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,qBAAqB,GAAG,yBAAyB,CAAC;AAE9D,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,kBAAkB,EAAE,OAAO,CAAC;IAC5B,QAAQ,EAAE,OAAO,CAAC;IAClB,cAAc,EAAE,OAAO,CAAC;IACxB,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC"}
@@ -0,0 +1,8 @@
1
+ export type MessageType = {
2
+ type: "__FRONTEND_NAV_CACHE__";
3
+ url: URL | string;
4
+ } | {
5
+ type: "__START_URL_CACHE__";
6
+ url: URL | string;
7
+ };
8
+ //# sourceMappingURL=sw-entry-worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sw-entry-worker.d.ts","sourceRoot":"","sources":["../src/sw-entry-worker.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,WAAW,GACnB;IACE,IAAI,EAAE,wBAAwB,CAAC;IAC/B,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC;CACnB,GACD;IACE,IAAI,EAAE,qBAAqB,CAAC;IAC5B,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC;CACnB,CAAC"}
@@ -0,0 +1,33 @@
1
+ self.onmessage = async (ev)=>{
2
+ switch(ev.data.type){
3
+ case "__START_URL_CACHE__":
4
+ {
5
+ const url = ev.data.url;
6
+ const response = await fetch(url);
7
+ if (!response.redirected) {
8
+ const startUrlCache = await caches.open("start-url");
9
+ return startUrlCache.put(url, response);
10
+ }
11
+ return Promise.resolve();
12
+ }
13
+ case "__FRONTEND_NAV_CACHE__":
14
+ {
15
+ const url = ev.data.url;
16
+ const pagesCache = await caches.open("pages");
17
+ const isPageCached = !!await pagesCache.match(url, {
18
+ ignoreSearch: true
19
+ });
20
+ if (isPageCached) {
21
+ return;
22
+ }
23
+ const page = await fetch(url);
24
+ if (!page.ok) {
25
+ return;
26
+ }
27
+ pagesCache.put(url, page.clone());
28
+ return Promise.resolve();
29
+ }
30
+ default:
31
+ return Promise.resolve();
32
+ }
33
+ };
@@ -0,0 +1,7 @@
1
+ import { Serwist } from "@serwist/window";
2
+ declare global {
3
+ interface Window {
4
+ serwist: Serwist;
5
+ }
6
+ }
7
+ //# sourceMappingURL=sw-entry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sw-entry.d.ts","sourceRoot":"","sources":["../src/sw-entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAK1C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,OAAO,EAAE,OAAO,CAAC;KAClB;CACF"}
@@ -0,0 +1,41 @@
1
+ import { Serwist } from '@serwist/window';
2
+
3
+ if (typeof window !== "undefined" && "serviceWorker" in navigator && typeof caches !== "undefined") {
4
+ let swEntryWorker;
5
+ if (self.__SERWIST_SW_ENTRY.swEntryWorker) {
6
+ swEntryWorker = new Worker(self.__SERWIST_SW_ENTRY.swEntryWorker);
7
+ }
8
+ window.serwist = new Serwist(window.location.origin + self.__SERWIST_SW_ENTRY.sw, {
9
+ scope: self.__SERWIST_SW_ENTRY.scope
10
+ });
11
+ if (self.__SERWIST_SW_ENTRY.register) {
12
+ window.serwist.register();
13
+ }
14
+ if (self.__SERWIST_SW_ENTRY.cacheOnFrontEndNav) {
15
+ const cacheOnFrontEndNav = async (url)=>{
16
+ if (!window.navigator.onLine || !url) {
17
+ return;
18
+ }
19
+ swEntryWorker?.postMessage({
20
+ type: "__FRONTEND_NAV_CACHE__",
21
+ url
22
+ });
23
+ };
24
+ const pushState = history.pushState;
25
+ history.pushState = (...args)=>{
26
+ pushState.apply(history, args);
27
+ cacheOnFrontEndNav(args[2]);
28
+ };
29
+ const replaceState = history.replaceState;
30
+ history.replaceState = (...args)=>{
31
+ replaceState.apply(history, args);
32
+ cacheOnFrontEndNav(args[2]);
33
+ };
34
+ window.addEventListener("online", ()=>{
35
+ cacheOnFrontEndNav(window.location.pathname);
36
+ });
37
+ }
38
+ if (self.__SERWIST_SW_ENTRY.reloadOnOnline) {
39
+ window.addEventListener("online", ()=>location.reload());
40
+ }
41
+ }
@@ -0,0 +1,83 @@
1
+ import type { WebpackInjectManifestOptions } from "@serwist/build";
2
+ type Require<T, U extends keyof T> = T & Required<Pick<T, U>>;
3
+ export interface PluginOptions extends Require<WebpackInjectManifestOptions, "swDest"> {
4
+ /**
5
+ * Enable additional route caching when users navigate through pages with
6
+ * `next/link`. This improves the user experience in some cases but it
7
+ * also adds a bit of overhead due to additional network calls.
8
+ * @default false
9
+ */
10
+ cacheOnFrontEndNav?: boolean;
11
+ /**
12
+ * Whether Serwist should be disabled.
13
+ * @default false
14
+ */
15
+ disable?: boolean;
16
+ /**
17
+ * URL scope for PWA. Set to `/foo/` so that paths under `/foo/` are PWA while others
18
+ * are not.
19
+ * @default nextConfig.basePath
20
+ */
21
+ scope?: string;
22
+ /**
23
+ * The URL to the service worker.
24
+ * @default "/sw.js"
25
+ */
26
+ swUrl?: string;
27
+ /**
28
+ * Allow this plugin to automatically register the service worker for you. Set
29
+ * this to `false` if you want to register the service worker yourself, which
30
+ * can be done by running `window.serwist.register()` in
31
+ * `componentDidMount` or `useEffect`.
32
+ * @example
33
+ * ```tsx
34
+ * // app/register-pwa.tsx
35
+ * "use client";
36
+ * import { useEffect } from "react";
37
+ * import type { Serwist } from "@serwist/window";
38
+ *
39
+ * declare global {
40
+ * interface Window {
41
+ * serwist: Serwist;
42
+ * }
43
+ * }
44
+ *
45
+ * export default function RegisterPWA() {
46
+ * useEffect(() => {
47
+ * if ("serviceWorker" in navigator && window.serwist !== undefined) {
48
+ * window.serwist.register();
49
+ * }
50
+ * }, []);
51
+ * return <></>;
52
+ * }
53
+ *
54
+ * // app/layout.tsx
55
+ * import RegisterPWA from "./register-pwa";
56
+ *
57
+ * export default function RootLayout({
58
+ * children,
59
+ * }: {
60
+ * children: React.ReactNode;
61
+ * }) {
62
+ * return (
63
+ * <html lang="en">
64
+ * <head />
65
+ * <body>
66
+ * <RegisterPWA />
67
+ * {children}
68
+ * </body>
69
+ * </html>
70
+ * );
71
+ * }
72
+ * ```
73
+ * @default true
74
+ */
75
+ register?: boolean;
76
+ /**
77
+ * Reload the app when it has gone back online.
78
+ * @default true
79
+ */
80
+ reloadOnOnline?: boolean;
81
+ }
82
+ export {};
83
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAC;AAEnE,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE9D,MAAM,WAAW,aAAc,SAAQ,OAAO,CAAC,4BAA4B,EAAE,QAAQ,CAAC;IACpF;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+CG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Find the first truthy value in an array.
3
+ * @param arr
4
+ * @param fn
5
+ * @returns
6
+ */
7
+ export declare const findFirstTruthy: <T, U>(arr: T[], fn: (elm: T) => U) => NonNullable<U> | undefined;
8
+ //# sourceMappingURL=find-first-truthy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-first-truthy.d.ts","sourceRoot":"","sources":["../../src/utils/find-first-truthy.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,eAAe,mEAQ3B,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type fs from "node:fs";
2
+ export declare const getContentHash: (file: fs.PathOrFileDescriptor, isDev: boolean) => string;
3
+ //# sourceMappingURL=get-content-hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-content-hash.d.ts","sourceRoot":"","sources":["../../src/utils/get-content-hash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAI9B,eAAO,MAAM,cAAc,SAAU,GAAG,oBAAoB,SAAS,OAAO,WAK3E,CAAC"}
@@ -0,0 +1,3 @@
1
+ import fs from "node:fs";
2
+ export declare const getFileHash: (file: fs.PathOrFileDescriptor) => string;
3
+ //# sourceMappingURL=get-file-hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-file-hash.d.ts","sourceRoot":"","sources":["../../src/utils/get-file-hash.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,eAAO,MAAM,WAAW,SAAU,GAAG,oBAAoB,WAAyE,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Get a package's version
3
+ * @param packageName
4
+ * @returns
5
+ */
6
+ export declare const getPackageVersion: (packageName: string) => string | undefined;
7
+ //# sourceMappingURL=get-package-version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-package-version.d.ts","sourceRoot":"","sources":["../../src/utils/get-package-version.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,gBAAiB,MAAM,KAAG,MAAM,GAAG,SAMhE,CAAC"}
@@ -0,0 +1,7 @@
1
+ export { findFirstTruthy } from "./find-first-truthy.js";
2
+ export { getContentHash } from "./get-content-hash.js";
3
+ export { getFileHash } from "./get-file-hash.js";
4
+ export { getPackageVersion } from "./get-package-version.js";
5
+ export { loadTSConfig } from "./load-tsconfig.js";
6
+ export * as logger from "./logger.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { TsConfigJson as TSConfigJSON } from "type-fest";
2
+ export declare const loadTSConfig: (baseDir: string, relativeTSConfigPath: string | undefined) => TSConfigJSON | undefined;
3
+ //# sourceMappingURL=load-tsconfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load-tsconfig.d.ts","sourceRoot":"","sources":["../../src/utils/load-tsconfig.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,IAAI,YAAY,EAAE,MAAM,WAAW,CAAC;AAI9D,eAAO,MAAM,YAAY,YAAa,MAAM,wBAAwB,MAAM,GAAG,SAAS,KAAG,YAAY,GAAG,SAmBvG,CAAC"}
@@ -0,0 +1,6 @@
1
+ export declare const wait: (...message: any[]) => void;
2
+ export declare const error: (...message: any[]) => void;
3
+ export declare const warn: (...message: any[]) => void;
4
+ export declare const info: (...message: any[]) => void;
5
+ export declare const event: (...message: any[]) => void;
6
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAsCA,eAAO,MAAM,IAAI,eAAgB,GAAG,EAAE,SAErC,CAAC;AAEF,eAAO,MAAM,KAAK,eAAgB,GAAG,EAAE,SAEtC,CAAC;AAEF,eAAO,MAAM,IAAI,eAAgB,GAAG,EAAE,SAErC,CAAC;AAEF,eAAO,MAAM,IAAI,eAAgB,GAAG,EAAE,SAErC,CAAC;AAEF,eAAO,MAAM,KAAK,eAAgB,GAAG,EAAE,SAEtC,CAAC"}
@@ -0,0 +1,4 @@
1
+ import fs from "node:fs";
2
+ export declare const getFileHash: (file: fs.PathOrFileDescriptor) => string;
3
+ export declare const getContentHash: (file: fs.PathOrFileDescriptor, isDev: boolean) => string;
4
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,eAAO,MAAM,WAAW,SAAU,GAAG,oBAAoB,WAAyE,CAAC;AAEnI,eAAO,MAAM,cAAc,SAAU,GAAG,oBAAoB,SAAS,OAAO,WAK3E,CAAC"}
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@serwist/next",
3
+ "version": "8.0.0",
4
+ "type": "module",
5
+ "description": "A module that integrates Serwist into your Next.js application.",
6
+ "files": [
7
+ "dist",
8
+ "!dist/dts"
9
+ ],
10
+ "keywords": [
11
+ "serwist",
12
+ "serwistjs",
13
+ "sw",
14
+ "service worker",
15
+ "web",
16
+ "service-worker"
17
+ ],
18
+ "author": "Serwist's Team",
19
+ "license": "MIT",
20
+ "repository": "serwist/serwist",
21
+ "bugs": "https://github.com/serwist/serwist/issues",
22
+ "homepage": "https://ducanh-next-pwa.vercel.app",
23
+ "module": "./dist/index.js",
24
+ "main": "./dist/index.old.cjs",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.old.cjs",
30
+ "types": "./dist/index.d.ts"
31
+ },
32
+ "./browser": {
33
+ "import": "./dist/index.browser.js",
34
+ "require": "./dist/index.browser.old.cjs",
35
+ "types": "./dist/index.browser.d.ts"
36
+ },
37
+ "./typings": {
38
+ "import": "./dist/sw-entry.d.ts",
39
+ "require": "./dist/sw-entry.d.ts"
40
+ },
41
+ "./package.json": "./package.json"
42
+ },
43
+ "typesVersions": {
44
+ "*": {
45
+ "browser": [
46
+ "./dist/index.browser.d.ts"
47
+ ],
48
+ "typings": [
49
+ "./dist/sw-entry.d.ts"
50
+ ]
51
+ }
52
+ },
53
+ "dependencies": {
54
+ "clean-webpack-plugin": "4.0.0",
55
+ "fast-glob": "3.3.1",
56
+ "semver": "7.5.4",
57
+ "terser-webpack-plugin": "5.3.9",
58
+ "@serwist/build": "8.0.0",
59
+ "@serwist/core": "8.0.0",
60
+ "@serwist/webpack-plugin": "8.0.0",
61
+ "@serwist/window": "8.0.0"
62
+ },
63
+ "devDependencies": {
64
+ "@rollup/plugin-alias": "5.0.1",
65
+ "@rollup/plugin-json": "6.0.1",
66
+ "@rollup/plugin-node-resolve": "15.2.3",
67
+ "@rollup/plugin-swc": "0.2.1",
68
+ "@rollup/plugin-typescript": "11.1.5",
69
+ "@swc/core": "1.3.93",
70
+ "@types/node": "20.8.7",
71
+ "@types/semver": "7.5.3",
72
+ "chalk": "5.3.0",
73
+ "next": "13.5.5",
74
+ "react": "18.2.0",
75
+ "react-dom": "18.2.0",
76
+ "rollup": "3.28.1",
77
+ "rollup-plugin-dts": "6.0.2",
78
+ "type-fest": "4.5.0",
79
+ "typescript": "5.3.0-dev.20231011",
80
+ "webpack": "5.89.0",
81
+ "@serwist/constants": "8.0.0"
82
+ },
83
+ "peerDependencies": {
84
+ "next": ">=14.0.0",
85
+ "webpack": ">=5.9.0"
86
+ },
87
+ "scripts": {
88
+ "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
89
+ "lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
90
+ "typecheck": "tsc"
91
+ }
92
+ }