Skip to content

Commit ad0375f

Browse files
committed
cleaned up resource.js
1 parent c271c55 commit ad0375f

File tree

1 file changed

+18
-59
lines changed

1 file changed

+18
-59
lines changed

resources.js

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export class ExamplePageCacheResource extends Resource {
6161

6262
// ***********EXAMPLE IMPLEMENTATION OF HTML-ONLY CACHE RETURN************
6363

64-
64+
// This is usefule if you are caching pages at scale and want to reduce the response payload
65+
6566

6667
/*
6768
@@ -89,46 +90,8 @@ export class ExamplePageCache extends PageCache {
8990

9091
/********************************************************************************************************** */
9192

92-
/*
93-
94-
A class used to update the cache for a specific page
95-
export class PageCacheResource extends PageCache {
9693

97-
// Invalidate the cache if necessary
98-
invalidate() {
99-
super.invalidate();
100-
}
101-
102-
// Fetch the page and update the cache
103-
async get() {
104-
try {
105-
const pageURL = ``; // URL of the page to cache
106-
const cacheId = `pageURL/ + ${this.getId()}`; // Get the ID of the page to cache (to be implemented by the user)
107-
108-
// Fetch the page content
109-
const response = await fetch(pageURL);
110-
111-
if (!response.ok) {
112-
throw new Error(`Failed to fetch the page: ${response.status}`);
113-
}
114-
115-
// Convert the HTML content to string
116-
const htmlContent = await response.text();
117-
118-
// Return the cached data in a structured format
119-
return { id: cacheId, cachedData: htmlContent };
120-
121-
} catch (e) {
122-
console.log("CACHING ERROR:", e);
123-
}
124-
}
125-
}
126-
127-
// Define PageCacheResource as the cache source
128-
PageCache.sourcedFrom(PageCacheResource);
129-
*/
130-
131-
/*
94+
/*
13295
Usage Instructions:
13396
13497
1. define the cache IDs.
@@ -140,28 +103,28 @@ export class PageCacheResource extends PageCache {
140103
- The returned `cachedData` will contain the HTML as a string.
141104
*/
142105

143-
144-
145-
146-
147-
148-
149-
150-
151-
// The code below is the a class that is used to update the cache
106+
// A class used to update the cache for a specific page
152107
export class PageCacheResource extends PageCache {
153108

109+
// Invalidate the cache if necessary
154110
invalidate() {
155111
super.invalidate();
156112
}
157113

158-
114+
// Fetch the page and update the cache
159115
async get() {
160116
try{
161117
const pageURL = "" // URL of the page to cache
162-
const cacheId = "" // id of the page to cache
163-
const response = (await fetch(pageURL)); // fetch the page and get the html content to cache
164-
const convertHtmlTextToStr = await response.text(); // convert the html content to string
118+
const cacheId = "" // Get the ID of the page to cache (to be implemented by the user)
119+
const response = (await fetch(pageURL)); // Fetch the page content
120+
121+
if (!response.ok) {
122+
throw new Error(`Failed to fetch the page: ${response.status}`);
123+
}
124+
// Convert the HTML content to string
125+
const convertHtmlTextToStr = await response.text();
126+
127+
//Return the cached data in a structured format
165128
return { id: cacheId, cachedData: convertHtmlTextToStr };
166129

167130
}catch(e){
@@ -171,10 +134,6 @@ export class PageCacheResource extends PageCache {
171134
}
172135
}
173136

174-
PageCache.sourcedFrom(PageCacheResource);
175-
176-
// you can access the cache from the browser using the following URL: http://localhost:9926/MyCache/testPage
177-
178-
//http://localhost:9926/PageCache/<cacheId>
179-
137+
// you can access the cache from the browser using the following URL: http://localhost:9926/PageCache/<cacheId>
180138

139+
PageCache.sourcedFrom(PageCacheResource);

0 commit comments

Comments
 (0)