Skip to content

Commit

Permalink
Merge pull request #9 from pavlikm/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
pavlikm authored Nov 8, 2021
2 parents c5200bb + e977a77 commit 994a309
Show file tree
Hide file tree
Showing 34 changed files with 1,802 additions and 23 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@

**cache-html-part** saves your bandwidth and reduce load speed with caching some parts of rendered html.

**This package is experimental**
## install
```javascript
npm install cache-html-part
```
As parameter to this middleware enters filter function for elements, which should be cached and count, how many rendering times this element should be rendered before apply cache.
```javascript
function (filterFunction: Function = (e: HTMLElement) => false, renderedCount: number = 1)
```
For example, for caching all divs bigger than 200 characters:
````javascript
(e) => {
return (e.toString().length >= 200 && e.tagName == 'div')
}
````

## use
Use cache-html-part as express middleware:
Use cache-html-part as express middleware that will cache footer:
```javascript
const express = require('express');
const http = require('http');
Expand All @@ -18,7 +29,9 @@ const server = http.createServer(app);
const fs = require('fs');
const cacheHtmlPart = require('cache-html-part');
app.use(cacheHtmlPart);
app.use(cacheHtmlPart((e) => {
return (e.classNames.toString().toLowerCase().indexOf('footer') >= 0)
}));
//send html from file
app.get("/test", (req, res) => {
Expand All @@ -36,21 +49,7 @@ app.get('/twig', function(req, res){
server.listen(8080, () => {});
```
and mark any static part of your page with html comment `<!-- static -->` and `<!-- static-end -->`:
```html
<html>
<head></head>
<body>

<div>This div will be transfered from server again and again and again... even if is still the same...</div>

<!-- static -->
<div>But this div will be transfered only once! Next time browser will render it by self.</div>
<!-- static-end -->

</body>
</html>
```

## cache-html-part and SEO
**cache-html-part** will not affect any SEO bots and crawlers. It use html comments which are "invisible" for bots.
3 changes: 3 additions & 0 deletions lib/analyze.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Record } from "./record";
export declare function analyze(html: string, filterFunction: Function | undefined, records: Record[]): void;
//# sourceMappingURL=analyze.d.ts.map
1 change: 1 addition & 0 deletions lib/analyze.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions lib/analyze.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/analyze.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/apply.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Record } from "./record";
export declare function apply(html: string, records: Record[], renderedCount: number): string;
//# sourceMappingURL=apply.d.ts.map
1 change: 1 addition & 0 deletions lib/apply.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/apply.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/apply.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/const.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare const START_TAG = "<!-- static -->";
export declare const END_TAG = "<!-- static-end -->";
//# sourceMappingURL=const.d.ts.map
1 change: 1 addition & 0 deletions lib/const.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/const.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/const.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/cookie.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import express from "express";
export declare function getCookie(req: express.Request, name: string): string[];
//# sourceMappingURL=cookie.d.ts.map
1 change: 1 addition & 0 deletions lib/cookie.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/cookie.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/cookie.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions lib/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/record.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export declare class Record {
count: number;
hash: string;
html: string;
constructor(hash: string, html: string);
}
//# sourceMappingURL=record.d.ts.map
1 change: 1 addition & 0 deletions lib/record.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/record.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/record.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 994a309

Please sign in to comment.