@pack/hydrogen 3.1.1 → 3.2.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.
- package/README.md +54 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.js +31 -6
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1 +1,54 @@
|
|
|
1
|
-
# hydrogen
|
|
1
|
+
# `@pack/hydrogen`
|
|
2
|
+
|
|
3
|
+
Hydrogen runtime integration for Pack content, preview, A/B testing, and server-side error tracking.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pack/hydrogen
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Main exports
|
|
12
|
+
|
|
13
|
+
- `createPackClient`
|
|
14
|
+
- `handleRequest`
|
|
15
|
+
- `PackSession`, `PackTestSession`
|
|
16
|
+
- `previewModeAction`, `previewModeLoader`
|
|
17
|
+
- A/B test helpers (`PackTestProvider`, `useAbTest`, etc.)
|
|
18
|
+
|
|
19
|
+
## Minimal setup
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import {
|
|
23
|
+
createPackClient,
|
|
24
|
+
handleRequest,
|
|
25
|
+
} from '@pack/hydrogen';
|
|
26
|
+
import { createServerErrorHandler } from '@pack/errors';
|
|
27
|
+
|
|
28
|
+
// Optional if you want route-level error capture in addition to handleRequest wrapping.
|
|
29
|
+
// Duplicate reports for the same Error instance are deduplicated automatically.
|
|
30
|
+
export const handleError = createServerErrorHandler();
|
|
31
|
+
|
|
32
|
+
export default async function fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
|
33
|
+
const pack = createPackClient({
|
|
34
|
+
cache: caches.default,
|
|
35
|
+
waitUntil: ctx.waitUntil.bind(ctx),
|
|
36
|
+
storeId: env.PACK_STORE_ID,
|
|
37
|
+
token: env.PACK_API_TOKEN,
|
|
38
|
+
errorTracking: {
|
|
39
|
+
dsn: env.PUBLIC_PACK_ERROR_TRACKING_DSN,
|
|
40
|
+
},
|
|
41
|
+
session,
|
|
42
|
+
testSession,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return handleRequest(pack, request, (req) => storefrontHandler(req));
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Development
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
yarn
|
|
53
|
+
yarn build
|
|
54
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PackClient } from '@pack/client';
|
|
2
|
+
import { ErrorTrackingOptions } from '@pack/errors';
|
|
2
3
|
import { CacheCustom } from '@shopify/hydrogen';
|
|
3
4
|
import { SessionStorage, Session, ActionFunctionArgs, LoaderFunctionArgs } from 'react-router';
|
|
4
5
|
import * as react from 'react';
|
|
@@ -85,6 +86,8 @@ interface CreatePackClientOptions extends EnvironmentOptions {
|
|
|
85
86
|
i18n?: I18nOptions;
|
|
86
87
|
/** Default theme data to use when no token is provided */
|
|
87
88
|
defaultThemeData?: DefaultThemeData;
|
|
89
|
+
/** Configuration for server-side error tracking */
|
|
90
|
+
errorTracking?: ErrorTrackingOptions;
|
|
88
91
|
/**
|
|
89
92
|
* Initial request to extract query parameters from.
|
|
90
93
|
* If not provided, it will be captured from the first handleRequest call.
|
|
@@ -142,12 +145,14 @@ interface Pack {
|
|
|
142
145
|
packAbTest: Test$1 | null | undefined;
|
|
143
146
|
packIsPreviewMode: boolean;
|
|
144
147
|
packCustomizerMeta: PackCustomizerMeta | null;
|
|
148
|
+
packErrorTracking?: ErrorTrackingOptions;
|
|
145
149
|
};
|
|
146
150
|
handleRequest(request: Request): Promise<(response: Response) => void>;
|
|
147
151
|
isPreviewModeEnabled: () => boolean;
|
|
148
152
|
isValidEditToken: PackClient["isValidEditToken"];
|
|
149
153
|
query: <T = any>(query: string, options: QueryOptions) => Promise<QueryResponse<T>>;
|
|
150
154
|
session: PackSession;
|
|
155
|
+
errorTracking?: ErrorTrackingOptions;
|
|
151
156
|
testSession: PackTestSession;
|
|
152
157
|
}
|
|
153
158
|
interface DefaultThemeData {
|
package/dist/index.js
CHANGED
|
@@ -4356,6 +4356,9 @@ var require_dist = __commonJS({
|
|
|
4356
4356
|
}
|
|
4357
4357
|
});
|
|
4358
4358
|
|
|
4359
|
+
// src/handle-request.ts
|
|
4360
|
+
import { sendServerErrorEvent } from "@pack/errors";
|
|
4361
|
+
|
|
4359
4362
|
// ../packlytics/dist/utils/get-packlytics-id.js
|
|
4360
4363
|
function sha256(ascii) {
|
|
4361
4364
|
function rightRotate(value, amount) {
|
|
@@ -4668,11 +4671,28 @@ async function packlytics(pack, request, next) {
|
|
|
4668
4671
|
}
|
|
4669
4672
|
|
|
4670
4673
|
// src/handle-request.ts
|
|
4674
|
+
function getServerStatusError(request, status) {
|
|
4675
|
+
let pathname = request.url;
|
|
4676
|
+
try {
|
|
4677
|
+
pathname = new URL(request.url).pathname;
|
|
4678
|
+
} catch {
|
|
4679
|
+
}
|
|
4680
|
+
return new Error(`HTTP ${status} response for ${request.method} ${pathname}`);
|
|
4681
|
+
}
|
|
4671
4682
|
async function handleRequest(pack, request, handleRequest2) {
|
|
4672
4683
|
const packHandleResponse = await pack.handleRequest(request);
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4684
|
+
let response;
|
|
4685
|
+
try {
|
|
4686
|
+
response = await packlytics(pack, request, () => {
|
|
4687
|
+
return handleRequest2(request);
|
|
4688
|
+
});
|
|
4689
|
+
} catch (error) {
|
|
4690
|
+
sendServerErrorEvent(error, request, pack);
|
|
4691
|
+
throw error;
|
|
4692
|
+
}
|
|
4693
|
+
if (response.status >= 500 && !request.signal.aborted) {
|
|
4694
|
+
sendServerErrorEvent(getServerStatusError(request, response.status), request, pack);
|
|
4695
|
+
}
|
|
4676
4696
|
packHandleResponse(response);
|
|
4677
4697
|
response.headers.append("powered-by", "Shopify, Hydrogen + Pack Digital");
|
|
4678
4698
|
response.headers.append("Set-Cookie", await pack.session.commit());
|
|
@@ -6160,6 +6180,7 @@ function createPackClient(options) {
|
|
|
6160
6180
|
token,
|
|
6161
6181
|
apiUrl,
|
|
6162
6182
|
defaultThemeData,
|
|
6183
|
+
errorTracking,
|
|
6163
6184
|
i18n,
|
|
6164
6185
|
request
|
|
6165
6186
|
} = options;
|
|
@@ -6234,7 +6255,8 @@ function createPackClient(options) {
|
|
|
6234
6255
|
previewEnabled
|
|
6235
6256
|
),
|
|
6236
6257
|
packIsPreviewMode: previewEnabled,
|
|
6237
|
-
packCustomizerMeta: session.get("customizerMeta")
|
|
6258
|
+
packCustomizerMeta: session.get("customizerMeta"),
|
|
6259
|
+
packErrorTracking: errorTracking
|
|
6238
6260
|
};
|
|
6239
6261
|
},
|
|
6240
6262
|
isPreviewModeEnabled: () => previewEnabled,
|
|
@@ -6249,6 +6271,7 @@ function createPackClient(options) {
|
|
|
6249
6271
|
return { data, error: null };
|
|
6250
6272
|
},
|
|
6251
6273
|
session,
|
|
6274
|
+
errorTracking,
|
|
6252
6275
|
testSession
|
|
6253
6276
|
};
|
|
6254
6277
|
}
|
|
@@ -6298,7 +6321,8 @@ function createPackClient(options) {
|
|
|
6298
6321
|
previewEnabled
|
|
6299
6322
|
),
|
|
6300
6323
|
packIsPreviewMode: previewEnabled,
|
|
6301
|
-
packCustomizerMeta: session.get("customizerMeta")
|
|
6324
|
+
packCustomizerMeta: session.get("customizerMeta"),
|
|
6325
|
+
packErrorTracking: errorTracking
|
|
6302
6326
|
};
|
|
6303
6327
|
},
|
|
6304
6328
|
handleRequest: handleRequest2,
|
|
@@ -6363,7 +6387,7 @@ function createPackClient(options) {
|
|
|
6363
6387
|
}
|
|
6364
6388
|
}
|
|
6365
6389
|
if (testInfoForRequest?.isFirstExposure) {
|
|
6366
|
-
const { isFirstExposure, ...testInfo } = testInfoForRequest;
|
|
6390
|
+
const { isFirstExposure: _isFirstExposure, ...testInfo } = testInfoForRequest;
|
|
6367
6391
|
testInfoForLoader = testInfo;
|
|
6368
6392
|
}
|
|
6369
6393
|
headers = setTestHeaders(headers, {
|
|
@@ -6425,6 +6449,7 @@ function createPackClient(options) {
|
|
|
6425
6449
|
}
|
|
6426
6450
|
},
|
|
6427
6451
|
session,
|
|
6452
|
+
errorTracking,
|
|
6428
6453
|
testSession
|
|
6429
6454
|
};
|
|
6430
6455
|
}
|