@spoosh/plugin-nextjs 0.1.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Spoosh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # @spoosh/plugin-nextjs
2
+
3
+ Next.js integration plugin for Spoosh - server-side cache revalidation after mutations.
4
+
5
+ **[Documentation](https://spoosh.dev/docs/plugins/nextjs)** · **Requirements:** TypeScript >= 5.0, Next.js supporting server actions · **Peer Dependencies:** `@spoosh/core`, `@spoosh/plugin-invalidation`
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @spoosh/plugin-nextjs
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Create server action
16
+
17
+ ```typescript
18
+ "use server";
19
+
20
+ import { revalidateTag, revalidatePath } from "next/cache";
21
+
22
+ export async function revalidateAction(tags: string[], paths: string[]) {
23
+ tags.forEach((tag) => revalidateTag(tag));
24
+ paths.forEach((path) => revalidatePath(path));
25
+ }
26
+ ```
27
+
28
+ ```typescript
29
+ import { nextjsPlugin } from "@spoosh/plugin-nextjs";
30
+ import { invalidationPlugin } from "@spoosh/plugin-invalidation";
31
+
32
+ const plugins = [
33
+ invalidationPlugin(),
34
+ nextjsPlugin({ serverRevalidator: revalidateAction }),
35
+ ];
36
+
37
+ // After a successful mutation, cache tags are automatically revalidated
38
+ const { trigger } = useWrite((api) => api.posts.$post);
39
+
40
+ await trigger({ body: { title: "New Post" } });
41
+ // Revalidates: ["posts"] tag on the server
42
+ ```
43
+
44
+ ### Revalidate Additional Paths
45
+
46
+ ```typescript
47
+ const { trigger } = useWrite((api) => api.posts.$post);
48
+
49
+ await trigger({
50
+ body: { title: "New Post" },
51
+ revalidatePaths: ["/", "/posts"],
52
+ });
53
+ ```
54
+
55
+ ### Skip Revalidation
56
+
57
+ ```typescript
58
+ // Skip for a specific mutation
59
+ await trigger({
60
+ body: { title: "Draft" },
61
+ serverRevalidate: false,
62
+ });
63
+ ```
64
+
65
+ ## When to Use `skipServerRevalidation`
66
+
67
+ ### Client-Heavy Apps (CSR/SPA)
68
+
69
+ For apps that primarily fetch data on the client side, set `skipServerRevalidation: true` by default and opt-in for specific mutations that affect server-rendered content:
70
+
71
+ ```typescript
72
+ const plugins = [
73
+ nextjsPlugin({
74
+ serverRevalidator: revalidate,
75
+ skipServerRevalidation: true, // Skip by default
76
+ }),
77
+ ];
78
+
79
+ // Most mutations don't need server revalidation
80
+ await trigger({ body: data });
81
+
82
+ // Opt-in when mutation affects server-rendered pages
83
+ await trigger({
84
+ body: data,
85
+ serverRevalidate: true, // Explicitly enable
86
+ });
87
+ ```
88
+
89
+ ### Server-Heavy Apps (SSR/RSC)
90
+
91
+ For apps that rely heavily on server-side rendering or React Server Components, keep the default behavior (revalidate on every mutation) and opt-out when needed:
92
+
93
+ ```typescript
94
+ const plugins = [
95
+ nextjsPlugin({
96
+ serverRevalidator: revalidate,
97
+ // skipServerRevalidation: false (default)
98
+ }),
99
+ ];
100
+
101
+ // Server cache is revalidated by default
102
+ await trigger({ body: data });
103
+
104
+ // Skip for mutations that don't affect server content
105
+ await trigger({
106
+ body: { theme: "dark" },
107
+ serverRevalidate: false, // Skip for client-only state
108
+ });
109
+ ```
110
+
111
+ ## Options
112
+
113
+ ### Plugin Config
114
+
115
+ | Option | Type | Default | Description |
116
+ | ------------------------ | ---------------------------------- | ------- | --------------------------------- |
117
+ | `serverRevalidator` | `(tags, paths) => void \| Promise` | - | Server action to revalidate cache |
118
+ | `skipServerRevalidation` | `boolean` | `false` | Skip revalidation by default |
119
+
120
+ ### Per-Request Options
121
+
122
+ | Option | Type | Description |
123
+ | ------------------ | ---------- | ----------------------------------------------- |
124
+ | `revalidatePaths` | `string[]` | Additional paths to revalidate |
125
+ | `serverRevalidate` | `boolean` | Override whether to trigger server revalidation |
@@ -0,0 +1,56 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ type ServerRevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
4
+ interface NextjsPluginConfig {
5
+ /** Server action to revalidate tags and paths */
6
+ serverRevalidator?: ServerRevalidateHandler;
7
+ /** Skip server revalidation by default. Default: false */
8
+ skipServerRevalidation?: boolean;
9
+ }
10
+ type NextjsReadOptions = object;
11
+ interface NextjsWriteOptions {
12
+ /** Additional paths to revalidate after mutation */
13
+ revalidatePaths?: string[];
14
+ /** Whether to trigger server revalidation. Overrides plugin default. */
15
+ serverRevalidate?: boolean;
16
+ }
17
+ type NextjsInfiniteReadOptions = object;
18
+ type NextjsReadResult = object;
19
+ type NextjsWriteResult = object;
20
+
21
+ /**
22
+ * Next.js integration plugin for server-side revalidation.
23
+ *
24
+ * Automatically revalidates Next.js cache tags and paths after successful mutations.
25
+ *
26
+ * @param config - Plugin configuration
27
+ *
28
+ * @see {@link https://spoosh.dev/docs/plugins/nextjs | Next.js Plugin Documentation}
29
+ *
30
+ * @returns Next.js plugin instance
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { nextjsPlugin } from "@spoosh/plugin-nextjs";
35
+ *
36
+ * const plugins = [
37
+ * nextjsPlugin({
38
+ * serverRevalidator: async (tags, paths) => {
39
+ * "use server";
40
+ * const { revalidateTag, revalidatePath } = await import("next/cache");
41
+ * tags.forEach((tag) => revalidateTag(tag));
42
+ * paths.forEach((path) => revalidatePath(path));
43
+ * },
44
+ * }),
45
+ * ];
46
+ * ```
47
+ */
48
+ declare function nextjsPlugin(config?: NextjsPluginConfig): SpooshPlugin<{
49
+ readOptions: NextjsReadOptions;
50
+ writeOptions: NextjsWriteOptions;
51
+ infiniteReadOptions: NextjsInfiniteReadOptions;
52
+ readResult: NextjsReadResult;
53
+ writeResult: NextjsWriteResult;
54
+ }>;
55
+
56
+ export { type NextjsInfiniteReadOptions, type NextjsPluginConfig, type NextjsReadOptions, type NextjsReadResult, type NextjsWriteOptions, type NextjsWriteResult, type ServerRevalidateHandler, nextjsPlugin };
@@ -0,0 +1,56 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ type ServerRevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
4
+ interface NextjsPluginConfig {
5
+ /** Server action to revalidate tags and paths */
6
+ serverRevalidator?: ServerRevalidateHandler;
7
+ /** Skip server revalidation by default. Default: false */
8
+ skipServerRevalidation?: boolean;
9
+ }
10
+ type NextjsReadOptions = object;
11
+ interface NextjsWriteOptions {
12
+ /** Additional paths to revalidate after mutation */
13
+ revalidatePaths?: string[];
14
+ /** Whether to trigger server revalidation. Overrides plugin default. */
15
+ serverRevalidate?: boolean;
16
+ }
17
+ type NextjsInfiniteReadOptions = object;
18
+ type NextjsReadResult = object;
19
+ type NextjsWriteResult = object;
20
+
21
+ /**
22
+ * Next.js integration plugin for server-side revalidation.
23
+ *
24
+ * Automatically revalidates Next.js cache tags and paths after successful mutations.
25
+ *
26
+ * @param config - Plugin configuration
27
+ *
28
+ * @see {@link https://spoosh.dev/docs/plugins/nextjs | Next.js Plugin Documentation}
29
+ *
30
+ * @returns Next.js plugin instance
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { nextjsPlugin } from "@spoosh/plugin-nextjs";
35
+ *
36
+ * const plugins = [
37
+ * nextjsPlugin({
38
+ * serverRevalidator: async (tags, paths) => {
39
+ * "use server";
40
+ * const { revalidateTag, revalidatePath } = await import("next/cache");
41
+ * tags.forEach((tag) => revalidateTag(tag));
42
+ * paths.forEach((path) => revalidatePath(path));
43
+ * },
44
+ * }),
45
+ * ];
46
+ * ```
47
+ */
48
+ declare function nextjsPlugin(config?: NextjsPluginConfig): SpooshPlugin<{
49
+ readOptions: NextjsReadOptions;
50
+ writeOptions: NextjsWriteOptions;
51
+ infiniteReadOptions: NextjsInfiniteReadOptions;
52
+ readResult: NextjsReadResult;
53
+ writeResult: NextjsWriteResult;
54
+ }>;
55
+
56
+ export { type NextjsInfiniteReadOptions, type NextjsPluginConfig, type NextjsReadOptions, type NextjsReadResult, type NextjsWriteOptions, type NextjsWriteResult, type ServerRevalidateHandler, nextjsPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ nextjsPlugin: () => nextjsPlugin
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/plugin.ts
28
+ function nextjsPlugin(config = {}) {
29
+ const { serverRevalidator, skipServerRevalidation = false } = config;
30
+ return {
31
+ name: "spoosh:nextjs",
32
+ operations: ["write"],
33
+ middleware: async (context, next) => {
34
+ const response = await next();
35
+ if (response.error || !serverRevalidator) {
36
+ return response;
37
+ }
38
+ const pluginOptions = context.pluginOptions;
39
+ const shouldRevalidate = pluginOptions?.serverRevalidate ?? !skipServerRevalidation;
40
+ if (!shouldRevalidate) {
41
+ return response;
42
+ }
43
+ const revalidatePaths = pluginOptions?.revalidatePaths ?? [];
44
+ if (context.tags.length > 0 || revalidatePaths.length > 0) {
45
+ await serverRevalidator(context.tags, revalidatePaths);
46
+ }
47
+ return response;
48
+ }
49
+ };
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ nextjsPlugin
54
+ });
55
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/plugin.ts"],"sourcesContent":["export * from \"./types\";\nexport * from \"./plugin\";\n","import type { SpooshPlugin } from \"@spoosh/core\";\nimport type {\n NextjsPluginConfig,\n NextjsReadOptions,\n NextjsWriteOptions,\n NextjsInfiniteReadOptions,\n NextjsReadResult,\n NextjsWriteResult,\n} from \"./types\";\n\n/**\n * Next.js integration plugin for server-side revalidation.\n *\n * Automatically revalidates Next.js cache tags and paths after successful mutations.\n *\n * @param config - Plugin configuration\n *\n * @see {@link https://spoosh.dev/docs/plugins/nextjs | Next.js Plugin Documentation}\n *\n * @returns Next.js plugin instance\n *\n * @example\n * ```ts\n * import { nextjsPlugin } from \"@spoosh/plugin-nextjs\";\n *\n * const plugins = [\n * nextjsPlugin({\n * serverRevalidator: async (tags, paths) => {\n * \"use server\";\n * const { revalidateTag, revalidatePath } = await import(\"next/cache\");\n * tags.forEach((tag) => revalidateTag(tag));\n * paths.forEach((path) => revalidatePath(path));\n * },\n * }),\n * ];\n * ```\n */\nexport function nextjsPlugin(config: NextjsPluginConfig = {}): SpooshPlugin<{\n readOptions: NextjsReadOptions;\n writeOptions: NextjsWriteOptions;\n infiniteReadOptions: NextjsInfiniteReadOptions;\n readResult: NextjsReadResult;\n writeResult: NextjsWriteResult;\n}> {\n const { serverRevalidator, skipServerRevalidation = false } = config;\n\n return {\n name: \"spoosh:nextjs\",\n operations: [\"write\"],\n\n middleware: async (context, next) => {\n const response = await next();\n\n if (response.error || !serverRevalidator) {\n return response;\n }\n\n const pluginOptions = context.pluginOptions as\n | NextjsWriteOptions\n | undefined;\n\n const shouldRevalidate =\n pluginOptions?.serverRevalidate ?? !skipServerRevalidation;\n\n if (!shouldRevalidate) {\n return response;\n }\n\n const revalidatePaths = pluginOptions?.revalidatePaths ?? [];\n\n if (context.tags.length > 0 || revalidatePaths.length > 0) {\n await serverRevalidator(context.tags, revalidatePaths);\n }\n\n return response;\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqCO,SAAS,aAAa,SAA6B,CAAC,GAMxD;AACD,QAAM,EAAE,mBAAmB,yBAAyB,MAAM,IAAI;AAE9D,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY,CAAC,OAAO;AAAA,IAEpB,YAAY,OAAO,SAAS,SAAS;AACnC,YAAM,WAAW,MAAM,KAAK;AAE5B,UAAI,SAAS,SAAS,CAAC,mBAAmB;AACxC,eAAO;AAAA,MACT;AAEA,YAAM,gBAAgB,QAAQ;AAI9B,YAAM,mBACJ,eAAe,oBAAoB,CAAC;AAEtC,UAAI,CAAC,kBAAkB;AACrB,eAAO;AAAA,MACT;AAEA,YAAM,kBAAkB,eAAe,mBAAmB,CAAC;AAE3D,UAAI,QAAQ,KAAK,SAAS,KAAK,gBAAgB,SAAS,GAAG;AACzD,cAAM,kBAAkB,QAAQ,MAAM,eAAe;AAAA,MACvD;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,28 @@
1
+ // src/plugin.ts
2
+ function nextjsPlugin(config = {}) {
3
+ const { serverRevalidator, skipServerRevalidation = false } = config;
4
+ return {
5
+ name: "spoosh:nextjs",
6
+ operations: ["write"],
7
+ middleware: async (context, next) => {
8
+ const response = await next();
9
+ if (response.error || !serverRevalidator) {
10
+ return response;
11
+ }
12
+ const pluginOptions = context.pluginOptions;
13
+ const shouldRevalidate = pluginOptions?.serverRevalidate ?? !skipServerRevalidation;
14
+ if (!shouldRevalidate) {
15
+ return response;
16
+ }
17
+ const revalidatePaths = pluginOptions?.revalidatePaths ?? [];
18
+ if (context.tags.length > 0 || revalidatePaths.length > 0) {
19
+ await serverRevalidator(context.tags, revalidatePaths);
20
+ }
21
+ return response;
22
+ }
23
+ };
24
+ }
25
+ export {
26
+ nextjsPlugin
27
+ };
28
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/plugin.ts"],"sourcesContent":["import type { SpooshPlugin } from \"@spoosh/core\";\nimport type {\n NextjsPluginConfig,\n NextjsReadOptions,\n NextjsWriteOptions,\n NextjsInfiniteReadOptions,\n NextjsReadResult,\n NextjsWriteResult,\n} from \"./types\";\n\n/**\n * Next.js integration plugin for server-side revalidation.\n *\n * Automatically revalidates Next.js cache tags and paths after successful mutations.\n *\n * @param config - Plugin configuration\n *\n * @see {@link https://spoosh.dev/docs/plugins/nextjs | Next.js Plugin Documentation}\n *\n * @returns Next.js plugin instance\n *\n * @example\n * ```ts\n * import { nextjsPlugin } from \"@spoosh/plugin-nextjs\";\n *\n * const plugins = [\n * nextjsPlugin({\n * serverRevalidator: async (tags, paths) => {\n * \"use server\";\n * const { revalidateTag, revalidatePath } = await import(\"next/cache\");\n * tags.forEach((tag) => revalidateTag(tag));\n * paths.forEach((path) => revalidatePath(path));\n * },\n * }),\n * ];\n * ```\n */\nexport function nextjsPlugin(config: NextjsPluginConfig = {}): SpooshPlugin<{\n readOptions: NextjsReadOptions;\n writeOptions: NextjsWriteOptions;\n infiniteReadOptions: NextjsInfiniteReadOptions;\n readResult: NextjsReadResult;\n writeResult: NextjsWriteResult;\n}> {\n const { serverRevalidator, skipServerRevalidation = false } = config;\n\n return {\n name: \"spoosh:nextjs\",\n operations: [\"write\"],\n\n middleware: async (context, next) => {\n const response = await next();\n\n if (response.error || !serverRevalidator) {\n return response;\n }\n\n const pluginOptions = context.pluginOptions as\n | NextjsWriteOptions\n | undefined;\n\n const shouldRevalidate =\n pluginOptions?.serverRevalidate ?? !skipServerRevalidation;\n\n if (!shouldRevalidate) {\n return response;\n }\n\n const revalidatePaths = pluginOptions?.revalidatePaths ?? [];\n\n if (context.tags.length > 0 || revalidatePaths.length > 0) {\n await serverRevalidator(context.tags, revalidatePaths);\n }\n\n return response;\n },\n };\n}\n"],"mappings":";AAqCO,SAAS,aAAa,SAA6B,CAAC,GAMxD;AACD,QAAM,EAAE,mBAAmB,yBAAyB,MAAM,IAAI;AAE9D,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY,CAAC,OAAO;AAAA,IAEpB,YAAY,OAAO,SAAS,SAAS;AACnC,YAAM,WAAW,MAAM,KAAK;AAE5B,UAAI,SAAS,SAAS,CAAC,mBAAmB;AACxC,eAAO;AAAA,MACT;AAEA,YAAM,gBAAgB,QAAQ;AAI9B,YAAM,mBACJ,eAAe,oBAAoB,CAAC;AAEtC,UAAI,CAAC,kBAAkB;AACrB,eAAO;AAAA,MACT;AAEA,YAAM,kBAAkB,eAAe,mBAAmB,CAAC;AAE3D,UAAI,QAAQ,KAAK,SAAS,KAAK,gBAAgB,SAAS,GAAG;AACzD,cAAM,kBAAkB,QAAQ,MAAM,eAAe;AAAA,MACvD;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@spoosh/plugin-nextjs",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Next.js integration plugin for Spoosh - server revalidation after mutations",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nxnom/spoosh.git",
9
+ "directory": "packages/plugin-nextjs"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nxnom/spoosh/issues"
13
+ },
14
+ "homepage": "https://spoosh.dev/docs/plugins/nextjs",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "keywords": [
19
+ "spoosh",
20
+ "plugin",
21
+ "nextjs",
22
+ "next.js",
23
+ "revalidation",
24
+ "api-client"
25
+ ],
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.mjs",
33
+ "require": "./dist/index.js"
34
+ }
35
+ },
36
+ "peerDependencies": {
37
+ "@spoosh/core": ">=0.1.0",
38
+ "next": ">=13.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@spoosh/core": "0.1.0-beta.0"
42
+ },
43
+ "scripts": {
44
+ "dev": "tsup --watch",
45
+ "build": "tsup",
46
+ "typecheck": "tsc --noEmit",
47
+ "lint": "eslint src --max-warnings 0",
48
+ "format": "prettier --write 'src/**/*.ts'"
49
+ }
50
+ }