mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-28 17:57:48 +02:00
fix(core): avoid hash collision when generating chunk names (#8538)
This commit is contained in:
parent
8714a95900
commit
00023c24b6
2 changed files with 41 additions and 1 deletions
|
@ -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', () => {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue