Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(isr): add compression (#1755) #1768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 42 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ba1cbbe
chore(isr): update CacheHandler and IsrServiceInterface typings
maxisam Aug 20, 2024
f7755ac
refactor: fix eslint issues
maxisam Aug 20, 2024
0af4f56
fix(isr): filter urlsOnHold by cacheKey instead of url
maxisam Aug 20, 2024
244bff7
feat: add allowed query params options #1743
maxisam Aug 22, 2024
50643b3
feat: handle query string for filesystem cache #1690
maxisam Aug 22, 2024
9dc5a70
fix(isr): in memory cache handler should use extends #1736
maxisam Aug 22, 2024
76aa916
Merge branch 'fix/instanceof' into dev
maxisam Aug 23, 2024
fcb59b2
Merge branch 'refactor/es-lint' into dev
maxisam Aug 23, 2024
cc19ee3
Merge branch 'feat/allowed-query-params' into dev
maxisam Aug 23, 2024
c37269b
refactor(isr): rename CacheRegeneration to CacheGeneration
maxisam Aug 23, 2024
039f09c
refactor(isr): rename CacheRegeneration to CacheGeneration
maxisam Aug 23, 2024
c95d1c6
fix(isr): handle modifyGeneratedHtml behavior consistantly #1758
maxisam Aug 29, 2024
e7ed6a5
Merge branch 'main' into dev
maxisam Aug 29, 2024
cb0261a
refactor(isr): use modifyGeneratedHtml instead
maxisam Aug 29, 2024
2dd9551
feat(isr): update the example to show modifyGeneratedHtml usage
maxisam Aug 29, 2024
9f4f0cd
Merge branch 'main' into feat/callback-cachemsg
maxisam Aug 29, 2024
5b7603e
Merge pull request #2 from maxisam/feat/consolidate-cache-generation
maxisam Aug 29, 2024
2c88c4f
feat(isr): add non-blocking-render option
maxisam Aug 29, 2024
dcc61f9
feat(isr): add background revalidation option
maxisam Aug 29, 2024
fe0bac2
feat(isr): enable background revalidation and non-blocking render
maxisam Aug 29, 2024
a88b5ca
Merge pull request #3 from maxisam/feat/response-first
maxisam Aug 29, 2024
2ae8857
feat(isr): add compression #1755
maxisam Aug 30, 2024
85b0dc3
chore(isr): rename HTML compression method
maxisam Aug 30, 2024
bdcc48b
Merge pull request #4 from maxisam/feat/compression
maxisam Aug 30, 2024
cd5c42d
Merge pull request #5 from maxisam/feat/compression
maxisam Aug 30, 2024
68f1d59
Merge branch 'main' into dev
maxisam Sep 3, 2024
a2192db
fix: merging issue
maxisam Sep 3, 2024
9ef7463
feat(isr): add compression
maxisam Sep 3, 2024
bb74688
fix(isr): fix eslint issue
maxisam Sep 3, 2024
d32eaf5
Merge pull request #8 from maxisam/chore/lint
maxisam Sep 5, 2024
0960536
Merge branch 'dev' into compress-new
maxisam Sep 5, 2024
55a7609
Merge branch 'main' into compress-new
maxisam Sep 18, 2024
d910520
chore: update yarn lock
maxisam Sep 18, 2024
b6f90a1
fix: format
maxisam Sep 19, 2024
ce45c32
feat: let cache handler handle cache html either buffer or string
maxisam Oct 7, 2024
65b0381
docs: how to use buffer in redis and compressHtml callback
maxisam Oct 7, 2024
7e8512b
docs(isr): add compression support for caching
maxisam Oct 16, 2024
28b86d2
Merge branch 'main' into compress-new
maxisam Nov 24, 2024
6c7670b
Merge branch 'main' into compress-new
maxisam Nov 24, 2024
f7fb152
refactor(isr): standardize terminology from 'route' to 'cacheKey'
maxisam Nov 27, 2024
bd14b62
docs(isr): update CacheHandler API doc
maxisam Nov 27, 2024
e08dd51
docs: revert
maxisam Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 2 apps/ssr-isr/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function app(): express.Express {
browserDistFolder,
bootstrap,
commonEngine,
backgroundRevalidation: true, // will revalidate in background and serve the cache page first
nonBlockingRender: true, // will serve page first and store in cache in background
modifyGeneratedHtml: customModifyGeneratedHtml,
// cache: fsCacheHandler,
});
Expand Down
11 changes: 11 additions & 0 deletions 11 libs/isr/models/src/isr-handler-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ export interface ISRHandlerConfig {
* which only add commented text to the html to indicate when it was generated with very low performance impact
*/
modifyGeneratedHtml?: modifyHtmlCallbackFn;

/**
* If set to true, the server will not wait for storing the rendered page to the cache storage first and will return the rendered HTML as soon as possible.
* This can avoid client-side waiting times if the remote cache storage is down.
*/
nonBlockingRender?: boolean;

/**
* If set to true, the server will provide the cached HTML as soon as possible and will revalidate the cache in the background.
*/
backgroundRevalidation?: boolean;
}

