Skip to content

Commit

Permalink
fix: check if page was already translated
Browse files Browse the repository at this point in the history
This prevent loops between two onCreatePage implementations.
  • Loading branch information
openscript committed Jan 9, 2023
1 parent b76b086 commit 8ba5896
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/onCreatePage/translatePage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,20 @@ describe('translatePage', () => {
},
});
});

it('should skip already translated pages', () => {
const page: Page = {
path: '/imprint',
component: {} as any,
context: {
locale: 'de-CH',
localePagesId: 'imprint',
},
};

translatePage({ page, actions } as any, options);

expect(actions.deletePage).not.toBeCalled();
expect(actions.createPage).not.toBeCalled();
});
});
5 changes: 5 additions & 0 deletions src/onCreatePage/translatePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { generatePageContextByPath, translatePagePath, translatePagePaths } from
export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageContext>, options: PluginOptions) => {
const { createPage, deletePage } = actions;

// If this page was already translated, we skip it.
if (page.context?.locale && page.context.localePagesId) {
return;
}

// Translate statefully created pages from `/src/pages` or gatsby-plugin-page-creator
if (options && page.isCreatedByStatefulCreatePages) {
const paths = translatePagePaths(page.path, options);
Expand Down

0 comments on commit 8ba5896

Please sign in to comment.