Skip to content

Commit

Permalink
Merge pull request #12 from mocoso/only-register-partials-at-initiali…
Browse files Browse the repository at this point in the history
…sation-unless-requested-otherwise

Improve performance by caching partials
  • Loading branch information
irustm authored May 6, 2021
2 parents a5f2efd + 5b83367 commit 0e78ddd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const DEFAULT_HANDLEBARS_CONFIG: HandlebarsConfig = {
extname: '.hbs',
layoutsDir: 'layouts/',
partialsDir: 'partials/',
cachePartials: true,
defaultLayout: 'main',
helpers: undefined,
compilerOptions: undefined,
Expand All @@ -32,3 +33,13 @@ const DEFAULT_HANDLEBARS_CONFIG: HandlebarsConfig = {
// Then render page to string
const result: string = await handle.renderView('index', { name: 'Alosaur' });
```

#### Rendering in development mode

By default partials are registered (and so cached) the first time you call
`renderView`. However, in development, it may be better to re-register them
every time you render a view so that the rendering reflects the latest changes
you have made to your partials.

You can ensure this happens by setting `cachePartials` to be false in your
configuration.
44 changes: 27 additions & 17 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface HandlebarsConfig {
extname: string;
layoutsDir: string;
partialsDir: string;
cachePartials?: boolean;
defaultLayout: string;
// deno-lint-ignore no-explicit-any
helpers: any;
Expand All @@ -26,6 +27,7 @@ const DEFAULT_HANDLEBARS_CONFIG: HandlebarsConfig = {
extname: ".hbs",
layoutsDir: "layouts/",
partialsDir: "partials/",
cachePartials: true,
defaultLayout: "main",
helpers: undefined,
compilerOptions: undefined,
Expand All @@ -36,6 +38,8 @@ function getNormalizePath(path: string) {
}

export class Handlebars {
#havePartialsBeenRegistered = false;

constructor(private config: HandlebarsConfig = DEFAULT_HANDLEBARS_CONFIG) {
this.config = { ...DEFAULT_HANDLEBARS_CONFIG, ...config };

Expand Down Expand Up @@ -72,10 +76,9 @@ export class Handlebars {

const config: HandlebarsConfig = this.config as HandlebarsConfig;

const partialsPathes = await this.getTemplatesPath(
join(config.baseDir, config.partialsDir),
);
partialsPathes && (await this.registerPartials(partialsPathes));
if (!config.cachePartials || !this.#havePartialsBeenRegistered) {
await this.registerPartials();
}

const path = join(config.baseDir, view + config.extname);
const body: string = await this.render(path, context);
Expand Down Expand Up @@ -116,20 +119,27 @@ export class Handlebars {
/**
* Processes on register partials
*/
private async registerPartials(pathes: string[]) {
for (const path of pathes) {
const templateName: string = path
.replace(
getNormalizePath(this.config.baseDir) + "/" +
this.config!.partialsDir,
"",
)
.replace(new RegExp(this.config!.extname + "$"), "");
const source: string = new TextDecoder().decode(await readFile(path));

// deno-lint-ignore no-explicit-any
(HandlebarsJS as any).registerPartial(templateName, source);
private async registerPartials() {
const paths = await this.getTemplatesPath(
join(this.config.baseDir, this.config.partialsDir),
);
if (paths) {
for (const path of paths) {
const templateName: string = path
.replace(
getNormalizePath(this.config.baseDir) + "/" +
this.config!.partialsDir,
"",
)
.replace(new RegExp(this.config!.extname + "$"), "");
const source: string = new TextDecoder().decode(await readFile(path));

// deno-lint-ignore no-explicit-any
(HandlebarsJS as any).registerPartial(templateName, source);
}
}

this.#havePartialsBeenRegistered = true;
}

/**
Expand Down

0 comments on commit 0e78ddd

Please sign in to comment.