@preprio/toolkit 0.1.0-beta.1 → 0.1.0-beta.4
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 +56 -3
- package/dist/astro/index.cjs +34 -10
- package/dist/astro/index.js +34 -10
- package/dist/{chunk-H7HVQSSF.js → chunk-3FOY6KL4.js} +100 -25
- package/dist/index.cjs +119 -29
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +20 -5
- package/dist/nextjs/components.cjs +100 -25
- package/dist/nextjs/components.js +1 -1
- package/dist/nextjs/index.cjs +23 -5
- package/dist/nextjs/index.js +23 -5
- package/dist/nuxt/index.cjs +399 -0
- package/dist/nuxt/index.d.cts +120 -0
- package/dist/nuxt/index.d.ts +120 -0
- package/dist/nuxt/index.js +362 -0
- package/dist/sveltekit/index.cjs +34 -10
- package/dist/sveltekit/index.js +34 -10
- package/package.json +11 -4
- package/src/nuxt/PreprToolbar.vue +38 -0
- package/src/nuxt/PreprTrackingPixel.vue +24 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// src/version.ts
|
|
2
|
+
var VERSION = "0.1.0-beta.4";
|
|
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 serializeCookie(cookie) {
|
|
24
|
+
const parts = [
|
|
25
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
26
|
+
`Max-Age=${cookie.maxAge}`,
|
|
27
|
+
`Path=${cookie.path}`
|
|
28
|
+
];
|
|
29
|
+
if (cookie.sameSite) {
|
|
30
|
+
parts.push(`SameSite=${cookie.sameSite}`);
|
|
31
|
+
}
|
|
32
|
+
if (cookie.secure) {
|
|
33
|
+
parts.push("Secure");
|
|
34
|
+
}
|
|
35
|
+
return parts.join("; ");
|
|
36
|
+
}
|
|
37
|
+
function parseCookies(request) {
|
|
38
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
39
|
+
const header = request.headers.get("cookie");
|
|
40
|
+
if (!header) return cookies;
|
|
41
|
+
for (const part of header.split(";")) {
|
|
42
|
+
const eq = part.indexOf("=");
|
|
43
|
+
if (eq === -1) continue;
|
|
44
|
+
const name = part.slice(0, eq).trim();
|
|
45
|
+
const value = part.slice(eq + 1).trim();
|
|
46
|
+
if (!name) continue;
|
|
47
|
+
cookies.set(name, decodeURIComponent(value));
|
|
48
|
+
}
|
|
49
|
+
return cookies;
|
|
50
|
+
}
|
|
51
|
+
function processPreprRequest(request, opts) {
|
|
52
|
+
const requestHeaders = new Headers(request.headers);
|
|
53
|
+
const responseCookies = [];
|
|
54
|
+
const requestCookies = parseCookies(request);
|
|
55
|
+
const url = new URL(request.url);
|
|
56
|
+
const searchParams = url.searchParams;
|
|
57
|
+
const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
58
|
+
const isSecure = forwardedProto ? forwardedProto === "https" : url.protocol === "https:";
|
|
59
|
+
const crossSite = isSecure ? { sameSite: "None", secure: true } : {};
|
|
60
|
+
for (const key of UTM_PARAMS) {
|
|
61
|
+
const value = searchParams.get(key);
|
|
62
|
+
if (value !== null) {
|
|
63
|
+
requestHeaders.set(`Prepr-Context-${key}`, value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const referer = request.headers.get("referer");
|
|
67
|
+
if (referer) {
|
|
68
|
+
requestHeaders.set("Prepr-Context-initial_referral", referer);
|
|
69
|
+
}
|
|
70
|
+
const userAgent = request.headers.get("user-agent");
|
|
71
|
+
if (userAgent) {
|
|
72
|
+
requestHeaders.set("Prepr-User-Agent", userAgent);
|
|
73
|
+
}
|
|
74
|
+
requestHeaders.set("Prepr-Package", `@preprio/toolkit@${opts?.version ?? VERSION}`);
|
|
75
|
+
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
76
|
+
if (ip) {
|
|
77
|
+
requestHeaders.set("Prepr-Visitor-IP", ip);
|
|
78
|
+
}
|
|
79
|
+
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
80
|
+
if (hutkCookie) {
|
|
81
|
+
requestHeaders.set("Prepr-Hubspot-Id", hutkCookie);
|
|
82
|
+
}
|
|
83
|
+
let uid = requestCookies.get(COOKIE_UID);
|
|
84
|
+
if (!uid) {
|
|
85
|
+
uid = crypto.randomUUID();
|
|
86
|
+
responseCookies.push({
|
|
87
|
+
name: COOKIE_UID,
|
|
88
|
+
value: uid,
|
|
89
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
90
|
+
path: "/"
|
|
91
|
+
});
|
|
92
|
+
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
93
|
+
}
|
|
94
|
+
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
95
|
+
if (!opts?.preview) {
|
|
96
|
+
return { requestHeaders, responseCookies };
|
|
97
|
+
}
|
|
98
|
+
const hasPreviewParams = searchParams.has(PARAM_SEGMENT) || searchParams.has(PARAM_VARIANT);
|
|
99
|
+
if (!hasPreviewParams) {
|
|
100
|
+
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
101
|
+
const previewModeEnabled = previewModeCookie !== "false";
|
|
102
|
+
if (!previewModeEnabled) {
|
|
103
|
+
return { requestHeaders, responseCookies };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
107
|
+
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
108
|
+
if (!isLivePreview && !requestCookies.has(COOKIE_PREVIEW_MODE)) {
|
|
109
|
+
responseCookies.push({
|
|
110
|
+
name: COOKIE_PREVIEW_MODE,
|
|
111
|
+
value: "true",
|
|
112
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
113
|
+
path: "/",
|
|
114
|
+
...crossSite
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (!isLivePreview) {
|
|
118
|
+
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
119
|
+
if (segmentCookie) {
|
|
120
|
+
requestHeaders.set("Prepr-Segments", segmentCookie);
|
|
121
|
+
}
|
|
122
|
+
const abCookie = requestCookies.get(COOKIE_VARIANT);
|
|
123
|
+
if (abCookie) {
|
|
124
|
+
requestHeaders.set("Prepr-ABtesting", abCookie);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const previewAb = searchParams.get(PARAM_VARIANT);
|
|
128
|
+
if (previewAb !== null) {
|
|
129
|
+
requestHeaders.set("Prepr-ABtesting", previewAb);
|
|
130
|
+
if (!isLivePreview) {
|
|
131
|
+
responseCookies.push({
|
|
132
|
+
name: COOKIE_VARIANT,
|
|
133
|
+
value: previewAb,
|
|
134
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
135
|
+
path: "/",
|
|
136
|
+
...crossSite
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const previewSegment = searchParams.get(PARAM_SEGMENT);
|
|
141
|
+
if (previewSegment !== null) {
|
|
142
|
+
requestHeaders.set("Prepr-Segments", previewSegment);
|
|
143
|
+
if (!isLivePreview) {
|
|
144
|
+
responseCookies.push({
|
|
145
|
+
name: COOKIE_SEGMENT,
|
|
146
|
+
value: previewSegment,
|
|
147
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
148
|
+
path: "/",
|
|
149
|
+
...crossSite
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { requestHeaders, responseCookies };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/nuxt/middleware.ts
|
|
157
|
+
var PREPR_CONTEXT_KEY = "prepr";
|
|
158
|
+
function handlePreprRequest(event, options) {
|
|
159
|
+
const req = event.node.req;
|
|
160
|
+
const host = typeof req.headers.host === "string" ? req.headers.host : "localhost";
|
|
161
|
+
const request = new Request(`http://${host}${req.url ?? "/"}`);
|
|
162
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
163
|
+
if (typeof value === "string") {
|
|
164
|
+
request.headers.set(key, value);
|
|
165
|
+
} else if (Array.isArray(value)) {
|
|
166
|
+
request.headers.set(key, value.join(", "));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const { requestHeaders, responseCookies } = processPreprRequest(request, options);
|
|
170
|
+
requestHeaders.forEach((value, key) => {
|
|
171
|
+
req.headers[key.toLowerCase()] = value;
|
|
172
|
+
});
|
|
173
|
+
event.context[PREPR_CONTEXT_KEY] = requestHeaders;
|
|
174
|
+
for (const cookie of responseCookies) {
|
|
175
|
+
event.node.res.appendHeader("Set-Cookie", serializeCookie(cookie));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function getPreprHeadersFromEvent(event) {
|
|
179
|
+
const headers = event.context[PREPR_CONTEXT_KEY];
|
|
180
|
+
return headers instanceof Headers ? headers : new Headers();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/core/server.ts
|
|
184
|
+
var PreprError = class extends Error {
|
|
185
|
+
constructor(message, code, context, originalError) {
|
|
186
|
+
super(message);
|
|
187
|
+
this.code = code;
|
|
188
|
+
this.context = context;
|
|
189
|
+
this.originalError = originalError;
|
|
190
|
+
this.name = "PreprError";
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
194
|
+
return headers.get("prepr-customer-id");
|
|
195
|
+
}
|
|
196
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
197
|
+
return headers.get("Prepr-Segments");
|
|
198
|
+
}
|
|
199
|
+
function getActiveVariantFromHeaders(headers) {
|
|
200
|
+
return headers.get("Prepr-ABtesting");
|
|
201
|
+
}
|
|
202
|
+
var PREPR_HEADER_KEYS = [
|
|
203
|
+
"prepr-customer-id",
|
|
204
|
+
"Prepr-Segments",
|
|
205
|
+
"Prepr-ABtesting",
|
|
206
|
+
"Prepr-Preview-Bar",
|
|
207
|
+
"Prepr-Context-utm_source",
|
|
208
|
+
"Prepr-Context-utm_medium",
|
|
209
|
+
"Prepr-Context-utm_term",
|
|
210
|
+
"Prepr-Context-utm_content",
|
|
211
|
+
"Prepr-Context-utm_campaign",
|
|
212
|
+
"Prepr-Context-initial_referral",
|
|
213
|
+
"Prepr-Visitor-IP",
|
|
214
|
+
"Prepr-Hubspot-Id",
|
|
215
|
+
"Prepr-Customer-Id-Created",
|
|
216
|
+
"Prepr-User-Agent"
|
|
217
|
+
];
|
|
218
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
219
|
+
const preprHeaders = {};
|
|
220
|
+
const copy = (key) => {
|
|
221
|
+
const value = headers.get(key);
|
|
222
|
+
if (value !== null) {
|
|
223
|
+
preprHeaders[key] = value;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
227
|
+
copy(key);
|
|
228
|
+
}
|
|
229
|
+
return preprHeaders;
|
|
230
|
+
}
|
|
231
|
+
function validatePreprToken(token) {
|
|
232
|
+
if (!token) {
|
|
233
|
+
throw new PreprError("Token is required", "MISSING_TOKEN", "validatePreprToken");
|
|
234
|
+
}
|
|
235
|
+
if (!token.startsWith("https://")) {
|
|
236
|
+
throw new PreprError(
|
|
237
|
+
"Token must be a valid HTTPS URL",
|
|
238
|
+
"INVALID_TOKEN",
|
|
239
|
+
"validatePreprToken"
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function extractAccessToken(graphqlUrl) {
|
|
244
|
+
let url;
|
|
245
|
+
try {
|
|
246
|
+
url = new URL(graphqlUrl);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw new PreprError(
|
|
249
|
+
"Token must be a valid URL",
|
|
250
|
+
"INVALID_TOKEN",
|
|
251
|
+
"extractAccessToken",
|
|
252
|
+
error instanceof Error ? error : new Error(String(error))
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
256
|
+
throw new PreprError(
|
|
257
|
+
"Token must be a graphql.prepr.io URL",
|
|
258
|
+
"INVALID_TOKEN",
|
|
259
|
+
"extractAccessToken"
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
const pathParts = url.pathname.split("/");
|
|
263
|
+
const token = pathParts[pathParts.length - 1];
|
|
264
|
+
if (!token) {
|
|
265
|
+
throw new PreprError(
|
|
266
|
+
"Token URL is missing an access token segment",
|
|
267
|
+
"INVALID_TOKEN",
|
|
268
|
+
"extractAccessToken"
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
return token;
|
|
272
|
+
}
|
|
273
|
+
async function getPreprEnvironmentSegments(token) {
|
|
274
|
+
validatePreprToken(token);
|
|
275
|
+
try {
|
|
276
|
+
const response = await fetch(token, {
|
|
277
|
+
headers: {
|
|
278
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
279
|
+
"Content-Type": "application/json"
|
|
280
|
+
},
|
|
281
|
+
method: "POST",
|
|
282
|
+
body: JSON.stringify({
|
|
283
|
+
query: `{
|
|
284
|
+
_Segments {
|
|
285
|
+
_id
|
|
286
|
+
name
|
|
287
|
+
}
|
|
288
|
+
}`
|
|
289
|
+
})
|
|
290
|
+
});
|
|
291
|
+
if (!response.ok) {
|
|
292
|
+
throw new PreprError(
|
|
293
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
294
|
+
"HTTP_ERROR",
|
|
295
|
+
"getPreprEnvironmentSegments"
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
const json = await response.json();
|
|
299
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
300
|
+
throw new PreprError(
|
|
301
|
+
"Invalid response format from Prepr API",
|
|
302
|
+
"INVALID_RESPONSE",
|
|
303
|
+
"getPreprEnvironmentSegments"
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
return json.data._Segments;
|
|
307
|
+
} catch (error) {
|
|
308
|
+
if (error instanceof PreprError) {
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
throw new PreprError(
|
|
312
|
+
"Failed to fetch segments from Prepr API",
|
|
313
|
+
"FETCH_ERROR",
|
|
314
|
+
"getPreprEnvironmentSegments",
|
|
315
|
+
error instanceof Error ? error : new Error(String(error))
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async function getToolbarPropsFromHeaders(headers, token) {
|
|
320
|
+
let segments = [];
|
|
321
|
+
try {
|
|
322
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
323
|
+
} catch {
|
|
324
|
+
segments = [];
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
activeSegment: getActiveSegmentFromHeaders(headers),
|
|
328
|
+
activeVariant: getActiveVariantFromHeaders(headers),
|
|
329
|
+
segments,
|
|
330
|
+
data: segments
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/nuxt/server.ts
|
|
335
|
+
function getPreprUUID(headers) {
|
|
336
|
+
return getPreprUUIDFromHeaders(headers);
|
|
337
|
+
}
|
|
338
|
+
function getActiveSegment(headers) {
|
|
339
|
+
return getActiveSegmentFromHeaders(headers);
|
|
340
|
+
}
|
|
341
|
+
function getActiveVariant(headers) {
|
|
342
|
+
return getActiveVariantFromHeaders(headers);
|
|
343
|
+
}
|
|
344
|
+
function getPreprHeaders(headers) {
|
|
345
|
+
return getPreprHeadersFromHeaders(headers);
|
|
346
|
+
}
|
|
347
|
+
async function getToolbarProps(headers, token) {
|
|
348
|
+
return getToolbarPropsFromHeaders(headers, token);
|
|
349
|
+
}
|
|
350
|
+
export {
|
|
351
|
+
PREPR_CONTEXT_KEY,
|
|
352
|
+
PreprError,
|
|
353
|
+
extractAccessToken,
|
|
354
|
+
getActiveSegment,
|
|
355
|
+
getActiveVariant,
|
|
356
|
+
getPreprHeaders,
|
|
357
|
+
getPreprHeadersFromEvent,
|
|
358
|
+
getPreprUUID,
|
|
359
|
+
getToolbarProps,
|
|
360
|
+
handlePreprRequest,
|
|
361
|
+
validatePreprToken
|
|
362
|
+
};
|
package/dist/sveltekit/index.cjs
CHANGED
|
@@ -35,7 +35,7 @@ __export(sveltekit_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(sveltekit_exports);
|
|
36
36
|
|
|
37
37
|
// src/version.ts
|
|
38
|
-
var VERSION = "0.1.0-beta.
|
|
38
|
+
var VERSION = "0.1.0-beta.4";
|
|
39
39
|
|
|
40
40
|
// src/core/constants.ts
|
|
41
41
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -56,6 +56,20 @@ var UTM_PARAMS = [
|
|
|
56
56
|
"utm_content",
|
|
57
57
|
"utm_campaign"
|
|
58
58
|
];
|
|
59
|
+
function serializeCookie(cookie) {
|
|
60
|
+
const parts = [
|
|
61
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
62
|
+
`Max-Age=${cookie.maxAge}`,
|
|
63
|
+
`Path=${cookie.path}`
|
|
64
|
+
];
|
|
65
|
+
if (cookie.sameSite) {
|
|
66
|
+
parts.push(`SameSite=${cookie.sameSite}`);
|
|
67
|
+
}
|
|
68
|
+
if (cookie.secure) {
|
|
69
|
+
parts.push("Secure");
|
|
70
|
+
}
|
|
71
|
+
return parts.join("; ");
|
|
72
|
+
}
|
|
59
73
|
function parseCookies(request) {
|
|
60
74
|
const cookies = /* @__PURE__ */ new Map();
|
|
61
75
|
const header = request.headers.get("cookie");
|
|
@@ -74,7 +88,11 @@ function processPreprRequest(request, opts) {
|
|
|
74
88
|
const requestHeaders = new Headers(request.headers);
|
|
75
89
|
const responseCookies = [];
|
|
76
90
|
const requestCookies = parseCookies(request);
|
|
77
|
-
const
|
|
91
|
+
const url = new URL(request.url);
|
|
92
|
+
const searchParams = url.searchParams;
|
|
93
|
+
const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
94
|
+
const isSecure = forwardedProto ? forwardedProto === "https" : url.protocol === "https:";
|
|
95
|
+
const crossSite = isSecure ? { sameSite: "None", secure: true } : {};
|
|
78
96
|
for (const key of UTM_PARAMS) {
|
|
79
97
|
const value = searchParams.get(key);
|
|
80
98
|
if (value !== null) {
|
|
@@ -123,6 +141,15 @@ function processPreprRequest(request, opts) {
|
|
|
123
141
|
}
|
|
124
142
|
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
125
143
|
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
144
|
+
if (!isLivePreview && !requestCookies.has(COOKIE_PREVIEW_MODE)) {
|
|
145
|
+
responseCookies.push({
|
|
146
|
+
name: COOKIE_PREVIEW_MODE,
|
|
147
|
+
value: "true",
|
|
148
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
149
|
+
path: "/",
|
|
150
|
+
...crossSite
|
|
151
|
+
});
|
|
152
|
+
}
|
|
126
153
|
if (!isLivePreview) {
|
|
127
154
|
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
128
155
|
if (segmentCookie) {
|
|
@@ -141,7 +168,8 @@ function processPreprRequest(request, opts) {
|
|
|
141
168
|
name: COOKIE_VARIANT,
|
|
142
169
|
value: previewAb,
|
|
143
170
|
maxAge: ONE_YEAR_SECONDS,
|
|
144
|
-
path: "/"
|
|
171
|
+
path: "/",
|
|
172
|
+
...crossSite
|
|
145
173
|
});
|
|
146
174
|
}
|
|
147
175
|
}
|
|
@@ -153,7 +181,8 @@ function processPreprRequest(request, opts) {
|
|
|
153
181
|
name: COOKIE_SEGMENT,
|
|
154
182
|
value: previewSegment,
|
|
155
183
|
maxAge: ONE_YEAR_SECONDS,
|
|
156
|
-
path: "/"
|
|
184
|
+
path: "/",
|
|
185
|
+
...crossSite
|
|
157
186
|
});
|
|
158
187
|
}
|
|
159
188
|
}
|
|
@@ -173,12 +202,7 @@ function preprHandle(options) {
|
|
|
173
202
|
}
|
|
174
203
|
const response = await resolve(event);
|
|
175
204
|
for (const cookie of responseCookies) {
|
|
176
|
-
|
|
177
|
-
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
178
|
-
`Max-Age=${cookie.maxAge}`,
|
|
179
|
-
`Path=${cookie.path}`
|
|
180
|
-
];
|
|
181
|
-
response.headers.append("Set-Cookie", parts.join("; "));
|
|
205
|
+
response.headers.append("Set-Cookie", serializeCookie(cookie));
|
|
182
206
|
}
|
|
183
207
|
return response;
|
|
184
208
|
};
|
package/dist/sveltekit/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var VERSION = "0.1.0-beta.
|
|
2
|
+
var VERSION = "0.1.0-beta.4";
|
|
3
3
|
|
|
4
4
|
// src/core/constants.ts
|
|
5
5
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -20,6 +20,20 @@ var UTM_PARAMS = [
|
|
|
20
20
|
"utm_content",
|
|
21
21
|
"utm_campaign"
|
|
22
22
|
];
|
|
23
|
+
function serializeCookie(cookie) {
|
|
24
|
+
const parts = [
|
|
25
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
26
|
+
`Max-Age=${cookie.maxAge}`,
|
|
27
|
+
`Path=${cookie.path}`
|
|
28
|
+
];
|
|
29
|
+
if (cookie.sameSite) {
|
|
30
|
+
parts.push(`SameSite=${cookie.sameSite}`);
|
|
31
|
+
}
|
|
32
|
+
if (cookie.secure) {
|
|
33
|
+
parts.push("Secure");
|
|
34
|
+
}
|
|
35
|
+
return parts.join("; ");
|
|
36
|
+
}
|
|
23
37
|
function parseCookies(request) {
|
|
24
38
|
const cookies = /* @__PURE__ */ new Map();
|
|
25
39
|
const header = request.headers.get("cookie");
|
|
@@ -38,7 +52,11 @@ function processPreprRequest(request, opts) {
|
|
|
38
52
|
const requestHeaders = new Headers(request.headers);
|
|
39
53
|
const responseCookies = [];
|
|
40
54
|
const requestCookies = parseCookies(request);
|
|
41
|
-
const
|
|
55
|
+
const url = new URL(request.url);
|
|
56
|
+
const searchParams = url.searchParams;
|
|
57
|
+
const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
58
|
+
const isSecure = forwardedProto ? forwardedProto === "https" : url.protocol === "https:";
|
|
59
|
+
const crossSite = isSecure ? { sameSite: "None", secure: true } : {};
|
|
42
60
|
for (const key of UTM_PARAMS) {
|
|
43
61
|
const value = searchParams.get(key);
|
|
44
62
|
if (value !== null) {
|
|
@@ -87,6 +105,15 @@ function processPreprRequest(request, opts) {
|
|
|
87
105
|
}
|
|
88
106
|
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
89
107
|
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
108
|
+
if (!isLivePreview && !requestCookies.has(COOKIE_PREVIEW_MODE)) {
|
|
109
|
+
responseCookies.push({
|
|
110
|
+
name: COOKIE_PREVIEW_MODE,
|
|
111
|
+
value: "true",
|
|
112
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
113
|
+
path: "/",
|
|
114
|
+
...crossSite
|
|
115
|
+
});
|
|
116
|
+
}
|
|
90
117
|
if (!isLivePreview) {
|
|
91
118
|
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
92
119
|
if (segmentCookie) {
|
|
@@ -105,7 +132,8 @@ function processPreprRequest(request, opts) {
|
|
|
105
132
|
name: COOKIE_VARIANT,
|
|
106
133
|
value: previewAb,
|
|
107
134
|
maxAge: ONE_YEAR_SECONDS,
|
|
108
|
-
path: "/"
|
|
135
|
+
path: "/",
|
|
136
|
+
...crossSite
|
|
109
137
|
});
|
|
110
138
|
}
|
|
111
139
|
}
|
|
@@ -117,7 +145,8 @@ function processPreprRequest(request, opts) {
|
|
|
117
145
|
name: COOKIE_SEGMENT,
|
|
118
146
|
value: previewSegment,
|
|
119
147
|
maxAge: ONE_YEAR_SECONDS,
|
|
120
|
-
path: "/"
|
|
148
|
+
path: "/",
|
|
149
|
+
...crossSite
|
|
121
150
|
});
|
|
122
151
|
}
|
|
123
152
|
}
|
|
@@ -137,12 +166,7 @@ function preprHandle(options) {
|
|
|
137
166
|
}
|
|
138
167
|
const response = await resolve(event);
|
|
139
168
|
for (const cookie of responseCookies) {
|
|
140
|
-
|
|
141
|
-
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
142
|
-
`Max-Age=${cookie.maxAge}`,
|
|
143
|
-
`Path=${cookie.path}`
|
|
144
|
-
];
|
|
145
|
-
response.headers.append("Set-Cookie", parts.join("; "));
|
|
169
|
+
response.headers.append("Set-Cookie", serializeCookie(cookie));
|
|
146
170
|
}
|
|
147
171
|
return response;
|
|
148
172
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@preprio/toolkit",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
4
|
-
"description": "Prepr personalization, A/B testing and preview toolkit for Next.js, Astro, SvelteKit and Express",
|
|
3
|
+
"version": "0.1.0-beta.4",
|
|
4
|
+
"description": "Prepr personalization, A/B testing and preview toolkit for Next.js, Nuxt, Astro, SvelteKit and Express",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -39,12 +39,19 @@
|
|
|
39
39
|
"import": "./dist/sveltekit/index.js",
|
|
40
40
|
"require": "./dist/sveltekit/index.cjs"
|
|
41
41
|
},
|
|
42
|
-
"./sveltekit/components/*": "./src/sveltekit/*.svelte"
|
|
42
|
+
"./sveltekit/components/*": "./src/sveltekit/*.svelte",
|
|
43
|
+
"./nuxt": {
|
|
44
|
+
"types": "./dist/nuxt/index.d.ts",
|
|
45
|
+
"import": "./dist/nuxt/index.js",
|
|
46
|
+
"require": "./dist/nuxt/index.cjs"
|
|
47
|
+
},
|
|
48
|
+
"./nuxt/components/*": "./src/nuxt/*.vue"
|
|
43
49
|
},
|
|
44
50
|
"files": [
|
|
45
51
|
"dist",
|
|
46
52
|
"src/astro/*.astro",
|
|
47
|
-
"src/sveltekit/*.svelte"
|
|
53
|
+
"src/sveltekit/*.svelte",
|
|
54
|
+
"src/nuxt/*.vue"
|
|
48
55
|
],
|
|
49
56
|
"dependencies": {
|
|
50
57
|
"@vercel/stega": "^1.1.0",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Mounts the Prepr toolbar custom element (and stega edit mode); renders
|
|
4
|
+
* nothing itself. `onMounted` keeps it off the SSR path.
|
|
5
|
+
*
|
|
6
|
+
* Ships as source — the consumer's Vite/Vue pipeline compiles this, not our
|
|
7
|
+
* tsup build. See the `./nuxt/components/*` export in package.json.
|
|
8
|
+
*/
|
|
9
|
+
import { onMounted, onUnmounted } from 'vue';
|
|
10
|
+
import { createPreprToolbar } from '@preprio/toolkit';
|
|
11
|
+
import type { PreprToolbarProps } from '@preprio/toolkit';
|
|
12
|
+
|
|
13
|
+
const props = defineProps<{
|
|
14
|
+
activeSegment: PreprToolbarProps['activeSegment'];
|
|
15
|
+
activeVariant: PreprToolbarProps['activeVariant'];
|
|
16
|
+
segments?: PreprToolbarProps['segments'];
|
|
17
|
+
data?: PreprToolbarProps['data'];
|
|
18
|
+
}>();
|
|
19
|
+
|
|
20
|
+
let controller: ReturnType<typeof createPreprToolbar> | null = null;
|
|
21
|
+
|
|
22
|
+
onMounted(() => {
|
|
23
|
+
controller = createPreprToolbar({
|
|
24
|
+
props: {
|
|
25
|
+
activeSegment: props.activeSegment,
|
|
26
|
+
activeVariant: props.activeVariant,
|
|
27
|
+
segments: props.segments,
|
|
28
|
+
data: props.data,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
onUnmounted(() => controller?.destroy());
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<template>
|
|
37
|
+
<!-- renders nothing; the toolbar is a custom element mounted imperatively -->
|
|
38
|
+
</template>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Loads Prepr's CDN tracking pixel client-side via `loadTrackingPixel`
|
|
4
|
+
* (core). Renders nothing visible. Ships as SOURCE — see PreprToolbar.vue.
|
|
5
|
+
*/
|
|
6
|
+
import { onMounted } from 'vue';
|
|
7
|
+
import { loadTrackingPixel } from '@preprio/toolkit';
|
|
8
|
+
|
|
9
|
+
const props = defineProps<{
|
|
10
|
+
id: string;
|
|
11
|
+
config?: {
|
|
12
|
+
destinations?: { googleTagManager?: boolean };
|
|
13
|
+
variantImpressionThreshold?: number;
|
|
14
|
+
};
|
|
15
|
+
}>();
|
|
16
|
+
|
|
17
|
+
onMounted(() => {
|
|
18
|
+
loadTrackingPixel(props.id, props.config ?? undefined);
|
|
19
|
+
});
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<template>
|
|
23
|
+
<!-- renders nothing; the pixel is injected as a script tag -->
|
|
24
|
+
</template>
|