Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Enabling caching per manifest path
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandcote committed Mar 21, 2023
1 parent e18d7b3 commit 54f1998
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-dogs-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/sewing-kit-koa': minor
---

Enabling manifest caching per manifest path.
8 changes: 4 additions & 4 deletions packages/sewing-kit-koa/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Assets {
assetPrefix: string;
userAgent?: string;
private manifests: Manifests;
private resolvedManifestEntry?: Manifest;
private resolvedManifestEntry: Map<string, Manifest> = new Map();

constructor({assetPrefix, userAgent, manifestPath, caching = true}: Options) {
this.assetPrefix = assetPrefix;
Expand Down Expand Up @@ -83,16 +83,16 @@ export default class Assets {
private async getResolvedManifestEntry(
locale: string | undefined,
): Promise<Manifest> {
if (this.resolvedManifestEntry) {
return this.resolvedManifestEntry;
if (this.resolvedManifestEntry.has(this.manifests.path)) {
return this.resolvedManifestEntry.get(this.manifests.path)!;
}

const {userAgent} = this;
const manifest = await this.manifests.resolve(userAgent, {
locale,
});

this.resolvedManifestEntry = manifest;
this.resolvedManifestEntry.set(this.manifests.path, manifest);
return manifest;
}

Expand Down
17 changes: 11 additions & 6 deletions packages/sewing-kit-koa/src/manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ function readGzipped(resolvedPath: string) {
.then((unzippedStr) => JSON.parse(unzippedStr.toString()));
}

let loadPromise: Promise<ReturnType<typeof groupManifests>> | null = null;
const loadPromise: Map<
string,
Promise<ReturnType<typeof groupManifests>>
> = new Map();
function loadConsolidatedManifest(manifestPath: string, caching: boolean) {
if (loadPromise && caching) {
return loadPromise;
if (loadPromise.has(manifestPath) && caching) {
return loadPromise.get(manifestPath)!;
}

const resolvedPath = join(appRoot.path, manifestPath);
const resolvedZippedPath = `${resolvedPath}.gz`;
loadPromise = pathExists(resolvedZippedPath)
const promise = pathExists(resolvedZippedPath)
.then((gzippedVersionExists) => {
return gzippedVersionExists
? readGzipped(resolvedZippedPath)
Expand All @@ -95,11 +98,13 @@ function loadConsolidatedManifest(manifestPath: string, caching: boolean) {
return groupManifests(manifests.map(backfillIdentity));
});

return loadPromise;
loadPromise.set(manifestPath, promise);

return promise;
}

export function internalOnlyClearCache() {
loadPromise = null;
loadPromise.clear();
}

function find(manifests: Manifest[] | undefined, userAgent) {
Expand Down
38 changes: 38 additions & 0 deletions packages/sewing-kit-koa/src/tests/manifests-read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,42 @@ describe('Manifests read', () => {
);
expect(readJson).toHaveBeenCalledWith(join(appRoot.path, manifestPath));
});

it('reads two different manifests', async () => {
const firstManifestPath = 'path/to/first-manifest';
const firstManifests = new Manifests(firstManifestPath);
const secondManifestPath = 'path/to/second-manifest';
const secondManifests = new Manifests(secondManifestPath);

await firstManifests.resolve('Chrome 73');

expect(pathExists).toHaveBeenCalledWith(
join(appRoot.path, `${firstManifestPath}.gz`),
);
expect(readJson).toHaveBeenCalledWith(
join(appRoot.path, firstManifestPath),
);

await secondManifests.resolve('Chrome 73');

expect(pathExists).toHaveBeenCalledWith(
join(appRoot.path, `${firstManifestPath}.gz`),
);
expect(readJson).toHaveBeenCalledWith(
join(appRoot.path, firstManifestPath),
);
});

it('does not reads twice the same manifest', async () => {
const manifestPath = 'path/to/first-manifest';
const manifests = new Manifests(manifestPath);

await manifests.resolve('Chrome 73');
await manifests.resolve('Chrome 73');

expect(readJson).toHaveBeenNthCalledWith(
1,
join(appRoot.path, manifestPath),
);
});
});

0 comments on commit 54f1998

Please sign in to comment.