Skip to content

Commit

Permalink
refactor(previews): improve performance for large repositories (#506)
Browse files Browse the repository at this point in the history
* refactor(preview): improve performance for large repositories

* refactor: use normalized typepaths filepath helper

* test(previews): update typepaths location

* docs: comment typo [skip ci]

* docs: reword comment [skip ci]
  • Loading branch information
angeloashmore authored May 16, 2022
1 parent dcc2d86 commit c934edd
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-prismic-previews/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const QUERY_PAGE_SIZE = 100;
/**
* Template used to generate a hash for a collection of type paths.
*/
export const TYPE_PATHS_BASENAME_TEMPLATE = "type-paths-store %s";
export const TYPE_PATHS_BASENAME_TEMPLATE = "type-paths___%s";

/**
* Identifier used to store plugin options on `window` to pass to other parts of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import md5 from "tiny-hashes/md5";

import { TYPE_PATHS_BASENAME_TEMPLATE } from "../constants";
import { sprintf } from "./sprintf";

Expand All @@ -8,5 +6,5 @@ export interface BuildTypePathsStoreFilenameEnv {
}

export const buildTypePathsStoreFilename = (repositoryName: string): string => {
return `${md5(sprintf(TYPE_PATHS_BASENAME_TEMPLATE, repositoryName))}.json`;
return `${sprintf(TYPE_PATHS_BASENAME_TEMPLATE, repositoryName)}.json`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import * as gatsby from "gatsby";
import * as gatsbyPrismic from "gatsby-source-prismic";
import * as path from "path";
import { createNodeHelpers } from "gatsby-node-helpers";
import md5 from "tiny-hashes/md5";

import { buildTypePathsStoreFilename } from "./lib/buildTypePathsStoreFilename";
import { serializeTypePathNodes } from "./lib/serializeTypePathsNodes";
import { sprintf } from "./lib/sprintf";

import {
TYPE_PATHS_MISSING_NODE_MSG,
WROTE_TYPE_PATHS_TO_FS_MSG,
REPORTER_TEMPLATE,
TYPE_PATHS_BASENAME_TEMPLATE,
} from "./constants";
import { PluginOptions } from "./types";

Expand Down Expand Up @@ -49,9 +48,7 @@ export const onPostBootstrap: NonNullable<

const serializedTypePaths = serializeTypePathNodes(typePathNodes);

const filename = `${md5(
sprintf(TYPE_PATHS_BASENAME_TEMPLATE, pluginOptions.repositoryName),
)}.json`;
const filename = buildTypePathsStoreFilename(pluginOptions.repositoryName);
const publicPath = path.join("public", "static", filename);

await pluginOptions.writeTypePathsToFilesystem({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import * as gatsbyPrismic from "gatsby-source-prismic";
import { resolveURL } from "./resolveURL";

export const createTypePathsMockedRequest = (
filename: string,
repositoryName: string,
typePaths: gatsbyPrismic.SerializedTypePath[],
): msw.RestHandler =>
msw.rest.get(
resolveURL(globalThis.__PATH_PREFIX__, `/static/${filename}`),
resolveURL(
globalThis.__PATH_PREFIX__,
`/static/type-paths___${repositoryName}.json`,
),
(_req, res, ctx) => res(ctx.json(typePaths)),
);
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test("saves serialized typepaths to filesystem", async (t) => {

t.is(
writeTypePathsToFilesystemCall.publicPath,
"public/static/3e66cce7662062ad5137e62e8bb62096.json",
`public/static/type-paths___${pluginOptions.repositoryName}.json`,
);

t.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ test.serial(
searchParams: { ref },
}),
createTypePathsMockedRequest(
"d26c1607b46a831c5d238303c3cbf489.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down Expand Up @@ -194,7 +194,7 @@ test("allows skipping", async (t) => {
searchParams: { ref },
}),
createTypePathsMockedRequest(
"87ec42108faaca92ab06c427cf0b3b9d.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test.serial(
searchParams: { ref },
}),
createTypePathsMockedRequest(
"fa7e36097b060b84eb14d0df1009fa58.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down Expand Up @@ -187,7 +187,7 @@ test.serial("does nothing if already bootstrapped", async (t) => {
searchParams: { ref },
}),
createTypePathsMockedRequest(
"d6c42f6728e21ab594cd600ff04e4913.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ test.serial("merges data if preview data is available", async (t) => {
searchParams: { ref },
}),
createTypePathsMockedRequest(
"a9101d270279c16322571b8448d7a329.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down Expand Up @@ -231,7 +231,7 @@ test.serial("handles custom types without a data field", async (t) => {
searchParams: { ref },
}),
createTypePathsMockedRequest(
"eac4669530f66bef76da4016f1111055.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ test.serial("merges data if preview data is available", async (t) => {
searchParams: { ref },
}),
createTypePathsMockedRequest(
"a9101d270279c16322571b8448d7a329.json",
pluginOptions.repositoryName,
runtime.typePaths,
),
);
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby-source-prismic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"fp-ts": "^2.10.5",
"gatsby-node-helpers": "^1.2.1",
"gatsby-source-filesystem": "^4.0.0",
"node-fetch": "^2.6.5",
"tiny-hashes": "^1.0.1"
"node-fetch": "^2.6.5"
},
"devDependencies": {
"@prismicio/mock": "^0.0.6",
Expand Down
14 changes: 11 additions & 3 deletions packages/gatsby-source-prismic/src/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as prismicH from "@prismicio/helpers";
import * as imgixGatsby from "@imgix/gatsby";
import * as nodeHelpers from "gatsby-node-helpers";
import { pipe } from "fp-ts/function";
import md5 from "tiny-hashes/md5";

import { SerializedTypePath, TransformFieldNameFn, TypePath } from "../types";
import { normalize } from "./normalize";
Expand All @@ -22,8 +21,17 @@ import { NormalizedDocumentValue } from "./normalizers";
import { serializeTypePaths } from "./serializeTypePaths";
import { serializePath } from "./serializePath";

const createNodeId = (input: string): string => md5(input);
const createContentDigest = <T>(input: T): string => md5(JSON.stringify(input));
// `createNodeId` would normally create a hash from its input, but we can treat
// it as an identity function since we are using it within the context of
// Prismic documents with unique IDs.
const createNodeId = (input: string): string => input;

// `createContentDigest` would normally create a hash from its input, but we
// can treat it as a stubbed function since a Gatsby node's
// `internal.contentDigest` property is an internal field. In a runtime
// preview, we don't need the digest.
const createContentDigest = <T>(_input: T): string =>
"contentDigest is not supported during previews";

export type RuntimeConfig = {
typePrefix?: string;
Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby-source-prismic/src/runtime/serializePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
import md5 from "tiny-hashes/md5";

export const serializePath = (path: string[]): string =>
process.env.NODE_ENV === "production" ? md5(path.join(".")) : path.join(".");
export const serializePath = (path: string[]): string => path.join(".");
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as ava from "ava";
import * as sinon from "sinon";
import * as crypto from "crypto";

import { UnpreparedPluginOptions } from "../../src/types";
import { md5 } from "./md5";

export const createPluginOptions = (
t: ava.ExecutionContext,
): UnpreparedPluginOptions => {
const repositoryName = crypto.createHash("md5").update(t.title).digest("hex");
const repositoryName = md5(t.title);

return {
repositoryName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as prismicT from "@prismicio/types";
import md5 from "tiny-hashes/md5";

import { md5 } from "./md5";

const createId = () => md5(Math.random().toString());

Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby-source-prismic/test/__testutils__/md5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as crypto from "crypto";

export const md5 = (input: string): string => {
return crypto.createHash("md5").update(input).digest("hex");
};
3 changes: 0 additions & 3 deletions packages/gatsby-source-prismic/types/tiny-hashes.d.ts

This file was deleted.

5 changes: 2 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9688,7 +9688,7 @@ gatsby-plugin-page-creator@^4.0.0, gatsby-plugin-page-creator@^4.1.0:
lodash "^4.17.21"

"gatsby-plugin-prismic-previews@link:packages/gatsby-plugin-prismic-previews":
version "5.2.5"
version "5.2.6"
dependencies:
"@imgix/gatsby" "^1.7.5"
"@prismicio/client" "^6.4.2"
Expand Down Expand Up @@ -9824,7 +9824,7 @@ gatsby-source-filesystem@^4.0.0:
xstate "^4.14.0"

"gatsby-source-prismic@link:packages/gatsby-source-prismic":
version "5.2.5"
version "5.2.6"
dependencies:
"@imgix/gatsby" "^1.7.5"
"@prismicio/client" "^6.4.2"
Expand All @@ -9836,7 +9836,6 @@ gatsby-source-filesystem@^4.0.0:
gatsby-node-helpers "^1.2.1"
gatsby-source-filesystem "^4.0.0"
node-fetch "^2.6.5"
tiny-hashes "^1.0.1"

gatsby-telemetry@^3.0.0, gatsby-telemetry@^3.1.0:
version "3.1.0"
Expand Down

0 comments on commit c934edd

Please sign in to comment.