Skip to content

Commit 6ea2788

Browse files
committed
Fix localhost image paths with environment-aware path utility
1 parent ed5d1e8 commit 6ea2788

4 files changed

Lines changed: 38 additions & 37 deletions

File tree

frontend/src/components/common/Header.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Link, useLocation } from 'react-router-dom';
33
import { useSelector } from 'react-redux';
44
import { ShoppingCart, Menu, X } from 'lucide-react';
55
import { NAVIGATION_ITEMS } from '../../utils/constants';
6+
import { getImagePath } from '../../utils/pathUtils';
67

78
const Header = () => {
89
const location = useLocation();
@@ -37,7 +38,7 @@ const Header = () => {
3738
isScrolled ? 'w-10 h-10' : 'w-14 h-14'
3839
}`}>
3940
<img
40-
src="./images/logo-removebg-preview.png"
41+
src={getImagePath("images/logo-removebg-preview.png")}
4142
alt="MoreByPrinal Logo"
4243
className={`object-contain transition-all duration-300 ${
4344
isScrolled ? 'w-10 h-10' : 'w-14 h-14'

frontend/src/components/product/ProductCard.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState } from 'react';
22
import { Link } from 'react-router-dom';
33
import { ShoppingCart } from 'lucide-react';
4+
import { getImagePath } from '../../utils/pathUtils';
45

56
const ProductCard = ({ product }) => {
67
const [isHovered, setIsHovered] = useState(false);
@@ -17,7 +18,7 @@ const ProductCard = ({ product }) => {
1718
<div className="relative w-full h-80 overflow-hidden">
1819
{/* Main Image */}
1920
<img
20-
src={product.images[0] || './images/placeholder-product.jpg'}
21+
src={product.images[0] || getImagePath('images/placeholder-product.jpg')}
2122
alt={product.name}
2223
className={`w-full h-80 object-cover transition-all duration-700 transform group-hover:scale-110 ${
2324
isHovered && product.images[1] ? 'opacity-0' : 'opacity-100'

frontend/src/models/Product.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getImagePath } from '../utils/pathUtils';
2+
13
// Product data structure
24
export const ProductModel = {
35
id: '',
@@ -18,18 +20,21 @@ export const ProductModel = {
1820
updatedAt: ''
1921
};
2022

23+
// Helper function to create product images with correct paths
24+
const createProductImages = (baseName) => [
25+
getImagePath(`images/products/${baseName}-1.jpg`),
26+
getImagePath(`images/products/${baseName}-2.jpg`),
27+
getImagePath(`images/products/${baseName}-3.jpg`)
28+
];
29+
2130
// Sample product data for development
2231
export const sampleProducts = [
2332
{
2433
id: '1',
2534
name: 'Durga',
2635
price: 899,
2736
originalPrice: 1299,
28-
images: [
29-
'./images/products/durga-1.jpg',
30-
'./images/products/durga-2.jpg',
31-
'./images/products/durga-3.jpg'
32-
],
37+
images: createProductImages('durga'),
3338
sizes: ['XS', 'S', 'M', 'L', 'XL'],
3439
description: 'Durga is a bold black top featuring intricate lace detailing and a dramatic backless design. Perfect for evening wear and special occasions.',
3540
shortDescription: 'Black top with intricate lace detailing',
@@ -46,11 +51,7 @@ export const sampleProducts = [
4651
name: 'Shakti',
4752
price: 899,
4853
originalPrice: 1299,
49-
images: [
50-
'./images/products/shakti-1.jpg',
51-
'./images/products/shakti-2.jpg',
52-
'./images/products/shakti-3.jpg'
53-
],
54+
images: createProductImages('shakti'),
5455
sizes: ['XS', 'S', 'M', 'L', 'XL'],
5556
description: 'Shakti is a powerful red top with intricate lace detailing and a dramatic backless design. Long in the front and open at the back, it\'s made to turn heads with elegance and fire.',
5657
shortDescription: 'Red top with intricate lace detailing',
@@ -67,11 +68,7 @@ export const sampleProducts = [
6768
name: 'Harsha',
6869
price: 599,
6970
originalPrice: 899,
70-
images: [
71-
'./images/products/harsha-1.jpg',
72-
'./images/products/harsha-2.jpg',
73-
'./images/products/harsha-3.jpg'
74-
],
71+
images: createProductImages('harsha'),
7572
sizes: ['XS', 'S', 'M', 'L', 'XL'],
7673
description: 'Harsha is a pure cotton yellow short kurti with beautiful floral patterns. Perfect for casual wear and comfortable all-day use.',
7774
shortDescription: 'Yellow cotton kurti with floral patterns',
@@ -88,11 +85,7 @@ export const sampleProducts = [
8885
name: 'Neha',
8986
price: 599,
9087
originalPrice: 899,
91-
images: [
92-
'./images/products/neha-1.jpg',
93-
'./images/products/neha-2.jpg',
94-
'./images/products/neha-3.jpg'
95-
],
88+
images: createProductImages('neha'),
9689
sizes: ['XS', 'S', 'M', 'L', 'XL'],
9790
description: 'Neha is a pure cotton short kurti featuring vibrant floral patterns. Lightweight and comfortable for everyday wear.',
9891
shortDescription: 'White cotton kurti with floral patterns',
@@ -109,11 +102,7 @@ export const sampleProducts = [
109102
name: 'Radha',
110103
price: 1299,
111104
originalPrice: 1899,
112-
images: [
113-
'./images/products/radha-1.jpg',
114-
'./images/products/radha-2.jpg',
115-
'./images/products/radha-3.jpg'
116-
],
105+
images: createProductImages('radha'),
117106
sizes: ['XS', 'S', 'M', 'L', 'XL'],
118107
description: 'Radha is an elegant peacock blue lehenga choli with intricate zari work and mirror embellishments. Perfect for festivals and special occasions.',
119108
shortDescription: 'Peacock blue lehenga choli with zari work',
@@ -130,11 +119,7 @@ export const sampleProducts = [
130119
name: 'Sanjh',
131120
price: 799,
132121
originalPrice: 1199,
133-
images: [
134-
'./images/products/sanjh-1.jpg',
135-
'./images/products/sanjh-2.jpg',
136-
'./images/products/sanjh-3.jpg'
137-
],
122+
images: createProductImages('sanjh'),
138123
sizes: ['XS', 'S', 'M', 'L', 'XL'],
139124
description: 'Sanjh is a beautiful evening dress in deep teal with elegant draping and subtle sequin work. Ideal for dinner parties and evening events.',
140125
shortDescription: 'Deep teal evening dress with sequins',
@@ -151,11 +136,7 @@ export const sampleProducts = [
151136
name: 'Shreya',
152137
price: 699,
153138
originalPrice: 999,
154-
images: [
155-
'./images/products/shreya-1.jpg',
156-
'./images/products/shreya-2.jpg',
157-
'./images/products/shreya-3.jpg'
158-
],
139+
images: createProductImages('shreya'),
159140
sizes: ['XS', 'S', 'M', 'L', 'XL'],
160141
description: 'Shreya is a comfortable cotton top in soft mint green with delicate embroidery. Perfect for casual outings and everyday wear.',
161142
shortDescription: 'Mint green cotton top with embroidery',

frontend/src/utils/pathUtils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Utility function to get correct asset paths for both development and production
2+
export const getAssetPath = (path) => {
3+
// Remove leading dot and slash if present
4+
const cleanPath = path.replace(/^\.\//, '');
5+
6+
// In development, React serves public folder files from root
7+
// In production (GitHub Pages), we need the /morebyprinal prefix
8+
if (process.env.NODE_ENV === 'development') {
9+
return `/${cleanPath}`;
10+
} else {
11+
return `./${cleanPath}`;
12+
}
13+
};
14+
15+
// Specific function for images
16+
export const getImagePath = (imagePath) => {
17+
return getAssetPath(imagePath);
18+
};

0 commit comments

Comments
 (0)