11// import PageCache table from schemas.graphql
22const { 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
5149export 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}
0 commit comments