Skip to content

Commit 1c62d5b

Browse files
committed
chore: patch package
1 parent 970fec1 commit 1c62d5b

File tree

5 files changed

+263
-409
lines changed

5 files changed

+263
-409
lines changed

apps/insights/next.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const config = {
44
cacheComponents: true,
55
reactCompiler: true,
66
},
7-
8-
cacheMaxMemorySize: 200 * 1024 * 1024, // 200MB
97
reactStrictMode: true,
108

119
pageExtensions: ["ts", "tsx", "mdx"],

apps/insights/src/server/pyth.ts

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import { unstable_cache } from "next/cache";
12
import superjson from "superjson";
2-
import { z } from 'zod';
3+
import { z } from "zod";
34

5+
// Your imports
46
import { Cluster, clients, priceFeedsSchema } from "../services/pyth";
57

6-
export const getPublishersForFeed = async (
7-
cluster: Cluster,
8-
) => {
9-
"use cache";
8+
// Backing implementation: NOT exported, never called directly except by cache wrapper
9+
const _getPublishersForFeed = async (cluster: Cluster) => {
1010
const start = performance.now();
1111
const data = await clients[cluster].getData();
1212
const result: Record<string, string[]> = {};
@@ -15,56 +15,62 @@ export const getPublishersForFeed = async (
1515
result[key] = price?.priceComponents.map(({ publisher }) => publisher.toBase58()) ?? [];
1616
}
1717
const end = performance.now();
18+
1819
// eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
19-
console.log(`getPublishersForFeed: ${end - start}ms`);
20+
console.log(`getPublishersForFeed: ${end - start}ms`, result.length);
2021
return result;
2122
};
2223

23-
const getFeeds = async (cluster: Cluster) => {
24-
"use cache";
24+
// Next.js unstable_cache wrapper; cluster is used as key
25+
export const getPublishersForFeed = unstable_cache(
26+
_getPublishersForFeed,
27+
[],
28+
{ revalidate: false }
29+
);
30+
31+
const _getFeeds = async (cluster: Cluster) => {
2532
const start = performance.now();
2633
const data = await clients[cluster].getData();
27-
28-
const result = superjson.stringify(priceFeedsSchema.parse(data.symbols.filter(
29-
(symbol) =>
30-
data.productFromSymbol.get(symbol)?.display_symbol !== undefined,
31-
).map((symbol) => ({
32-
symbol,
33-
product: data.productFromSymbol.get(symbol),
34-
price: {
35-
...data.productPrice.get(symbol),
36-
priceComponents: data.productPrice.get(symbol)?.priceComponents.map(({ publisher }) => ({
37-
publisher: publisher.toBase58(),
38-
})) ?? [],
39-
},
40-
}))))
41-
const end = performance.now();
42-
// eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
43-
console.log(`getFeeds: ${end - start}ms`);
44-
return result;
45-
}
34+
const parsedData = priceFeedsSchema.parse(
35+
data.symbols
36+
.filter(
37+
(symbol) =>
38+
data.productFromSymbol.get(symbol)?.display_symbol !== undefined
39+
)
40+
.map((symbol) => ({
41+
symbol,
42+
product: data.productFromSymbol.get(symbol),
43+
price: {
44+
...data.productPrice.get(symbol),
45+
priceComponents:
46+
data.productPrice
47+
.get(symbol)
48+
?.priceComponents.map(({ publisher }) => ({
49+
publisher: publisher.toBase58(),
50+
})) ?? [],
51+
},
52+
}))
53+
)
54+
const result = superjson.stringify(
55+
parsedData
56+
);
4657

47-
export const getFeedsForPublisherCached = async (
48-
cluster: Cluster,
49-
publisher: string,
50-
) => {
51-
const start = performance.now();
52-
const rawFeeds = await getFeeds(cluster);
53-
const feeds = superjson.parse<z.infer<typeof priceFeedsSchema>>(rawFeeds);
5458
const end = performance.now();
5559
// eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
56-
console.log(`getFeedsForPublisherCached: ${end - start}ms`);
57-
return priceFeedsSchema.parse(feeds.filter(({ price }) =>
58-
price.priceComponents.some(
59-
(component) => component.publisher.toString() === publisher,
60-
),
61-
));
60+
console.log(`getFeeds: ${end - start}ms`, parsedData.length, result.length);
61+
return result;
6262
};
6363

64+
export const getFeeds = unstable_cache(
65+
_getFeeds,
66+
[],
67+
{ revalidate: false }
68+
);
69+
70+
// Now getFeedsCached simply uses the cached version
6471
export const getFeedsCached = async (cluster: Cluster) => {
65-
"use cache";
6672
const start = performance.now();
67-
const rawFeeds = await getFeeds(cluster);
73+
const rawFeeds = await getFeeds(cluster); // uses cache
6874
const end = performance.now();
6975
// eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
7076
console.log(`getFeedsCached: ${end - start}ms`);
@@ -73,9 +79,28 @@ export const getFeedsCached = async (cluster: Cluster) => {
7379

7480
export const getPublishersForFeedCached = async (cluster: Cluster, symbol: string) => {
7581
const start = performance.now();
76-
const data = await getPublishersForFeed(cluster);
82+
const data = await getPublishersForFeed(cluster); // uses cache
7783
const end = performance.now();
7884
// eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
7985
console.log(`getPublishersForFeedCached: ${end - start}ms`);
8086
return data[symbol];
81-
};
87+
};
88+
89+
export const getFeedsForPublisherCached = async (
90+
cluster: Cluster,
91+
publisher: string
92+
) => {
93+
const start = performance.now();
94+
const rawFeeds = await getFeeds(cluster); // uses cache
95+
const feeds = superjson.parse<z.infer<typeof priceFeedsSchema>>(rawFeeds);
96+
const end = performance.now();
97+
// eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
98+
console.log(`getFeedsForPublisherCached: ${end - start}ms.`);
99+
return priceFeedsSchema.parse(
100+
feeds.filter(({ price }) =>
101+
price.priceComponents.some(
102+
(component) => component.publisher.toString() === publisher
103+
)
104+
)
105+
);
106+
};

patches/[email protected]

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
diff --git a/dist/server/lib/incremental-cache/index.js b/dist/server/lib/incremental-cache/index.js
2+
index 7570bd7edb5c966884f3f7fd84db5c7bcb128c03..c13eb71ddd040581177b34d4d481543fee2e962b 100644
3+
--- a/dist/server/lib/incremental-cache/index.js
4+
+++ b/dist/server/lib/incremental-cache/index.js
5+
@@ -385,14 +385,14 @@ class IncrementalCache {
6+
}
7+
if (this.disableForTestmode || this.dev && !ctx.fetchCache) return;
8+
pathname = this._getPathname(pathname, ctx.fetchCache);
9+
- // FetchCache has upper limit of 2MB per-entry currently
10+
+ // FetchCache has upper limit of 200MB per-entry currently
11+
const itemSize = JSON.stringify(data).length;
12+
- if (ctx.fetchCache && itemSize > 2 * 1024 * 1024 && // We ignore the size limit when custom cache handler is being used, as it
13+
+ if (ctx.fetchCache && itemSize > 200 * 1024 * 1024 && // We ignore the size limit when custom cache handler is being used, as it
14+
// might not have this limit
15+
!this.hasCustomCacheHandler && // We also ignore the size limit when it's an implicit build-time-only
16+
// caching that the user isn't even aware of.
17+
!ctx.isImplicitBuildTimeCache) {
18+
- const warningText = `Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 2MB can not be cached (${itemSize} bytes)`;
19+
+ const warningText = `Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 200MB can not be cached (${itemSize} bytes)`;
20+
if (this.dev) {
21+
throw Object.defineProperty(new Error(warningText), "__NEXT_ERROR_CODE", {
22+
value: "E394",

0 commit comments

Comments
 (0)