Skip to content

Commit

Permalink
fix useTitle/useTitleTemplate in concurrent environments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Sep 23, 2023
1 parent 7f4f81f commit b15d99a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions __tests__/useMeta.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ describe('useMeta', () => {
));
});
vi.runAllTimers();
console.log(document.head.innerHTML);
expect(document.head.innerHTML).toContain('<meta charset="utf-2">');
expect(document.head.innerHTML).toContain(
'<meta name="description" content="This is not a test">'
Expand Down
23 changes: 16 additions & 7 deletions src/dispatcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const createDispatcher = () => {
titleTemplateQueue[0]
);

metaQueue.forEach((meta) => {
metaQueue.forEach(meta => {
if (!visited.has(meta.charset ? meta.keyword : meta[meta.keyword])) {
visited.add(meta.charset ? meta.keyword : meta[meta.keyword]);
changeOrCreateMetaTag(meta);
Expand Down Expand Up @@ -136,18 +136,27 @@ export const createDispatcher = () => {
const index = queue.indexOf(payload as string);
queue.splice(index, 1);

let currentIndex =
type === TITLE ? currentTitleIndex : currentTitleTemplateIndex;
if (currentIndex === index) {
currentIndex--;
}

if (index === 0)
document.title = applyTitleTemplate(
titleQueue[0] || '',
titleTemplateQueue[0]
);
} else {
const oldMeta = metaQueue[metaQueue.indexOf(payload as MetaPayload)];
const index = metaQueue.indexOf(payload as MetaPayload);

const oldMeta = metaQueue[index];

if (oldMeta) {
metaQueue.splice(metaQueue.indexOf(payload as MetaPayload), 1);
metaQueue.splice(index, 1);

const newMeta = metaQueue.find(
(m) =>
m =>
m.keyword === oldMeta.keyword &&
(m.charset || m[m.keyword] === oldMeta[m.keyword])
);
Expand Down Expand Up @@ -252,7 +261,7 @@ export const createDispatcher = () => {
const scripts = [...scriptQueue];
metaQueue.reverse();
// @ts-ignore
const metas = [...metaQueue].filter((meta) => {
const metas = [...metaQueue].filter(meta => {
if (!visited.has(meta.charset ? meta.keyword : meta[meta.keyword])) {
visited.add(meta.charset ? meta.keyword : meta[meta.keyword]);
return true;
Expand All @@ -269,9 +278,9 @@ export const createDispatcher = () => {
return {
lang,
title,
links: links.map((x) => ({ ...x, ['data-hoofd']: '1' })),
links: links.map(x => ({ ...x, ['data-hoofd']: '1' })),
scripts,
metas: metas.map((meta) =>
metas: metas.map(meta =>
meta.keyword === 'charset'
? {
charset: meta[meta.keyword],
Expand Down

0 comments on commit b15d99a

Please sign in to comment.