@preprio/toolkit 0.1.0-beta.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 +903 -0
- package/dist/astro/index.cjs +319 -0
- package/dist/astro/index.d.cts +76 -0
- package/dist/astro/index.d.ts +76 -0
- package/dist/astro/index.js +287 -0
- package/dist/chunk-H7HVQSSF.js +2081 -0
- package/dist/index.cjs +2393 -0
- package/dist/index.d.cts +179 -0
- package/dist/index.d.ts +179 -0
- package/dist/index.js +306 -0
- package/dist/nextjs/components.cjs +2087 -0
- package/dist/nextjs/components.d.cts +17 -0
- package/dist/nextjs/components.d.ts +17 -0
- package/dist/nextjs/components.js +34 -0
- package/dist/nextjs/image-loader.cjs +122 -0
- package/dist/nextjs/image-loader.d.cts +10 -0
- package/dist/nextjs/image-loader.d.ts +10 -0
- package/dist/nextjs/image-loader.js +101 -0
- package/dist/nextjs/index.cjs +386 -0
- package/dist/nextjs/index.d.cts +90 -0
- package/dist/nextjs/index.d.ts +90 -0
- package/dist/nextjs/index.js +352 -0
- package/dist/pixel-DVSJQC0K.d.cts +94 -0
- package/dist/pixel-DVSJQC0K.d.ts +94 -0
- package/dist/sveltekit/index.cjs +371 -0
- package/dist/sveltekit/index.d.cts +117 -0
- package/dist/sveltekit/index.d.ts +117 -0
- package/dist/sveltekit/index.js +334 -0
- package/package.json +93 -0
- package/src/astro/PreprToolbar.astro +53 -0
- package/src/astro/PreprTrackingPixel.astro +48 -0
- package/src/sveltekit/PreprToolbar.svelte +21 -0
- package/src/sveltekit/PreprTrackingPixel.svelte +22 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
// src/version.ts
|
|
2
|
+
var VERSION = "0.1.0-beta.1";
|
|
3
|
+
|
|
4
|
+
// src/core/constants.ts
|
|
5
|
+
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
6
|
+
var COOKIE_SEGMENT = "Prepr-Segments";
|
|
7
|
+
var COOKIE_VARIANT = "Prepr-ABtesting";
|
|
8
|
+
var COOKIE_UID = "__prepr_uid";
|
|
9
|
+
var COOKIE_HUBSPOT = "hubspotutk";
|
|
10
|
+
var PARAM_SEGMENT = "prepr_preview_segment";
|
|
11
|
+
var PARAM_VARIANT = "prepr_preview_ab";
|
|
12
|
+
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
13
|
+
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
14
|
+
|
|
15
|
+
// src/core/middleware.ts
|
|
16
|
+
var UTM_PARAMS = [
|
|
17
|
+
"utm_source",
|
|
18
|
+
"utm_medium",
|
|
19
|
+
"utm_term",
|
|
20
|
+
"utm_content",
|
|
21
|
+
"utm_campaign"
|
|
22
|
+
];
|
|
23
|
+
function parseCookies(request) {
|
|
24
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
25
|
+
const header = request.headers.get("cookie");
|
|
26
|
+
if (!header) return cookies;
|
|
27
|
+
for (const part of header.split(";")) {
|
|
28
|
+
const eq = part.indexOf("=");
|
|
29
|
+
if (eq === -1) continue;
|
|
30
|
+
const name = part.slice(0, eq).trim();
|
|
31
|
+
const value = part.slice(eq + 1).trim();
|
|
32
|
+
if (!name) continue;
|
|
33
|
+
cookies.set(name, decodeURIComponent(value));
|
|
34
|
+
}
|
|
35
|
+
return cookies;
|
|
36
|
+
}
|
|
37
|
+
function processPreprRequest(request, opts) {
|
|
38
|
+
const requestHeaders = new Headers(request.headers);
|
|
39
|
+
const responseCookies = [];
|
|
40
|
+
const requestCookies = parseCookies(request);
|
|
41
|
+
const searchParams = new URL(request.url).searchParams;
|
|
42
|
+
for (const key of UTM_PARAMS) {
|
|
43
|
+
const value = searchParams.get(key);
|
|
44
|
+
if (value !== null) {
|
|
45
|
+
requestHeaders.set(`Prepr-Context-${key}`, value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const referer = request.headers.get("referer");
|
|
49
|
+
if (referer) {
|
|
50
|
+
requestHeaders.set("Prepr-Context-initial_referral", referer);
|
|
51
|
+
}
|
|
52
|
+
const userAgent = request.headers.get("user-agent");
|
|
53
|
+
if (userAgent) {
|
|
54
|
+
requestHeaders.set("Prepr-User-Agent", userAgent);
|
|
55
|
+
}
|
|
56
|
+
requestHeaders.set("Prepr-Package", `@preprio/toolkit@${opts?.version ?? VERSION}`);
|
|
57
|
+
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
58
|
+
if (ip) {
|
|
59
|
+
requestHeaders.set("Prepr-Visitor-IP", ip);
|
|
60
|
+
}
|
|
61
|
+
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
62
|
+
if (hutkCookie) {
|
|
63
|
+
requestHeaders.set("Prepr-Hubspot-Id", hutkCookie);
|
|
64
|
+
}
|
|
65
|
+
let uid = requestCookies.get(COOKIE_UID);
|
|
66
|
+
if (!uid) {
|
|
67
|
+
uid = crypto.randomUUID();
|
|
68
|
+
responseCookies.push({
|
|
69
|
+
name: COOKIE_UID,
|
|
70
|
+
value: uid,
|
|
71
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
72
|
+
path: "/"
|
|
73
|
+
});
|
|
74
|
+
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
75
|
+
}
|
|
76
|
+
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
77
|
+
if (!opts?.preview) {
|
|
78
|
+
return { requestHeaders, responseCookies };
|
|
79
|
+
}
|
|
80
|
+
const hasPreviewParams = searchParams.has(PARAM_SEGMENT) || searchParams.has(PARAM_VARIANT);
|
|
81
|
+
if (!hasPreviewParams) {
|
|
82
|
+
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
83
|
+
const previewModeEnabled = previewModeCookie !== "false";
|
|
84
|
+
if (!previewModeEnabled) {
|
|
85
|
+
return { requestHeaders, responseCookies };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
89
|
+
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
90
|
+
if (!isLivePreview) {
|
|
91
|
+
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
92
|
+
if (segmentCookie) {
|
|
93
|
+
requestHeaders.set("Prepr-Segments", segmentCookie);
|
|
94
|
+
}
|
|
95
|
+
const abCookie = requestCookies.get(COOKIE_VARIANT);
|
|
96
|
+
if (abCookie) {
|
|
97
|
+
requestHeaders.set("Prepr-ABtesting", abCookie);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const previewAb = searchParams.get(PARAM_VARIANT);
|
|
101
|
+
if (previewAb !== null) {
|
|
102
|
+
requestHeaders.set("Prepr-ABtesting", previewAb);
|
|
103
|
+
if (!isLivePreview) {
|
|
104
|
+
responseCookies.push({
|
|
105
|
+
name: COOKIE_VARIANT,
|
|
106
|
+
value: previewAb,
|
|
107
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
108
|
+
path: "/"
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const previewSegment = searchParams.get(PARAM_SEGMENT);
|
|
113
|
+
if (previewSegment !== null) {
|
|
114
|
+
requestHeaders.set("Prepr-Segments", previewSegment);
|
|
115
|
+
if (!isLivePreview) {
|
|
116
|
+
responseCookies.push({
|
|
117
|
+
name: COOKIE_SEGMENT,
|
|
118
|
+
value: previewSegment,
|
|
119
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
120
|
+
path: "/"
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { requestHeaders, responseCookies };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/sveltekit/hooks.ts
|
|
128
|
+
var PREPR_LOCALS_KEY = "prepr";
|
|
129
|
+
function preprHandle(options) {
|
|
130
|
+
return async ({ event, resolve }) => {
|
|
131
|
+
const { requestHeaders, responseCookies } = processPreprRequest(event.request, options);
|
|
132
|
+
requestHeaders.forEach((value, key) => {
|
|
133
|
+
event.request.headers.set(key, value);
|
|
134
|
+
});
|
|
135
|
+
if (event.locals) {
|
|
136
|
+
event.locals[PREPR_LOCALS_KEY] = requestHeaders;
|
|
137
|
+
}
|
|
138
|
+
const response = await resolve(event);
|
|
139
|
+
for (const cookie of responseCookies) {
|
|
140
|
+
const parts = [
|
|
141
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
142
|
+
`Max-Age=${cookie.maxAge}`,
|
|
143
|
+
`Path=${cookie.path}`
|
|
144
|
+
];
|
|
145
|
+
response.headers.append("Set-Cookie", parts.join("; "));
|
|
146
|
+
}
|
|
147
|
+
return response;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function getPreprHeadersFromLocals(locals) {
|
|
151
|
+
const headers = locals[PREPR_LOCALS_KEY];
|
|
152
|
+
return headers instanceof Headers ? headers : new Headers();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/core/server.ts
|
|
156
|
+
var PreprError = class extends Error {
|
|
157
|
+
constructor(message, code, context, originalError) {
|
|
158
|
+
super(message);
|
|
159
|
+
this.code = code;
|
|
160
|
+
this.context = context;
|
|
161
|
+
this.originalError = originalError;
|
|
162
|
+
this.name = "PreprError";
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
166
|
+
return headers.get("prepr-customer-id");
|
|
167
|
+
}
|
|
168
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
169
|
+
return headers.get("Prepr-Segments");
|
|
170
|
+
}
|
|
171
|
+
function getActiveVariantFromHeaders(headers) {
|
|
172
|
+
return headers.get("Prepr-ABtesting");
|
|
173
|
+
}
|
|
174
|
+
var PREPR_HEADER_KEYS = [
|
|
175
|
+
"prepr-customer-id",
|
|
176
|
+
"Prepr-Segments",
|
|
177
|
+
"Prepr-ABtesting",
|
|
178
|
+
"Prepr-Preview-Bar",
|
|
179
|
+
"Prepr-Context-utm_source",
|
|
180
|
+
"Prepr-Context-utm_medium",
|
|
181
|
+
"Prepr-Context-utm_term",
|
|
182
|
+
"Prepr-Context-utm_content",
|
|
183
|
+
"Prepr-Context-utm_campaign",
|
|
184
|
+
"Prepr-Context-initial_referral",
|
|
185
|
+
"Prepr-Visitor-IP",
|
|
186
|
+
"Prepr-Hubspot-Id",
|
|
187
|
+
"Prepr-Customer-Id-Created",
|
|
188
|
+
"Prepr-User-Agent"
|
|
189
|
+
];
|
|
190
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
191
|
+
const preprHeaders = {};
|
|
192
|
+
const copy = (key) => {
|
|
193
|
+
const value = headers.get(key);
|
|
194
|
+
if (value !== null) {
|
|
195
|
+
preprHeaders[key] = value;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
199
|
+
copy(key);
|
|
200
|
+
}
|
|
201
|
+
return preprHeaders;
|
|
202
|
+
}
|
|
203
|
+
function validatePreprToken(token) {
|
|
204
|
+
if (!token) {
|
|
205
|
+
throw new PreprError("Token is required", "MISSING_TOKEN", "validatePreprToken");
|
|
206
|
+
}
|
|
207
|
+
if (!token.startsWith("https://")) {
|
|
208
|
+
throw new PreprError(
|
|
209
|
+
"Token must be a valid HTTPS URL",
|
|
210
|
+
"INVALID_TOKEN",
|
|
211
|
+
"validatePreprToken"
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function extractAccessToken(graphqlUrl) {
|
|
216
|
+
let url;
|
|
217
|
+
try {
|
|
218
|
+
url = new URL(graphqlUrl);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
throw new PreprError(
|
|
221
|
+
"Token must be a valid URL",
|
|
222
|
+
"INVALID_TOKEN",
|
|
223
|
+
"extractAccessToken",
|
|
224
|
+
error instanceof Error ? error : new Error(String(error))
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
228
|
+
throw new PreprError(
|
|
229
|
+
"Token must be a graphql.prepr.io URL",
|
|
230
|
+
"INVALID_TOKEN",
|
|
231
|
+
"extractAccessToken"
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
const pathParts = url.pathname.split("/");
|
|
235
|
+
const token = pathParts[pathParts.length - 1];
|
|
236
|
+
if (!token) {
|
|
237
|
+
throw new PreprError(
|
|
238
|
+
"Token URL is missing an access token segment",
|
|
239
|
+
"INVALID_TOKEN",
|
|
240
|
+
"extractAccessToken"
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
return token;
|
|
244
|
+
}
|
|
245
|
+
async function getPreprEnvironmentSegments(token) {
|
|
246
|
+
validatePreprToken(token);
|
|
247
|
+
try {
|
|
248
|
+
const response = await fetch(token, {
|
|
249
|
+
headers: {
|
|
250
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
251
|
+
"Content-Type": "application/json"
|
|
252
|
+
},
|
|
253
|
+
method: "POST",
|
|
254
|
+
body: JSON.stringify({
|
|
255
|
+
query: `{
|
|
256
|
+
_Segments {
|
|
257
|
+
_id
|
|
258
|
+
name
|
|
259
|
+
}
|
|
260
|
+
}`
|
|
261
|
+
})
|
|
262
|
+
});
|
|
263
|
+
if (!response.ok) {
|
|
264
|
+
throw new PreprError(
|
|
265
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
266
|
+
"HTTP_ERROR",
|
|
267
|
+
"getPreprEnvironmentSegments"
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
const json = await response.json();
|
|
271
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
272
|
+
throw new PreprError(
|
|
273
|
+
"Invalid response format from Prepr API",
|
|
274
|
+
"INVALID_RESPONSE",
|
|
275
|
+
"getPreprEnvironmentSegments"
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
return json.data._Segments;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (error instanceof PreprError) {
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
throw new PreprError(
|
|
284
|
+
"Failed to fetch segments from Prepr API",
|
|
285
|
+
"FETCH_ERROR",
|
|
286
|
+
"getPreprEnvironmentSegments",
|
|
287
|
+
error instanceof Error ? error : new Error(String(error))
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
async function getToolbarPropsFromHeaders(headers, token) {
|
|
292
|
+
let segments = [];
|
|
293
|
+
try {
|
|
294
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
295
|
+
} catch {
|
|
296
|
+
segments = [];
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
activeSegment: getActiveSegmentFromHeaders(headers),
|
|
300
|
+
activeVariant: getActiveVariantFromHeaders(headers),
|
|
301
|
+
segments,
|
|
302
|
+
data: segments
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/sveltekit/server.ts
|
|
307
|
+
function getPreprUUID(headers) {
|
|
308
|
+
return getPreprUUIDFromHeaders(headers);
|
|
309
|
+
}
|
|
310
|
+
function getActiveSegment(headers) {
|
|
311
|
+
return getActiveSegmentFromHeaders(headers);
|
|
312
|
+
}
|
|
313
|
+
function getActiveVariant(headers) {
|
|
314
|
+
return getActiveVariantFromHeaders(headers);
|
|
315
|
+
}
|
|
316
|
+
function getPreprHeaders(headers) {
|
|
317
|
+
return getPreprHeadersFromHeaders(headers);
|
|
318
|
+
}
|
|
319
|
+
async function getToolbarProps(headers, token) {
|
|
320
|
+
return getToolbarPropsFromHeaders(headers, token);
|
|
321
|
+
}
|
|
322
|
+
export {
|
|
323
|
+
PREPR_LOCALS_KEY,
|
|
324
|
+
PreprError,
|
|
325
|
+
extractAccessToken,
|
|
326
|
+
getActiveSegment,
|
|
327
|
+
getActiveVariant,
|
|
328
|
+
getPreprHeaders,
|
|
329
|
+
getPreprHeadersFromLocals,
|
|
330
|
+
getPreprUUID,
|
|
331
|
+
getToolbarProps,
|
|
332
|
+
preprHandle,
|
|
333
|
+
validatePreprToken
|
|
334
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@preprio/toolkit",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "Prepr personalization, A/B testing and preview toolkit for Next.js, Astro, SvelteKit and Express",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/preprio/prepr-toolkit.git",
|
|
9
|
+
"directory": "packages/toolkit"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/preprio/prepr-toolkit#readme",
|
|
12
|
+
"bugs": "https://github.com/preprio/prepr-toolkit/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.cjs"
|
|
20
|
+
},
|
|
21
|
+
"./nextjs": {
|
|
22
|
+
"types": "./dist/nextjs/index.d.ts",
|
|
23
|
+
"import": "./dist/nextjs/index.js",
|
|
24
|
+
"require": "./dist/nextjs/index.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./nextjs/image-loader": {
|
|
27
|
+
"types": "./dist/nextjs/image-loader.d.ts",
|
|
28
|
+
"import": "./dist/nextjs/image-loader.js",
|
|
29
|
+
"require": "./dist/nextjs/image-loader.cjs"
|
|
30
|
+
},
|
|
31
|
+
"./astro": {
|
|
32
|
+
"types": "./dist/astro/index.d.ts",
|
|
33
|
+
"import": "./dist/astro/index.js",
|
|
34
|
+
"require": "./dist/astro/index.cjs"
|
|
35
|
+
},
|
|
36
|
+
"./astro/components/*": "./src/astro/*.astro",
|
|
37
|
+
"./sveltekit": {
|
|
38
|
+
"types": "./dist/sveltekit/index.d.ts",
|
|
39
|
+
"import": "./dist/sveltekit/index.js",
|
|
40
|
+
"require": "./dist/sveltekit/index.cjs"
|
|
41
|
+
},
|
|
42
|
+
"./sveltekit/components/*": "./src/sveltekit/*.svelte"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"src/astro/*.astro",
|
|
47
|
+
"src/sveltekit/*.svelte"
|
|
48
|
+
],
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@vercel/stega": "^1.1.0",
|
|
51
|
+
"preact": "^10.29.7"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"next": ">=13",
|
|
55
|
+
"react": ">=17",
|
|
56
|
+
"react-dom": ">=17"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"next": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"react": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"react-dom": {
|
|
66
|
+
"optional": true
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@types/node": "^22.20.1",
|
|
71
|
+
"@types/react": "^19.2.17",
|
|
72
|
+
"@types/react-dom": "^19.2.3",
|
|
73
|
+
"browserslist": "^4.28.7",
|
|
74
|
+
"happy-dom": "^20.11.1",
|
|
75
|
+
"lightningcss": "^1.33.0",
|
|
76
|
+
"next": "^16.2.12",
|
|
77
|
+
"react": "^19.2.8",
|
|
78
|
+
"react-dom": "^19.2.8",
|
|
79
|
+
"tsup": "^8.5.1",
|
|
80
|
+
"typescript": "^5.9.3",
|
|
81
|
+
"vitest": "^4.1.10"
|
|
82
|
+
},
|
|
83
|
+
"scripts": {
|
|
84
|
+
"gen:css": "node scripts/compile-css.mjs",
|
|
85
|
+
"prebuild": "rm -rf dist && pnpm gen:css && node scripts/check-version.mjs",
|
|
86
|
+
"pretest": "pnpm gen:css",
|
|
87
|
+
"pretypecheck": "pnpm gen:css",
|
|
88
|
+
"build": "tsup",
|
|
89
|
+
"postbuild": "node scripts/check-exports.mjs",
|
|
90
|
+
"test": "vitest run",
|
|
91
|
+
"typecheck": "tsc --noEmit"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Server-renders nothing visible — mounts the framework-free Prepr toolbar
|
|
4
|
+
* custom element client-side via `createPreprToolbar` (core).
|
|
5
|
+
*
|
|
6
|
+
* Ships as SOURCE: this file is compiled by the *consumer's* Astro/Vite
|
|
7
|
+
* pipeline (not by this package's own `tsup` build), matching how Astro
|
|
8
|
+
* component libraries distribute `.astro` files. See package.json's
|
|
9
|
+
* `./astro/components/*` export map entry and `files` field.
|
|
10
|
+
*
|
|
11
|
+
* Astro is multi-page (MPA) by default, so the default navigation adapter
|
|
12
|
+
* (core's `window.location.assign`, used when no `navigation` override is
|
|
13
|
+
* passed to `createPreprToolbar`) is the correct choice here — a full page
|
|
14
|
+
* navigation re-runs Astro's server-rendered output (and this component's
|
|
15
|
+
* frontmatter) with the new query params, which is what drives segment /
|
|
16
|
+
* A/B query-param personalization. No client-router
|
|
17
|
+
* integration is wired up; consumers using Astro's View Transitions/client
|
|
18
|
+
* router can pass their own adapter if they fork this component.
|
|
19
|
+
*
|
|
20
|
+
* Server → client props transfer: the props are emitted into a
|
|
21
|
+
* `<script type="application/json">` tag and read back by a SEPARATE,
|
|
22
|
+
* NON-inline `<script>` that Astro/Vite bundles. We deliberately do NOT use
|
|
23
|
+
* `define:vars`, which would force the mount script to be `is:inline` —
|
|
24
|
+
* Astro/Vite do not process bare `import` specifiers in inline scripts, so
|
|
25
|
+
* `import { createPreprToolbar } from '@preprio/toolkit'` would fail to
|
|
26
|
+
* resolve in the browser. A processed (bundled) module script gets its
|
|
27
|
+
* imports rewritten to real, hashed module URLs, so the import resolves.
|
|
28
|
+
* (JSON-serializable values only — matches `PreprToolbarProps`, a plain
|
|
29
|
+
* data shape of primitives/arrays.)
|
|
30
|
+
*/
|
|
31
|
+
import type { PreprToolbarProps } from '@preprio/toolkit';
|
|
32
|
+
|
|
33
|
+
type Props = PreprToolbarProps;
|
|
34
|
+
|
|
35
|
+
const { activeSegment, activeVariant, segments, data } = Astro.props as Props;
|
|
36
|
+
// `data` is the deprecated alias for `segments`; accept either.
|
|
37
|
+
const props = { activeSegment, activeVariant, segments: segments ?? data ?? [] };
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
<script
|
|
41
|
+
type="application/json"
|
|
42
|
+
data-prepr-toolbar-props
|
|
43
|
+
set:html={JSON.stringify(props)}
|
|
44
|
+
/>
|
|
45
|
+
|
|
46
|
+
<script>
|
|
47
|
+
import { createPreprToolbar } from '@preprio/toolkit';
|
|
48
|
+
|
|
49
|
+
const el = document.querySelector('script[data-prepr-toolbar-props]');
|
|
50
|
+
if (el?.textContent) {
|
|
51
|
+
createPreprToolbar({ props: JSON.parse(el.textContent) });
|
|
52
|
+
}
|
|
53
|
+
</script>
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Loads Prepr's CDN tracking pixel client-side via `loadTrackingPixel`
|
|
4
|
+
* (core). Renders nothing visible.
|
|
5
|
+
*
|
|
6
|
+
* Ships as SOURCE — see `PreprToolbar.astro` for the rationale (compiled by
|
|
7
|
+
* the consumer's own Astro pipeline, not this package's `tsup` build).
|
|
8
|
+
*
|
|
9
|
+
* Server → client props transfer: props are emitted into a
|
|
10
|
+
* `<script type="application/json">` tag and read back by a SEPARATE,
|
|
11
|
+
* NON-inline `<script>` that Astro/Vite bundles. We deliberately avoid
|
|
12
|
+
* `define:vars` (which forces `is:inline`) because inline scripts do not get
|
|
13
|
+
* their bare `import` specifiers processed — `import { loadTrackingPixel }
|
|
14
|
+
* from '@preprio/toolkit'` would fail to resolve in the browser. See
|
|
15
|
+
* `PreprToolbar.astro` for the full explanation.
|
|
16
|
+
*/
|
|
17
|
+
interface Props {
|
|
18
|
+
id: string;
|
|
19
|
+
config?: {
|
|
20
|
+
destinations?: { googleTagManager?: boolean };
|
|
21
|
+
variantImpressionThreshold?: number;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const { id, config } = Astro.props as Props;
|
|
26
|
+
// JSON-serialize into a plain data payload. An omitted `config` becomes
|
|
27
|
+
// `undefined`, which `JSON.stringify` drops from the object entirely — the
|
|
28
|
+
// mount script normalizes a missing `config` back to `undefined` so
|
|
29
|
+
// `loadTrackingPixel`'s `config !== undefined` check (core/pixel.ts) behaves
|
|
30
|
+
// the same as calling it directly with no config argument.
|
|
31
|
+
const payload = { id, config };
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
<script
|
|
35
|
+
type="application/json"
|
|
36
|
+
data-prepr-pixel-props
|
|
37
|
+
set:html={JSON.stringify(payload)}
|
|
38
|
+
/>
|
|
39
|
+
|
|
40
|
+
<script>
|
|
41
|
+
import { loadTrackingPixel } from '@preprio/toolkit';
|
|
42
|
+
|
|
43
|
+
const el = document.querySelector('script[data-prepr-pixel-props]');
|
|
44
|
+
if (el?.textContent) {
|
|
45
|
+
const { id, config } = JSON.parse(el.textContent);
|
|
46
|
+
loadTrackingPixel(id, config ?? undefined);
|
|
47
|
+
}
|
|
48
|
+
</script>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Mounts the Prepr toolbar custom element (and stega edit mode); renders
|
|
4
|
+
* nothing itself. `onMount` keeps it off the SSR path.
|
|
5
|
+
*
|
|
6
|
+
* Ships as source — the consumer's Vite/Svelte pipeline compiles this, not
|
|
7
|
+
* our tsup build. See the `./sveltekit/components/*` export in package.json.
|
|
8
|
+
*/
|
|
9
|
+
import { onMount } from 'svelte';
|
|
10
|
+
import { createPreprToolbar } from '@preprio/toolkit';
|
|
11
|
+
import type { PreprToolbarProps } from '@preprio/toolkit';
|
|
12
|
+
|
|
13
|
+
let { activeSegment, activeVariant, data, segments }: PreprToolbarProps = $props();
|
|
14
|
+
|
|
15
|
+
onMount(() => {
|
|
16
|
+
const controller = createPreprToolbar({
|
|
17
|
+
props: { activeSegment, activeVariant, data, segments },
|
|
18
|
+
});
|
|
19
|
+
return () => controller.destroy();
|
|
20
|
+
});
|
|
21
|
+
</script>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Loads Prepr's CDN tracking pixel client-side via `loadTrackingPixel`
|
|
4
|
+
* (core). Renders nothing visible. Ships as SOURCE — see PreprToolbar.svelte.
|
|
5
|
+
*/
|
|
6
|
+
import { onMount } from 'svelte';
|
|
7
|
+
import { loadTrackingPixel } from '@preprio/toolkit';
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
id: string;
|
|
11
|
+
config?: {
|
|
12
|
+
destinations?: { googleTagManager?: boolean };
|
|
13
|
+
variantImpressionThreshold?: number;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let { id, config }: Props = $props();
|
|
18
|
+
|
|
19
|
+
onMount(() => {
|
|
20
|
+
loadTrackingPixel(id, config ?? undefined);
|
|
21
|
+
});
|
|
22
|
+
</script>
|