Skip to content

Commit

Permalink
feat: add resetCache (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Jul 20, 2023
1 parent feb4223 commit 868e9b4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -4,7 +4,6 @@ Express Handlebars
A [Handlebars][] view engine for [Express][] which doesn't suck.

[![npm version][npm-badge]][npm]
[![dependency status][dep-badge]][dep-status]

**This package used to be named `express3-handlebars`. The previous `express-handlebars` package by @jneen can be found [here][jneen-exphbs].**

Expand All @@ -13,8 +12,6 @@ A [Handlebars][] view engine for [Express][] which doesn't suck.
[Handlebars]: https://github.com/handlebars-lang/handlebars.js
[npm]: https://www.npmjs.org/package/express-handlebars
[npm-badge]: https://img.shields.io/npm/v/express-handlebars.svg?style=flat-square
[dep-status]: https://david-dm.org/express-handlebars/express-handlebars
[dep-badge]: https://img.shields.io/david/express-handlebars/express-handlebars.svg?style=flat-square
[jneen-exphbs]: https://github.com/jneen/express-handlebars


Expand Down Expand Up @@ -477,6 +474,13 @@ Use `options.precompiled` to receive precompiled Handlebars templates — this i

* `[precompiled=false]`: Whether precompiled templates should be provided, instead of a compiled Handlebars template function.

#### `resetCache([filePathsOrFilter])`
Reset template cache. The cache can be partially reset by providing a filter argument. If no argument is given the whole cache will be reset.

**Parameters:**

* `[filePathsOrFilter]`: Optional filter to reset part of the cache. This can be a file path, an array of file paths, or a filter function based on file path.

#### `render(filePath, context, [options])`
Renders the template at the specified `filePath` with the `context`, using this instance's `helpers` and partials by default, and returns a Promise for the resulting string.

Expand Down
18 changes: 18 additions & 0 deletions lib/express-handlebars.ts
Expand Up @@ -288,6 +288,24 @@ export default class ExpressHandlebars {
return promise;
}

resetCache (filePathsOrFilter?: string | string[] | ((template: string) => boolean)) {
let filePaths: string[] = [];

if (typeof filePathsOrFilter === "undefined") {
filePaths = Object.keys(this._fsCache);
} else if (typeof filePathsOrFilter === "string") {
filePaths = [filePathsOrFilter];
} else if (typeof filePathsOrFilter === "function") {
filePaths = Object.keys(this._fsCache).filter(filePathsOrFilter);
} else if (Array.isArray(filePathsOrFilter)) {
filePaths = filePathsOrFilter;
}

for (const filePath of filePaths) {
delete this._fsCache[filePath];
}
}

// -- Protected Hooks ----------------------------------------------------------

protected _compileTemplate (template: string, options: RuntimeOptions = {}): HandlebarsTemplateDelegate {
Expand Down
74 changes: 74 additions & 0 deletions spec/express-handlebars.test.ts
Expand Up @@ -566,6 +566,80 @@ describe("express-handlebars", () => {
});
});

describe("resetCache", () => {
test("should reset all cache", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const template = fixturePath("templates/template.handlebars");
await exphbs.getTemplates(dirPath);
expect(exphbs._fsCache[template]).toBeDefined();
exphbs.resetCache();
expect(exphbs._fsCache).toEqual({});
});

test("should reset all cache with undefined", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const template = fixturePath("templates/template.handlebars");
await exphbs.getTemplates(dirPath);
expect(exphbs._fsCache[template]).toBeDefined();
let undef: undefined;
exphbs.resetCache(undef);
expect(exphbs._fsCache).toEqual({});
});

test("should reset cached file path", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const template = fixturePath("templates/template.handlebars");
await exphbs.getTemplates(dirPath);
expect(Object.keys(exphbs._fsCache).length).toEqual(4);
expect(exphbs._fsCache[template]).toBeDefined();
exphbs.resetCache(template);
expect(Object.keys(exphbs._fsCache).length).toEqual(3);
expect(exphbs._fsCache[template]).toBeUndefined();
});

test("should reset cached file paths", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const template = fixturePath("templates/template.handlebars");
const templateLatin1 = fixturePath("templates/template-latin1.handlebars");
await exphbs.getTemplates(dirPath);
expect(Object.keys(exphbs._fsCache).length).toEqual(4);
expect(exphbs._fsCache[template]).toBeDefined();
expect(exphbs._fsCache[templateLatin1]).toBeDefined();
exphbs.resetCache([template, templateLatin1]);
expect(Object.keys(exphbs._fsCache).length).toEqual(2);
expect(exphbs._fsCache[template]).toBeUndefined();
expect(exphbs._fsCache[templateLatin1]).toBeUndefined();
});

test("should reset cached file based on filter", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const templateLatin1 = fixturePath("templates/template-latin1.handlebars");
await exphbs.getTemplates(dirPath);
expect(Object.keys(exphbs._fsCache).length).toEqual(4);
expect(exphbs._fsCache[templateLatin1]).toBeDefined();
exphbs.resetCache((f) => f.includes("latin1"));
expect(Object.keys(exphbs._fsCache).length).toEqual(3);
expect(exphbs._fsCache[templateLatin1]).toBeUndefined();
});

test("should not error on invalid file path", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const template = fixturePath("templates/invalid.handlebars");
await exphbs.getTemplates(dirPath);
expect(Object.keys(exphbs._fsCache).length).toEqual(4);
expect(exphbs._fsCache[template]).toBeUndefined();
exphbs.resetCache(template);
expect(Object.keys(exphbs._fsCache).length).toEqual(4);
expect(exphbs._fsCache[template]).toBeUndefined();
});
});

describe("hooks", () => {
describe("_compileTemplate", () => {
test("should call template with context and options", () => {
Expand Down

0 comments on commit 868e9b4

Please sign in to comment.