@obelism/improve-sdk-next 0.2.0 → 0.3.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.
Files changed (2) hide show
  1. package/README.md +79 -0
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1 +1,80 @@
1
1
  # Obelism Improve NextJS SDK
2
+
3
+ The NextJS SDK sits around the [Javascript Server SDK](https://improve.obelism.studio/docs/sdk/javascript#server) and gives React specific handlers.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i @obelism/improve-sdk-next
9
+ ```
10
+
11
+ ## Generator
12
+
13
+ The main export exposes a generator to create the middleware handler using the passed configuration.
14
+
15
+ ```ts
16
+ import { ImproveServerSDK } from '@obelism/improve-sdk/server'
17
+ import { generateImproveNextMiddleware } from '@obelism/improve-sdk-next'
18
+
19
+ const improveMiddlewareHandler = generateImproveNextMiddleware({
20
+ improveSdk: new ImproveServerSDK(IMPROVE_ARGS),
21
+ serverABtests: [],
22
+ })
23
+ ```
24
+
25
+ ### improveSdk
26
+
27
+ ```ts
28
+ generateImproveNextMiddleware({
29
+ improveSdk: ImproveServerSDK,
30
+ })
31
+ ```
32
+
33
+ The [ImproveServerSDK](https://improve.obelism.studio/docs/sdk/javascript#server) to be used. Required to be loaded with a configuration in place before handlig requests.
34
+
35
+ ### serverABtests
36
+
37
+ ```ts
38
+ generateImproveNextMiddleware({
39
+ serverABtests: ServerABTestConfig[]
40
+ })
41
+ ```
42
+
43
+ Not every AB test from the config needs to be used be used for the middleware handler. For each one that does need to be used it needs to be passed in this argument. Each entry in this list resembles one server side AB test.
44
+
45
+ ```ts
46
+ export type OptionConfig = {
47
+ value: string // Should match the slug from the option
48
+ slug: string // Slug to send people towards when this version is active
49
+ }
50
+
51
+ export type ServerABTestConfig = {
52
+ // Needs to match an AB test in Improve
53
+ slug: string
54
+
55
+ // Route to run the AB test on, usually this matches the slug
56
+ routeHandler: string
57
+
58
+ // For more complicated rewrites pass a function to format a NextUrl based on the matching option
59
+ formatSlug?: (url: NextURL, matchingOption: OptionConfig) => NextURL
60
+
61
+ // List of options for the AB test
62
+ options: OptionConfig[]
63
+ }
64
+ ```
65
+
66
+ ## MiddlewareHandler
67
+
68
+ ```ts
69
+ const improveMiddlewareHandler = generateImproveNextMiddleware(...)
70
+
71
+ export const middleware = async (request: NextRequest) => {
72
+ return improveMiddlewareHandler(request)
73
+ }
74
+ ```
75
+
76
+ The generated handler based on your configuration. Receives a `NextRequest` and rewrites based on one of three reasons:
77
+
78
+ - Search parameters matching a valid test with option configuration. Recommended for development or testing purposes, shouldn't be used for real users since it will impact the distribution.
79
+ - AB test cookie value which matches a valid test for name with option for value, the `improveMiddlewareHandler` will both read and write this cookie
80
+ - Visitor ID cookie, for long running servers the `ImproveServerSDK` will also be able to store the saved decision for a visitor in memory and use that to serve the same result. For serverless or edge this won't work for this purpose.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@obelism/improve-sdk-next",
3
3
  "description": "Obelism Improve NextJS SDK",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "keywords": [
6
6
  "ab-tests",
7
7
  "feature-flags",
@@ -58,7 +58,7 @@
58
58
  "vitest": "^2.0.5"
59
59
  },
60
60
  "dependencies": {
61
- "@obelism/improve-sdk": "^0.2.0",
62
- "@obelism/improve-sdk-react": "^0.2.0"
61
+ "@obelism/improve-sdk": "^0.3.0",
62
+ "@obelism/improve-sdk-react": "^0.3.0"
63
63
  }
64
64
  }