Skip to content

Commit

Permalink
fix(core): avoid hash collision when generating chunk names (#8538)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Jan 18, 2023
1 parent 8714a95 commit 00023c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/docusaurus/src/server/__tests__/routes.test.ts
Expand Up @@ -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', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/docusaurus/src/server/routes.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 00023c2

Please sign in to comment.