Skip to content

Commit 266c4ab

Browse files
committed
fix: unoptimize images if we have base prefix
1 parent 35a7233 commit 266c4ab

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

.github/workflows/cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
NEXT_PUBLIC_POSTHOG_HOST_API: ${{ vars.NEXT_PUBLIC_POSTHOG_HOST_API }}
6363
# Build Date
6464
MAPSWIPE_BUILD_DATE: ${{ env.MAPSWIPE_API_LAST_MODIFIED_EPOCH }}
65-
BASE_PREFIX: ${{ vars.BASE_PREFIX }}
65+
NEXT_PUBLIC_BASE_PREFIX: ${{ vars.BASE_PREFIX }}
6666

6767
- name: Upload GH artifacts
6868
uses: actions/upload-pages-artifact@v3

next.config.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
const withExportImages = require('next-export-optimize-images');
22

3-
const basePrefix = process.env.BASE_PREFIX;
3+
const basePrefix = process.env.NEXT_PUBLIC_BASE_PREFIX;
44
const isBasePrefixAvailable = basePrefix !== undefined && basePrefix.trim().length > 0;
55

6-
const nextConfig = withExportImages({
6+
let nextConfig = {
77
output: 'export',
88
trailingSlash: true,
99
reactStrictMode: true,
1010
basePath: isBasePrefixAvailable ? `/${basePrefix}` : undefined,
1111
assetPrefix: isBasePrefixAvailable ? `/${basePrefix}/` : undefined,
1212
images: {
13-
domain: ['https://firebasestorage.googleapis.com/v0/b/msf-mapswipe.appspot.com'],
13+
domains: ['firebasestorage.googleapis.com'],
1414
deviceSizes: [640, 960, 1280, 1600, 1920],
15+
unoptimized: true,
1516
},
16-
});
17+
};
18+
19+
// Only wrap with `next-export-optimize-images` if no basePrefix (i.e., local/dev)
20+
if (!isBasePrefixAvailable) {
21+
nextConfig = withExportImages({
22+
...nextConfig,
23+
images: {
24+
domains: ['firebasestorage.googleapis.com'],
25+
deviceSizes: [640, 960, 1280, 1600, 1920],
26+
},
27+
});
28+
}
1729

1830
module.exports = nextConfig;

src/components/ImageWrapper/index.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ function ImageWrapper(props: Props) {
2121
...otherProps
2222
} = props;
2323

24+
const basePrefix = process.env.NEXT_PUBLIC_BASE_PREFIX ?? '';
25+
const isExternal = src.startsWith('http://') || src.startsWith('https://');
26+
27+
let modifiedSrc = src;
28+
29+
const isBasePrefixAvailable = basePrefix !== undefined && basePrefix.trim().length > 0;
30+
if (isBasePrefixAvailable) {
31+
modifiedSrc = isExternal ? src : `/${basePrefix}/${src.replace(/^\/+/, '')}`;
32+
}
33+
2434
return (
2535
<div className={_cs(className, styles.imageWrapper)}>
26-
{!nonOptimizedImage ? (
36+
{(!nonOptimizedImage && !isBasePrefixAvailable) ? (
2737
<Image
28-
src={src}
38+
src={modifiedSrc}
2939
className={_cs(imageClassName, styles.image)}
3040
fill
3141
// eslint-disable-next-line react/jsx-props-no-spreading
@@ -34,7 +44,7 @@ function ImageWrapper(props: Props) {
3444
) : (
3545
// eslint-disable-next-line @next/next/no-img-element
3646
<img
37-
src={src}
47+
src={modifiedSrc}
3848
className={_cs(imageClassName, styles.image, styles.nonOptimizedImage)}
3949
alt={otherProps.alt}
4050
/>

src/components/Navbar/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import { useTranslation } from 'next-i18next';
33
import { useRouter } from 'next/router';
44
import { _cs } from '@togglecorp/fujs';
5-
import Image from 'next/image';
65
import { MdMenu } from 'react-icons/md';
76
import { IoEarthSharp } from 'react-icons/io5';
87

@@ -12,6 +11,7 @@ import LanguageSwitcher from 'components/LanguageSwitcher';
1211
import DropdownMenu from 'components/DropdownMenu';
1312
import useBooleanState from 'hooks/useBooleanState';
1413
import languageTitleMap from 'utils/languages';
14+
import ImageWrapper from 'components/ImageWrapper';
1515

1616
import i18nextConfig from '@/next-i18next.config';
1717

@@ -42,10 +42,9 @@ function Navbar(props: Props) {
4242
className={styles.logo}
4343
href="/[locale]/"
4444
>
45-
<Image
45+
<ImageWrapper
4646
src="/logo.svg"
4747
alt={t('mapswipe-logo')}
48-
fill
4948
/>
5049
</Link>
5150
<div

0 commit comments

Comments
 (0)