UNPKG

@newrelic/gatsby-theme-newrelic

Version:

[![Community Project header](https://github.com/newrelic/opensource-website/raw/master/src/images/categories/Community_Project.png)](https://opensource.newrelic.com/oss-category/#community-project)

50 lines (41 loc) 1.37 kB
const { getI18nConfig } = require('../src/utils/config'); const { getFileRelativePath } = require('./utils/fs'); const getLocale = require('./utils/getLocale'); const addLocale = ({ page }, themeOptions) => { const { defaultLocale, locales } = getI18nConfig(themeOptions); const additionalLocales = locales.filter((locale) => !locale.isDefault); // We don't want to add the locale to 404 pageContext because we want to // determine the locale at runtime based on the path in the URL if ( page.context.locale || page.internalComponentName === 'Component/404.html' || page.internalComponentName === 'Component/dev-404-page/' || page.internalComponentName === 'Component/404/' ) { return page; } const { locale } = additionalLocales.find( ({ locale }) => locale === getLocale({ location: { pathname: page.path } }, themeOptions) ) || defaultLocale; return { ...page, context: { ...page.context, locale } }; }; const addFileRelativePath = ({ page, store }) => { if (page.context.fileRelativePath) { return page; } const { program } = store.getState(); return { ...page, context: { ...page.context, fileRelativePath: getFileRelativePath( page.componentPath, program.directory ), }, }; }; module.exports = [addLocale, addFileRelativePath];