Skip to content

Commit

Permalink
Update server network debug tool (#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel authored Jan 11, 2024
1 parent 92840e5 commit 335371c
Show file tree
Hide file tree
Showing 25 changed files with 2,042 additions and 450 deletions.
58 changes: 58 additions & 0 deletions .changeset/neat-falcons-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
'@shopify/remix-oxygen': patch
'@shopify/hydrogen': patch
'@shopify/cli-hydrogen': patch
'@shopify/create-hydrogen': patch
---

Subrequest Profiler (stable) - Provides an overview of network requests happening on the server side

#### How to use:

1. Run `h2 dev`
2. Visit http://localhost:3000/subrequest-profiler

#### Set request display name with `storefront.query`:

```tsx
context.storefront.query(
HOMEPAGE_FEATURED_PRODUCTS_QUERY,
{
displayName: 'Feature products',
variables: {
country,
language,
},
},
)
```

#### Set request debug information with `createWithCache`:

```tsx
const withCache = createWithCache({
cache,
waitUntil,
request,
});

const data3p = async () => {
return await withCache(
['Some unique cache keys'],
CacheLong(),
({addDebugData}) => {
return fetch('https://some-3p-endpoint.com').then(async (response) => {
if (process.env.NODE_ENV === 'development') {
addDebugData({
displayName: '3p endpoint display name',
response,
});
}

return await response.json();
});
},
);
};
```

89 changes: 18 additions & 71 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "module",
"scripts": {
"build": "tsup && node scripts/build-check.mjs",
"dev": "tsup --watch",
"dev": "tsup --watch ./src",
"typecheck": "tsc --noEmit",
"generate:manifest": "node scripts/generate-manifest.mjs",
"test": "cross-env SHOPIFY_UNIT_TEST=1 vitest run --test-timeout=20000",
Expand All @@ -26,7 +26,7 @@
"@vitest/coverage-v8": "^1.0.4",
"devtools-protocol": "^0.0.1177611",
"fast-glob": "^3.2.12",
"flame-chart-js": "2.3.1",
"flame-chart-js": "2.3.2",
"get-port": "^7.0.0",
"type-fest": "^4.5.0",
"vitest": "^1.0.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/hydrogen/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async function runDev({
})}`,
),
colors.dim(
`\nView server-side network requests: ${miniOxygen.listeningAt}/debug-network`,
`\nView server network requests: ${miniOxygen.listeningAt}/subrequest-profiler`,
),
],
});
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/lib/mini-oxygen/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {MiniOxygenInstance, MiniOxygenOptions} from './types.js';
import {OXYGEN_HEADERS_MAP, logRequestLine} from './common.js';
import {
H2O_BINDING_NAME,
logRequestEvent,
createLogRequestEvent,
handleDebugNetworkRequest,
setConstructors,
} from '../request-events.js';
Expand All @@ -35,6 +35,7 @@ export async function startNodeServer({

setConstructors({Response});

const logRequestEvent = createLogRequestEvent();
const asyncLocalStorage = new AsyncLocalStorage();
const serviceBindings = {
[H2O_BINDING_NAME]: {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/mini-oxygen/workerd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {OXYGEN_HEADERS_MAP, logRequestLine} from './common.js';
import {
H2O_BINDING_NAME,
handleDebugNetworkRequest,
logRequestEvent,
createLogRequestEvent,
setConstructors,
} from '../request-events.js';
import {
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function startWorkerdServer({
compatibilityDate: '2022-10-31',
bindings: {...env},
serviceBindings: {
[H2O_BINDING_NAME]: logRequestEvent,
[H2O_BINDING_NAME]: createLogRequestEvent({absoluteBundlePath}),
},
},
],
Expand Down
Loading

0 comments on commit 335371c

Please sign in to comment.