Skip to content

Commit 2c6abc7

Browse files
committed
fetch fix part 1
1 parent 931235a commit 2c6abc7

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

โ€Žsrc/moto ride.jsโ€Ž

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,38 @@ function changeCatalogMenueFunction(event) {
8282

8383
//?: ื”ืฉืžื” ืฉืœ ืื™ืจื•ืข ืฉืžืจืขื ืŸ ืืช ื”ื“ืคื™ื
8484
document.addEventListener('DOMContentLoaded', () => {
85+
// ื”ื—ืœืฃ ืœึพusername ื•ึพrepo ืฉืœืš
86+
const githubUsername = 'shaialt';
87+
const githubRepo = 'Moto-Ride';
88+
89+
// ื‘ื•ื“ืง ืื ืื ื—ื ื• ื‘ึพGitHub Pages ืœืคื™ ื”-hostname (ืืคืฉืจ ืœื”ืชืื™ื ืœืคื™ ื”ืฆื•ืจืš)
90+
const isGitHubPages = window.location.hostname === `${githubUsername}.github.io`;
91+
92+
// ืžื’ื“ื™ืจ ืืช ื‘ืกื™ืก ื”ื ืชื™ื‘ ืœืคื™ ื”ืกื‘ื™ื‘ื”:
93+
// ืื GitHub Pages: /repo-name/
94+
// ืื—ืจืช (ืœื™ื™ื‘ ืกืจื‘ืจ ืžืงื•ืžื™ ืื• ืกื‘ื™ื‘ื” ืื—ืจืช): ../
95+
const basePath = isGitHubPages ? `/${githubRepo}/` : '/';
96+
8597
//?: ื”ื›ื ืกืช ืžื•ืฆืจื™ื ืžืชื•ืš ื’'ื™ื™ืกื•ืŸ ืœืงื˜ืœื•ื’ ืื•ืคื ื•ืขื™ื
86-
fetch('/public/data/products.json')
98+
fetch(`${basePath}public/data/products.json`)
8799
.then(response => response.json())
88100
.then(products => {
89101

102+
// ืžืชืงืŸ ื ืชื™ื‘ื™ื ืฉืœ ืชืžื•ื ื•ืช ื‘ื›ืœ ืžื•ืฆืจ
103+
products.forEach(product => {
104+
// ื›ืืŸ ืžื ืงื™ื ืืช ื”ื ืชื™ื‘ ื”ื™ื—ืกื™ "../" ื•ืžื•ืกื™ืคื™ื ืืช basePath
105+
// ืฉื™ื ืœื‘ ืฉืฆืจื™ืš ืœื”ืชืื™ื ืืช ื”ื ืชื™ื‘ ื›ืš ืฉื™ืชืื™ื ืœืชื™ืงื™ื™ืช images ื‘ืžืขืจื›ืช ืฉืœืš
106+
if (product.image) {
107+
product.image = product.image.replace(/^(\.\.\/)+/, basePath);
108+
}
109+
if (product.logo) {
110+
product.logo = product.logo.replace(/^(\.\.\/)+/, basePath);
111+
}
112+
if (Array.isArray(product.images)) {
113+
product.images = product.images.map(imgPath => imgPath.replace(/^(\.\.\/)+/, basePath));
114+
}
115+
});
116+
90117
// ื‘ื“ื™ืงืช ื›ืžื•ืช ืžื•ืฆืจื™ื ื‘ืืชืจ
91118
let productsNumbersTotal = 0;
92119
products.forEach(() => productsNumbersTotal++);

0 commit comments

Comments
ย (0)