Skip to content

Commit

Permalink
feat(core): ✨ add setting to cache rendered HTML
Browse files Browse the repository at this point in the history
This close #713.
  • Loading branch information
xavierfoucrier committed Aug 1, 2023
2 parents 4c97b59 + b745e97 commit 7075b1f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/core/__tests__/core/core.init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,11 @@ it('has other options', () => {
expect(requestError()).toBeFalsy();
expect(barba.wrapper).toBe(wrapper);
});

it('cache the first rendered page', () => {
barba.init({
cacheFirstPage: true,
});

expect(barba.cache.has('http://localhost/')).toBeTruthy();
});
14 changes: 10 additions & 4 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class Core {
*/
public timeout: number;
public cacheIgnore: IgnoreOption;
public cacheFirstPage: boolean;
public prefetchIgnore: IgnoreOption;
public preventRunning: boolean;
/**
Expand Down Expand Up @@ -140,6 +141,7 @@ export class Core {
* - schema: [[SchemaAttribute]]
* - timeout: `2e3`
* - cacheIgnore: `false`
* - cacheFirstPage: `false`
* - prefetchIgnore: `false`
* - preventRunning: `false`
* - prevent: `null`,
Expand All @@ -154,6 +156,7 @@ export class Core {
requestError,
timeout = 2e3,
cacheIgnore = false,
cacheFirstPage = false,
prefetchIgnore = false,
/* istanbul ignore next */
preventRunning = false,
Expand All @@ -178,6 +181,7 @@ export class Core {
this._requestCustomError = requestError;
this.timeout = timeout;
this.cacheIgnore = cacheIgnore;
this.cacheFirstPage = cacheFirstPage;
this.prefetchIgnore = prefetchIgnore;
this.preventRunning = preventRunning;

Expand Down Expand Up @@ -216,10 +220,12 @@ export class Core {
this.history.init(current.url.href, current.namespace);

// 6. Add to cache
this.cache.set(current.url.href, Promise.resolve({
html: current.html,
url: current.url,
}), 'init', 'fulfilled');
if (cacheFirstPage) {
this.cache.set(current.url.href, Promise.resolve({
html: current.html,
url: current.url,
}), 'init', 'fulfilled');
}

// 7. Bind context
this._onLinkEnter = this._onLinkEnter.bind(this);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/defs/barba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface IBarbaOptions {
requestError?: RequestCustomError | undefined;
/** Disable cache or ignore some routes. */
cacheIgnore?: IgnoreOption;
/** Disable cache on the first rendered page. */
cacheFirstPage?: boolean;
/** Disable prefetch or ignore routes. */
prefetchIgnore?: IgnoreOption;
/** Custom prevent check. */
Expand Down

0 comments on commit 7075b1f

Please sign in to comment.