next-suspend 1.0.0 → 1.0.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,5 +1,8 @@
1
1
  # next-suspend
2
2
 
3
+ The `suspend` utility runs your data resolver function through `React.cache` using your Next.js page `params` as the cache keys.
4
+ It returns an enhanced `<Suspense>` component, which awaits the `params` and renders the resolved value through a children prop.
5
+
3
6
  ```
4
7
  npm i next-suspend
5
8
  ```
@@ -11,21 +14,22 @@ import { suspend } from "next-suspend";
11
14
 
12
15
  // 1. Setup your data resolver
13
16
  export const SuspendedProduct = suspend(
14
- async ({ productId }: { productId: string}) => {
17
+ async ({ productId }: { productId: string }) => {
15
18
  const product = await db.select.productById({ productId });
16
19
  if (!product) {
17
- notFound();
20
+ notFound();
18
21
  }
19
22
  return product;
20
23
  },
21
24
  );
22
25
 
23
-
24
26
  // 2. Pass the params & render values suspensfully
25
- export default async function Page({ params }: PageProps<"/inventory/[productId]">) {
27
+ export default async function Page({
28
+ params,
29
+ }: PageProps<"/inventory/[productId]">) {
26
30
  return (
27
- <div className="mb-8 w-full">
28
- <Property icon={<TbGardenCart className="-mt-1 mr-1 size-6" />}>
31
+ <div>
32
+ <Property icon={<TbGardenCart />}>
29
33
  <PropertyTitle>Product</PropertyTitle>
30
34
  <PropertyValue>
31
35
  <SuspendedProduct params={params} fallback={<SkeletonLine />}>
@@ -33,7 +37,7 @@ export default async function Page({ params }: PageProps<"/inventory/[productId]
33
37
  </SuspendedProduct>
34
38
  </PropertyValue>
35
39
  </Property>
36
- <Property icon={<TbGardenCart className="-mt-1 mr-1 size-6" />}>
40
+ <Property icon={<TbGardenCart />}>
37
41
  <PropertyTitle>Description</PropertyTitle>
38
42
  <PropertyValue>
39
43
  <SuspendedProduct params={params} fallback={<SkeletonLine />}>
@@ -41,11 +45,12 @@ export default async function Page({ params }: PageProps<"/inventory/[productId]
41
45
  </SuspendedProduct>
42
46
  </PropertyValue>
43
47
  </Property>
44
- )
48
+ </div>
49
+ );
45
50
  }
46
51
  ```
47
52
 
48
- Your resolver will run once per params.
53
+ Your resolver will run once per params. The page can now the titles (Product & Description) into a static shell.
49
54
 
50
55
  ## Helper types
51
56
 
package/dist/index.d.mts CHANGED
@@ -1,7 +1,16 @@
1
- import { ReactNode, SuspenseProps } from "react";
1
+ import { ReactNode } from "react";
2
2
  //#region src/suspend.d.ts
3
- export declare function suspend<Params extends Record<string, string>, Result>(resolve: (params: Params) => Promise<Result>): ({ params, children, ...props }: Omit<SuspenseProps, "children"> & {
3
+ /**
4
+ * A rendering utility.
5
+ * @param resolve The function to cache by the page params promise.
6
+ * @returns A component to render the cached result suspense-fully.
7
+ */
8
+ export declare function suspend<Params extends Record<string, string>, Result>(resolve: (params: Params) => Promise<Result>): ({ params, children, ...props }: {
9
+ fallback?: ReactNode;
10
+ name?: string | undefined;
4
11
  params: Promise<Params>;
5
- children: (props: Result) => ReactNode;
12
+ children: (data: Result) => ReactNode;
6
13
  }) => Promise<import("react").JSX.Element>;
14
+ export type Prettify<T> = Identity<{ [K in keyof T]: T[K]; }>;
15
+ type Identity<T> = T;
7
16
  //#endregion
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "next-suspend",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Suspensfully access data by page params.",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/MiroslavPetrik/next-suspend.git"
11
+ },
8
12
  "keywords": [
9
13
  "react",
10
14
  "nextjs",