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 1 commit
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
Prev Previous commit
Next Next commit
docs: how to use buffer in redis and compressHtml callback
  • Loading branch information
maxisam committed Oct 7, 2024
commit 65b0381920c6bd3d0aa9f2cbf3c6ca202710dce1
93 changes: 45 additions & 48 deletions 93 apps/docs/docs/isr/cache-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,67 +71,61 @@ export class RedisCacheHandler extends CacheHandler {

this.redis = new Redis(this.options.connectionString);
console.log('RedisCacheHandler initialized 🚀');
options.keyPrefix = options.keyPrefix || 'isr';
}

add(
url: string,
html: string,
options: ISROptions = { revalidate: null }
): Promise<void> {
const htmlWithMsg = html + cacheMsg(options.revalidate);

return new Promise((resolve, reject) => {
const cacheData: CacheData = {
html: htmlWithMsg,
options,
createdAt: Date.now(),
};
const key = this.createKey(url);
this.redis.set(key, JSON.stringify(cacheData)).then(() => {
resolve();
});
add(url: string, html: string | Buffer, options: ISROptions = { revalidate: null }): Promise<void> {
const key = this.createKey(cacheKey);
const createdAt = Date.now().toString();
await this.redis.hmset(key, {
html,
revalidate: config.revalidate ? config.revalidate.toString() : '',
buildId: config.buildId || '',
createdAt,
});
}

get(url: string): Promise<CacheData> {
return new Promise((resolve, reject) => {
const key = this.createKey(url);
this.redis.get(key, (err, result) => {
if (err || result === null || result === undefined) {
reject('This url does not exist in cache!');
} else {
resolve(JSON.parse(result));
}
});
});
// in this example, it is assumed that the html is stored as a buffer, use hgetall if it is stored as a string
async get(cacheKey: string): Promise<CacheData> {
const key = this.createKey(cacheKey);
const data = await this.redis.hgetallBuffer(key);
if (Object.keys(data).length > 0) {
const revalidate = data['revalidate'] ? parseInt(data['revalidate'].toString(), 10) : null;
return {
html: data['html'],
options: {
revalidate,
buildId: data['buildId'].toString() || null,
},
createdAt: parseInt(data['createdAt'].toString(), 10),
} as CacheData;
} else {
this.logger.info(`Cache with key ${cacheKey} not found`);
throw new Error(`Cache with key ${cacheKey} not found`);
}
}

getAll(): Promise<string[]> {
console.log('getAll() is not implemented for RedisCacheHandler');
return Promise.resolve([]);
async getAll(): Promise<string[]> {
return await this.redis.keys(`${this.redisCacheOptions.keyPrefix}:*`);
}

has(url: string): Promise<boolean> {
return new Promise((resolve, reject) => {
const key = this.createKey(url);
resolve(this.redis.exists(key).then((exists) => exists === 1));
});
async has(cacheKey: string): Promise<boolean> {
const key = this.createKey(cacheKey);
return (await this.redis.exists(key)) === 1;
}

delete(url: string): Promise<boolean> {
return new Promise((resolve, reject) => {
const key = this.createKey(url);
resolve(this.redis.del(key).then((deleted) => deleted === 1));
});
async delete(cacheKey: string): Promise<boolean> {
const key = this.createKey(cacheKey);
return (await this.redis.del(key)) === 1;
}

clearCache?(): Promise<boolean> {
throw new Error('Method not implemented.');
async clearCache(): Promise<boolean> {
await this.redis.flushdb();
return true;
}

private createKey(url: string): string {
const prefix = this.options.keyPrefix || 'isr';
return `${prefix}:${url}`;
private createKey(cacheKey: string): string {
return `${this.redisCacheOptions.keyPrefix}:${cacheKey}`;
}
}

Expand Down Expand Up @@ -203,7 +197,7 @@ The `CacheHandler` abstract class has the following API:

```typescript
export abstract class CacheHandler {
abstract add(url: string, html: string, options: ISROptions): Promise<void>;
abstract add(url: string | Buffer, html: string, options: ISROptions): Promise<void>;

abstract get(url: string): Promise<CacheData>;

Expand All @@ -223,8 +217,11 @@ The `CacheData` interface is used to store the cached pages in the cache handler

```typescript
export interface CacheData {
html: string;
html: string | Buffer;
options: ISROptions;
createdAt: number;
}
```

note: The `html` field can be a string or a buffer. It depends on if you set `compressHtml` function in the `ISRHandler` options.
If it is set, the html will be compressed and stored as a buffer. If it is not set, the html will be stored as a string.
8 changes: 6 additions & 2 deletions 8 apps/docs/docs/isr/cache-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ To do that, you can use the `modifyCachedHtml` and `modifyGeneratedHtml` callbac

### modifyCachedHtml

The `modifyCachedHtml` callback is called when the html is served from cache (on every user request). It receives the request and the cached html as parameters. It should return the modified html.
The `modifyCachedHtml` callback is called when the html is served from cache (on every user request). It receives the request and the cached html as parameters. It should return the modified html. However, if compressHtml is set, this callback will not be called, since the cached html is compressed and cannot be modified.

### modifyGeneratedHtml

The `modifyGeneratedHtml` callback is called when the html is generated on the fly (before the cache is stored). It receives the request and the generated html as parameters. It should return the modified html.

### Example
#### Example

```ts
server.get(
Expand Down Expand Up @@ -55,3 +55,7 @@ server.get(
:::caution **Important:**
Use these methods with caution as the logic written can increase the processing time.
:::

### compressHtml (> v18.1.0)

A compression callback can be provided to compress the HTML before storing it in the cache. If not provided, the HTML will be stored without compression. When provided, the HTML will be compressed and stored as Buffer | string in the cache (depending on how cache handler is implemented. Default examples use Buffer). Note that this will disable the modifyCachedHtml callback, as compressed HTML cannot be modified.
4 changes: 4 additions & 0 deletions 4 libs/isr/models/src/cache-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface CacheISRConfig {
errors?: string[];
}

// html could be a string or a buffer, it is depending on if `compressHtml` is set in `ISRHandler` config.
// if `compressHtml` is set, the html will be a buffer, otherwise it will be a string
export interface CacheData {
html: string | Buffer;
options: CacheISRConfig;
Expand All @@ -29,6 +31,8 @@ export interface VariantRebuildItem {
}

export abstract class CacheHandler {
// html could be a string or a buffer, it is depending on if `compressHtml` is set in `ISRHandler` config.
// if `compressHtml` is set, the html will be a buffer, otherwise it will be a string
abstract add(
url: string,
html: string | Buffer,
Expand Down
11 changes: 6 additions & 5 deletions 11 libs/isr/models/src/isr-handler-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ export interface ISRHandlerConfig {
backgroundRevalidation?: boolean;

/**
* compression callback to compress the html before storing it in the cache.
* If not provided, the html will not be compressed.
* If provided, the html will be compressed before storing it as base64 in the cache,
* also this will disable the modifyCachedHtml callback, because html is compressed and can't be modified.
*/
* A compression callback can be provided to compress the HTML before storing it in the cache.
* If not provided, the HTML will be stored without compression.
* When provided, the HTML will be compressed and stored as Buffer | string in the cache
* (depending on how cache handler is implemented. Default examples use Buffer)
* Note that this will disable the modifyCachedHtml callback, as compressed HTML cannot be modified.
**/
compressHtml?: CompressHtmlFn;

/**
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.