mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-14 07:18:02 +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');
|
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', () => {
|
describe('handleDuplicateRoutes', () => {
|
||||||
|
|
|
@ -51,6 +51,7 @@ function indent(str: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunkNameCache = new Map<string, 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.
|
* 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);
|
const shortHash = simpleHash(modulePath, 3);
|
||||||
str = `${preferredName}${shortHash}`;
|
str = `${preferredName}${shortHash}`;
|
||||||
}
|
}
|
||||||
const name = str === '/' ? 'index' : docuHash(str);
|
const name = docuHash(str);
|
||||||
chunkName = prefix ? `${prefix}---${name}` : name;
|
chunkName = prefix ? `${prefix}---${name}` : name;
|
||||||
}
|
}
|
||||||
|
const seenCount = (chunkNameCount.get(chunkName) ?? 0) + 1;
|
||||||
|
if (seenCount > 1) {
|
||||||
|
chunkName += seenCount.toString(36);
|
||||||
|
}
|
||||||
chunkNameCache.set(modulePath, chunkName);
|
chunkNameCache.set(modulePath, chunkName);
|
||||||
|
chunkNameCount.set(chunkName, seenCount);
|
||||||
}
|
}
|
||||||
return chunkName;
|
return chunkName;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue