Skip to content

Commit

Permalink
Fix i18n for preview pages (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey800 authored Jan 12, 2022
1 parent a39fcdb commit 7d15e9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
46 changes: 23 additions & 23 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ const { i18n } = require('./next-i18next.config');

const { PREVIEW } = process.env;
const PROD = process.env.NODE_ENV === 'production';
const isPreview = !!(PROD && PREVIEW);

// Enable static HTML export of the preview page in production and if the
// preview file is provided.
const previewOptions =
PROD && PREVIEW
? {
// The Image API doesn't work for exported apps, so we need to use a
// different image loader to supplement it. The workaround is to use imgix
// with a root path for Next.js v10: https://git.io/J0k6G.
images: {
loader: 'imgix',
path: process.env.BASE_PATH || '/',
},

// Override default pages being exported to be only the preview page:
// https://stackoverflow.com/a/64071979
exportPathMap() {
return {
'/preview': { page: '/preview' },
};
},
}
: {};

if (PROD && PREVIEW) {
const previewOptions = isPreview
? {
// The Image API doesn't work for exported apps, so we need to use a
// different image loader to supplement it. The workaround is to use imgix
// with a root path for Next.js v10: https://git.io/J0k6G.
images: {
loader: 'imgix',
path: process.env.BASE_PATH || '/',
},

// Override default pages being exported to be only the preview page:
// https://stackoverflow.com/a/64071979
exportPathMap() {
return {
'/preview': { page: '/preview' },
};
},
}
: {};

if (isPreview) {
console.log('Building preview page for plugin file', PREVIEW);
}

module.exports = {
...previewOptions,
i18n,
i18n: isPreview ? undefined : i18n,

basePath: process.env.BASE_PATH || '',
pageExtensions: ['ts', 'tsx'],
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/pages/[...parts].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface Props extends SSRConfig {

const LOCALE_DIR = __dirname.replace('.next/server/pages', 'i18n');

const isPreview = !!(
process.env.NODE_ENV === 'production' && process.env.PREVIEW
);

/**
* Special Next.js function responsible for returning all possible paths that
* can be built by this page at build time. Since we use `getStaticPaths()` to
Expand All @@ -39,7 +43,7 @@ export function getStaticPaths(): GetStaticPathsResult {
for (const locale of supportedLocales) {
for (const file of mdxFiles) {
paths.push({
locale,
locale: isPreview ? undefined : locale,
params: {
parts: file.split('/'),
},
Expand Down

0 comments on commit 7d15e9f

Please sign in to comment.