Skip to content

Commit 080b10f

Browse files
committed
added description on how to store cached data as binary and changed error handling to throw
1 parent 16e860d commit 080b10f

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

resources.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// import PageCache table from schemas.graphql
22
const { PageCache } = tables;
33

4-
54
// ***********EXAMPLE IMPLEMENTATION OF HTML-ONLY CACHE RETURN************
65

76
// This is usefule if you are caching pages at scale and want to reduce the response payload
@@ -46,7 +45,6 @@ export class ExamplePageCache extends PageCache {
4645
- The returned `cachedData` will contain the HTML as a string.
4746
*/
4847

49-
5048
// A class used to update the cache for a specific page
5149
export class PageCacheResource extends PageCache {
5250
// Invalidate the cache if necessary
@@ -58,16 +56,28 @@ export class PageCacheResource extends PageCache {
5856
async get() {
5957
try {
6058
const pageURL = "https://www.google.com/"; // URL of the page to cache
61-
const cacheId = ""; // the ID of the page to cache (example: https://www.birkenstock.com/us/men/ or /us/men/)
59+
60+
const cacheId = `/examplePage/{this.getId}`; // the ID of the page to cache (example: https://www.birkenstock.com/us/men/ or /us/men/)
61+
6262
const response = await fetch(pageURL); // Fetch the page content
6363

64-
// Convert the HTML content to string(Use response.text()) default response is in binary
65-
const convertHtmlTextToStr = await response.binary();
64+
/**
65+
* To save response as binary data, use response.arrayBuffer() instead of response.text()
66+
*
67+
* Example:
68+
* const convertHtmlToBiuinary = await response.arrayBuffer()
69+
* const byteArray = new Uint8Array(convertHtmlToBiuinary);
70+
* return { id: cacheId, cachedData: byteArray };
71+
* Set cachedData type in schemas.graphql to Bytes
72+
*/
73+
74+
//convert html to string
75+
const convertHtmlTextToStr = await response.text();
6676

6777
//Return the cached data in a structured format
6878
return { id: cacheId, cachedData: convertHtmlTextToStr };
6979
} catch (e) {
70-
console.log("CACHING ERROR", e);
80+
throw new Error(`Error fetching cached data: ${e.message}`);
7181
}
7282
}
7383
}

schema.graphql

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
## Here we can define any tables in our database. This example shows how we define a type as a table using
22
## the type name as the table name and specifying it is an "export" available in the REST and other external protocols.
33

4-
5-
64
# This will be the table where users can store their pages they would like to cache
75
# Keep this table simple
86
type PageCache @table(expiration: 3600) @export {
9-
cacheControl: String
10-
# Stores the Cache-Control headers for the page, which dictate how caching should be handled (e.g., max-age, no-cache).
7+
cacheControl: String
8+
# Stores the Cache-Control headers for the page, which dictate how caching should be handled (e.g., max-age, no-cache).
119

12-
id: ID @primaryKey
13-
# The primary key representing the URL path of the cached webpage.
14-
# Example: For the URL https://www.birkenstock.com/us/men/, the `id` should be "/us/men/".
15-
# This allows us to uniquely identify cached pages based on their path.
10+
id: ID @primaryKey
11+
# The primary key representing the URL path of the cached webpage.
12+
# Example: For the URL https://www.birkenstock.com/us/men/, the `id` should be "/us/men/".
13+
# This allows us to uniquely identify cached pages based on their path.
1614

17-
cachedData: String
18-
# Stores the full HTML of the cached webpage as a string.
19-
# This is the rendered content that will be served from cache on subsequent requests.
20-
}
15+
cachedData: String # Set type to Bytes to save as binary
2116

17+
# Stores the full HTML of the cached webpage as a string.
18+
# This is the rendered content that will be served from cache on subsequent requests.
19+
}

0 commit comments

Comments
 (0)