export interface ServeFromCacheConfig {
Expand Down
15 changes: 11 additions & 4 deletions 15 libs/isr/server/src/cache-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,17 @@ export class CacheGeneration {
return { html: finalHtml };
}
// add the regenerated page to cache
await this.cache.add(cacheKey, finalHtml, {
revalidate,
buildId: this.isrConfig.buildId,
});
if (this.isrConfig.nonBlockingRender) {
this.cache.add(cacheKey, finalHtml, {
revalidate,
buildId: this.isrConfig.buildId,
});
} else {
await this.cache.add(cacheKey, finalHtml, {
revalidate,
buildId: this.isrConfig.buildId,
});
}
if (mode === 'regenerate') {
// remove from urlsOnHold because we are done
this.urlsOnHold = this.urlsOnHold.filter((x) => x !== cacheKey);
Expand Down
46 changes: 29 additions & 17 deletions 46 libs/isr/server/src/isr-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,34 +187,46 @@ export class ISRHandler {
return;
}

// Apply the callback if given
let finalHtml = html;
if (config?.modifyCachedHtml) {
const timeStart = performance.now();
finalHtml = config.modifyCachedHtml(req, html);
const totalTime = (performance.now() - timeStart).toFixed(2);
finalHtml += `<!--\nℹ️ ISR: This cachedHtml has been modified with modifyCachedHtml()\n❗️
This resulted into more ${totalTime}ms of processing time.\n-->`;
}

// Cache exists. Send it.
this.logger.log(`Page was retrieved from cache: `, cacheKey);
let finalHtml = html;

// if the cache is expired, we will regenerate it
if (cacheConfig.revalidate && cacheConfig.revalidate > 0) {
const lastCacheDateDiff = (Date.now() - createdAt) / 1000; // in seconds

if (lastCacheDateDiff > cacheConfig.revalidate) {
// regenerate the page without awaiting, so the user gets the cached page immediately
this.cacheGeneration.generateWithCacheKey(
req,
res,
cacheKey,
config?.providers,
'regenerate',
);
if (this.isrConfig.backgroundRevalidation) {
this.cacheGeneration.generateWithCacheKey(
req,
res,
cacheKey,
config?.providers,
'regenerate',
);
} else {
const result = await this.cacheGeneration.generateWithCacheKey(
req,
res,
cacheKey,
config?.providers,
'regenerate',
);
if (result?.html) {
finalHtml = result.html;
}
}
}
}
// Apply the callback if given
if (config?.modifyCachedHtml) {
const timeStart = performance.now();
finalHtml = config.modifyCachedHtml(req, finalHtml);
const totalTime = (performance.now() - timeStart).toFixed(2);
finalHtml += `<!--\nℹ️ ISR: This cachedHtml has been modified with modifyCachedHtml()\n❗️
This resulted into more ${totalTime}ms of processing time.\n-->`;
}
return res.send(finalHtml);
} catch (error) {
// Cache does not exist. Serve user using SSR
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.