@preprio/prepr-nextjs 1.0.0-beta.3 → 1.0.0-beta.5
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 +42 -9
- package/package.json +1 -1
- package/dist/components.d.mts +0 -9
- package/dist/components.d.ts +0 -9
- package/dist/index.d.mts +0 -56
- package/dist/index.d.ts +0 -56
package/README.md
CHANGED
|
@@ -10,22 +10,22 @@ easier personalization & A/B testing implementation in your NextJS project.
|
|
|
10
10
|
To install the Prepr NextJS package, run the following command:
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
|
-
npm install @
|
|
13
|
+
npm install @preprio/prepr-nextjs
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
Next you should navigate to your
|
|
16
|
+
Next you should navigate to your `.env` file and add the following environment variable:
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
19
|
PREPR_ENV=
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
If you want to include the PreviewBar component in your project, set the `PREPR_ENV` environment variable to `preview`.
|
|
23
|
+
When you're launching your project to production, then set the `PREPR_ENV` environment variable to `production`. This way, the PreviewBar component doesn't get displayed on a live web app.
|
|
24
24
|
|
|
25
|
-
Next we will implement the PreprMiddleware function
|
|
25
|
+
Next, we will implement the PreprMiddleware function. Navigate to your `middleware.js` or the `middleware.ts`
|
|
26
26
|
file. If you don't have this file, you can create it in the root of your project.
|
|
27
27
|
|
|
28
|
-
Then add the following code to the `middleware.
|
|
28
|
+
Then add the following code to the `middleware.ts` file:
|
|
29
29
|
```javascript
|
|
30
30
|
import type { NextRequest } from 'next/server'
|
|
31
31
|
import { PreprMiddleware } from '@preprio/prepr-nextjs'
|
|
@@ -35,6 +35,15 @@ export function middleware(request: NextRequest) {
|
|
|
35
35
|
}
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
+
Or add the following code to the `middleware.js` file:
|
|
39
|
+
```javascript
|
|
40
|
+
import { PreprMiddleware } from '@preprio/prepr-nextjs'
|
|
41
|
+
|
|
42
|
+
export function middleware(request) {
|
|
43
|
+
return PreprMiddleware(request)
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
38
47
|
The PreprMiddleware accepts a request and optional response property and returns a `NextRequest` object.
|
|
39
48
|
This is done so you are able to chain your own middleware to it.
|
|
40
49
|
|
|
@@ -46,7 +55,8 @@ If the `PREPR_ENV` environment variable is set to `preview`, the PreprMiddleware
|
|
|
46
55
|
If these searchParams are set, the PreprMiddleware will set the `Prepr-Segments` and `Prepr-AB-Testing` headers with the values of the searchParams, and store its value in a cookie.
|
|
47
56
|
|
|
48
57
|
## Usage
|
|
49
|
-
To
|
|
58
|
+
To set up the headers with your API calls, you can call the `getPreprHeaders()` helper function. This will return an array of headers that you can spread in your fetch call.
|
|
59
|
+
See the example code below in the `page.tsx` file.
|
|
50
60
|
|
|
51
61
|
```javascript
|
|
52
62
|
import { getClient } from '@/lib/client'
|
|
@@ -68,13 +78,36 @@ const getData = async () => {
|
|
|
68
78
|
})
|
|
69
79
|
}
|
|
70
80
|
```
|
|
81
|
+
See the javascript example code below in the `page.js`file.
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
import { getClient } from '@/lib/client'
|
|
85
|
+
import { GetPageBySlug } from '@/queries/get-page-by-slug';
|
|
86
|
+
import { getPreprHeaders } from '@preprio/prepr-nextjs'
|
|
87
|
+
|
|
88
|
+
const getData = async () => {
|
|
89
|
+
// Fetching the data using Apollo Client
|
|
90
|
+
const { data } = await client.query({
|
|
91
|
+
query: GetPageBySlug,
|
|
92
|
+
variables: {
|
|
93
|
+
slug: '/',
|
|
94
|
+
},
|
|
95
|
+
context: {
|
|
96
|
+
// Call the getPreprHeaders function to get the appropriate headers
|
|
97
|
+
headers: getPreprHeaders(),
|
|
98
|
+
},
|
|
99
|
+
fetchPolicy: 'no-cache',
|
|
100
|
+
})
|
|
101
|
+
return data;
|
|
102
|
+
}
|
|
103
|
+
```
|
|
71
104
|
|
|
72
105
|
### Installing the PreviewBar component
|
|
73
106
|
|
|
74
107
|
For the PreviewBar to work we need to fetch all the segments from the Prepr API. To do this navigate to Prepr -> Settings -> Access tokens and create a new access token with the following scopes:
|
|
75
108
|
- `segments`
|
|
76
109
|
|
|
77
|
-
|
|
110
|
+
Then copy the access token and navigate to your `.env` file and add the following environment variable:
|
|
78
111
|
```bash
|
|
79
112
|
PREPR_SEGMENTS_ACCESS_TOKEN=<YOUR_ACCESS_TOKEN>
|
|
80
113
|
```
|
|
@@ -123,7 +156,7 @@ The `getPreprUUID()` function will return the value of the `__prepr_uid` cookie.
|
|
|
123
156
|
Returns the active segment from the `Prepr-Segments` header.
|
|
124
157
|
|
|
125
158
|
#### getActiveVariant()
|
|
126
|
-
Returns the active variant from the `Prepr-
|
|
159
|
+
Returns the active variant from the `Prepr-ABTesting` header.
|
|
127
160
|
|
|
128
161
|
#### getPreviewHeaders()
|
|
129
162
|
Helper function to only get the preview headers.
|
package/package.json
CHANGED
package/dist/components.d.mts
DELETED
package/dist/components.d.ts
DELETED
package/dist/index.d.mts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
|
|
3
|
-
declare function PreprMiddleware(request: NextRequest, response?: NextResponse): NextResponse<unknown>;
|
|
4
|
-
/**
|
|
5
|
-
* Returns the Prepr Customer ID from the headers
|
|
6
|
-
*/
|
|
7
|
-
declare function getPreprUUID(): string;
|
|
8
|
-
/**
|
|
9
|
-
* Retuns the active segment from the headers
|
|
10
|
-
*/
|
|
11
|
-
declare function getActiveSegment(): string;
|
|
12
|
-
/**
|
|
13
|
-
* Returns the active variant from the headers
|
|
14
|
-
*/
|
|
15
|
-
declare function getActiveVariant(): string;
|
|
16
|
-
/**
|
|
17
|
-
* Helper function to retrieve Prepr headers (will filter out customer ID if in preview mode)
|
|
18
|
-
*/
|
|
19
|
-
declare function getPreprHeaders(): {
|
|
20
|
-
[key: string]: string;
|
|
21
|
-
};
|
|
22
|
-
type PreprSegment = {
|
|
23
|
-
id: string;
|
|
24
|
-
created_on: string;
|
|
25
|
-
changed_on: string;
|
|
26
|
-
synced_on: string;
|
|
27
|
-
label: string;
|
|
28
|
-
reference_id: string;
|
|
29
|
-
body: string;
|
|
30
|
-
query: string;
|
|
31
|
-
count: number;
|
|
32
|
-
};
|
|
33
|
-
type PreprSegmentsResponse = {
|
|
34
|
-
total: number;
|
|
35
|
-
skip: number;
|
|
36
|
-
limit: number;
|
|
37
|
-
items: PreprSegment[];
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Fetches the segments from the Prepr API
|
|
41
|
-
* @param token Prepr access token with scope 'segments'
|
|
42
|
-
* @returns Object with total, skip, limit and items
|
|
43
|
-
*/
|
|
44
|
-
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegmentsResponse>;
|
|
45
|
-
/**
|
|
46
|
-
* Fetches all the necessary previewbar props
|
|
47
|
-
* @param token Prepr access token with scope 'segments'
|
|
48
|
-
* @returns Object with activeSegment, activeVariant and data
|
|
49
|
-
*/
|
|
50
|
-
declare function getPreviewBarProps(token: string): Promise<{
|
|
51
|
-
activeSegment: string | null;
|
|
52
|
-
activeVariant: string | null;
|
|
53
|
-
data: PreprSegmentsResponse;
|
|
54
|
-
}>;
|
|
55
|
-
|
|
56
|
-
export { PreprMiddleware, type PreprSegment, type PreprSegmentsResponse, getActiveSegment, getActiveVariant, getPreprEnvironmentSegments, getPreprHeaders, getPreprUUID, getPreviewBarProps };
|
package/dist/index.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
|
|
3
|
-
declare function PreprMiddleware(request: NextRequest, response?: NextResponse): NextResponse<unknown>;
|
|
4
|
-
/**
|
|
5
|
-
* Returns the Prepr Customer ID from the headers
|
|
6
|
-
*/
|
|
7
|
-
declare function getPreprUUID(): string;
|
|
8
|
-
/**
|
|
9
|
-
* Retuns the active segment from the headers
|
|
10
|
-
*/
|
|
11
|
-
declare function getActiveSegment(): string;
|
|
12
|
-
/**
|
|
13
|
-
* Returns the active variant from the headers
|
|
14
|
-
*/
|
|
15
|
-
declare function getActiveVariant(): string;
|
|
16
|
-
/**
|
|
17
|
-
* Helper function to retrieve Prepr headers (will filter out customer ID if in preview mode)
|
|
18
|
-
*/
|
|
19
|
-
declare function getPreprHeaders(): {
|
|
20
|
-
[key: string]: string;
|
|
21
|
-
};
|
|
22
|
-
type PreprSegment = {
|
|
23
|
-
id: string;
|
|
24
|
-
created_on: string;
|
|
25
|
-
changed_on: string;
|
|
26
|
-
synced_on: string;
|
|
27
|
-
label: string;
|
|
28
|
-
reference_id: string;
|
|
29
|
-
body: string;
|
|
30
|
-
query: string;
|
|
31
|
-
count: number;
|
|
32
|
-
};
|
|
33
|
-
type PreprSegmentsResponse = {
|
|
34
|
-
total: number;
|
|
35
|
-
skip: number;
|
|
36
|
-
limit: number;
|
|
37
|
-
items: PreprSegment[];
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Fetches the segments from the Prepr API
|
|
41
|
-
* @param token Prepr access token with scope 'segments'
|
|
42
|
-
* @returns Object with total, skip, limit and items
|
|
43
|
-
*/
|
|
44
|
-
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegmentsResponse>;
|
|
45
|
-
/**
|
|
46
|
-
* Fetches all the necessary previewbar props
|
|
47
|
-
* @param token Prepr access token with scope 'segments'
|
|
48
|
-
* @returns Object with activeSegment, activeVariant and data
|
|
49
|
-
*/
|
|
50
|
-
declare function getPreviewBarProps(token: string): Promise<{
|
|
51
|
-
activeSegment: string | null;
|
|
52
|
-
activeVariant: string | null;
|
|
53
|
-
data: PreprSegmentsResponse;
|
|
54
|
-
}>;
|
|
55
|
-
|
|
56
|
-
export { PreprMiddleware, type PreprSegment, type PreprSegmentsResponse, getActiveSegment, getActiveVariant, getPreprEnvironmentSegments, getPreprHeaders, getPreprUUID, getPreviewBarProps };
|