Skip to content

Commit

Permalink
Support withCache in debug-network for workerd (#1438)
Browse files Browse the repository at this point in the history
* Support withCache debug-network for workerd

* Changesets
  • Loading branch information
frandiox authored Oct 22, 2023
1 parent f5b0573 commit 945c55a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
19 changes: 19 additions & 0 deletions .changeset/unlucky-wolves-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@shopify/hydrogen': patch
---

Calls to `withCache` can now be shown in the `/debug-network` tool when using the Worker runtime. For this to work, use the new `request` parameter in `createWithCache`:

```diff
export default {
fetch(request, env, executionContext) {
// ...
const withCache = createWithCache({
cache,
waitUntil,
+ request,
});
// ...
},
}
```
4 changes: 2 additions & 2 deletions packages/hydrogen/src/cache/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
export type CacheKey = string | readonly unknown[];

export type FetchDebugInfo = {
requestId?: string;
graphql?: string;
requestId?: string | null;
graphql?: string | null;
purpose?: string | null;
};

Expand Down
3 changes: 2 additions & 1 deletion packages/hydrogen/src/with-cache.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default {
const cache = await caches.open('my-cms');
const withCache = createWithCache({
cache,
waitUntil: executionContext.waitUntil,
waitUntil: executionContext.waitUntil.bind(executionContext),
request,
});

// Create a custom utility to query a third-party API:
Expand Down
3 changes: 2 additions & 1 deletion packages/hydrogen/src/with-cache.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default {
const cache = await caches.open('my-cms');
const withCache = createWithCache({
cache,
waitUntil: executionContext.waitUntil,
waitUntil: executionContext.waitUntil.bind(executionContext),
request,
});

// Create a custom utility to query a third-party API:
Expand Down
28 changes: 23 additions & 5 deletions packages/hydrogen/src/with-cache.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import {type CacheKey, runWithCache} from './cache/fetch';
import type {CachingStrategy} from './cache/strategies';

type CrossRuntimeRequest = {
headers: {
get?: (key: string) => string | null | undefined;
[key: string]: any;
};
};

type CreateWithCacheOptions = {
/** An instance that implements the [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache) */
cache: Cache;
/** The `waitUntil` function is used to keep the current request/response lifecycle alive even after a response has been sent. It should be provided by your platform. */
waitUntil: ExecutionContext['waitUntil'];
/** The `request` object is used to access certain headers for debugging */
request?: CrossRuntimeRequest;
};

function getHeader(key: string, request?: CrossRuntimeRequest) {
const value = request?.headers?.get?.(key) ?? request?.headers?.[key];
return typeof value === 'string' ? value : undefined;
}

/**
* Creates a utility function that executes an asynchronous operation
* like `fetch` and caches the result according to the strategy provided.
* Use this to call any third-party APIs from loaders or actions.
* By default, it uses the `CacheShort` strategy.
*
*/
export function createWithCache<T = unknown>(
options: CreateWithCacheOptions,
): CreateWithCacheReturn<T> {
const {cache, waitUntil} = options;
export function createWithCache<T = unknown>({
cache,
waitUntil,
request,
}: CreateWithCacheOptions): CreateWithCacheReturn<T> {
return function withCache<T = unknown>(
cacheKey: CacheKey,
strategy: CachingStrategy,
Expand All @@ -28,7 +43,10 @@ export function createWithCache<T = unknown>(
strategy,
cacheInstance: cache,
waitUntil,
debugInfo: {},
debugInfo: {
requestId: getHeader('request-id', request),
purpose: getHeader('purpose', request),
},
});
};
}
Expand Down

0 comments on commit 945c55a

Please # to comment.