Skip to content

Commit

Permalink
Background page caching
Browse files Browse the repository at this point in the history
  • Loading branch information
CMEONE committed Mar 4, 2021
1 parent a77be68 commit 7266d89
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tApp.configure({
caching: {
maxBytes: 5 * 1000 * 1000, // around 5MB in bytes (5 MB * 1000 KB/MB * 1000 bytes/KB)
updateCache: 5 * 60 * 1000, // updates the cache every 5 minutes in milliseconds (5 minutes * 60 seconds/minute * 1000 seconds/millisecond)
backgroundRoutes: ["#/", "#/about", "#/text", "#/404", "#/403"]
backgroundPages: ["./views/index.html", "./views/about.html"]
}
});

Expand Down
2 changes: 1 addition & 1 deletion example/views/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ <h1>About</h1>
<li>The navbar is not changed between pages. That means that while the rest of the page is loading, the navbar stays persistent and doesn't disappear, just like any normal app navbar. This persistence is not just limited to the navbar, anything outside the main changing view (like a persistent header or footer) can also stay across pages.</li>
<li>You can serve custom 403 forbidden and 404 not found error messages and pages right from your client, since all routing is done in the client.</li>
<li>Caching allows your tApp to be blazingly fast. You can set custom cache size and time limits in your code, and tApp will save loaded pages in the cache so that they load instantly the next time the user navigates to that section of the tApp.</li>
<li>Routes can be specified to automatically load and cache asynchronously in the background when the user first loads your tApp, making the next page loads instantaneous. This background loading functionality is not fully implemented but will be integrated in a later release of the library.</li>
<li>Routes can be specified to automatically load and cache asynchronously in the background when the user first loads your tApp, making the next page loads instantaneous.</li>
<li>tApps can work offline, just like normal apps. Since all server-like routing and computation can be handled in the client through tApp, users can browse the tApp offline. This offline functionality is not fully implemented but will be integrated in a later release of the library.</li>
</ul>
25 changes: 20 additions & 5 deletions tApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class tApp {
static cache = {};
static cacheSize = 0;
static get version() {
return "v0.2.1";
return "v0.3.0";
}
static configure(params) {
if(params == null) {
Expand Down Expand Up @@ -48,10 +48,10 @@ class tApp {
error: "tAppError: Invalid configure parameter, caching is not of type Object."
}
}
if(params.caching.backgroundRoutes != null && !(params.caching.backgroundRoutes instanceof Array)) {
if(params.caching.backgroundPages != null && !(params.caching.backgroundPages instanceof Array)) {
return {
valid: false,
error: "tAppError: Invalid configure parameter, caching.backgroundRoutes is not of type Array."
error: "tAppError: Invalid configure parameter, caching.backgroundPages is not of type Array."
}
}
if(params.caching != null && params.caching.maxBytes == null) {
Expand All @@ -60,8 +60,8 @@ class tApp {
if(params.caching != null && params.caching.updateCache == null) {
params.caching.updateCache = Infinity;
}
if(params.caching != null && params.caching.backgroundRoutes == null) {
params.caching.backgroundRoutes = [];
if(params.caching != null && params.caching.backgroundPages == null) {
params.caching.backgroundPages = [];
}
return {
valid: true,
Expand Down Expand Up @@ -153,10 +153,25 @@ class tApp {
tApp.render("");
}
}
static loadBackgroundPages() {
for(let i = 0; i < tApp.config.caching.backgroundPages.length; i++) {
tApp.get(tApp.config.caching.backgroundPages[i]);
}
if(tApp.config.caching.updateCache < 60000) {
setTimeout(() => {
tApp.loadBackgroundPages();
}, tApp.config.caching.updateCache);
} else {
setTimeout(() => {
tApp.loadBackgroundPages();
}, 60000);
}
}
static start() {
window.addEventListener("hashchange", () => {
tApp.updatePage(window.location.hash);
}, false);
tApp.updatePage(window.location.hash);
tApp.loadBackgroundPages()
}
}

0 comments on commit 7266d89

Please sign in to comment.