fix(core): avoid hash collision when generating chunk names (#8538)

This commit is contained in:
Joshua Chen 2023-01-18 13:19:07 -05:00 committed by GitHub
parent 8714a95900
commit 00023c24b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View file

@ -56,6 +56,40 @@ describe('genChunkName', () => {
});
expect(genChunkName('d', undefined, undefined, true)).toBe('8277e091');
});
// https://github.com/facebook/docusaurus/issues/8536
it('avoids hash collisions', () => {
expect(
genChunkName(
'@site/blog/2022-11-18-bye-medium/index.mdx?truncated=true',
'content',
'blog',
false,
),
).not.toBe(
genChunkName(
'@site/blog/2019-10-05-react-nfc/index.mdx?truncated=true',
'content',
'blog',
false,
),
);
expect(
genChunkName(
'@site/blog/2022-11-18-bye-medium/index.mdx?truncated=true',
'content',
'blog',
true,
),
).not.toBe(
genChunkName(
'@site/blog/2019-10-05-react-nfc/index.mdx?truncated=true',
'content',
'blog',
true,
),
);
});
});
describe('handleDuplicateRoutes', () => {

View file

@ -51,6 +51,7 @@ function indent(str: string) {
}
const chunkNameCache = new Map<string, string>();
const chunkNameCount = new Map<string, number>();
/**
* Generates a unique chunk name that can be used in the chunk registry.
@ -79,10 +80,15 @@ export function genChunkName(
const shortHash = simpleHash(modulePath, 3);
str = `${preferredName}${shortHash}`;
}
const name = str === '/' ? 'index' : docuHash(str);
const name = docuHash(str);
chunkName = prefix ? `${prefix}---${name}` : name;
}
const seenCount = (chunkNameCount.get(chunkName) ?? 0) + 1;
if (seenCount > 1) {
chunkName += seenCount.toString(36);
}
chunkNameCache.set(modulePath, chunkName);
chunkNameCount.set(chunkName, seenCount);
}
return chunkName;
}