fix and simplify baseurl

This commit is contained in:
Dobromir Popov
2024-04-01 11:12:00 +03:00
parent f83a3e9bd9
commit f488fa971a
8 changed files with 24 additions and 67 deletions

View File

@ -76,39 +76,7 @@ exports.setBaseUrl = function (req) {
};
exports.getBaseUrl = function (relative = "", req = null) {
if (typeof window === 'undefined') {
// Server-side logic
// Read the base URL from env (PUBLIC_URL):
return process.env.PUBLIC_URL + relative;
// const filePath = path.join(__dirname, 'baseUrl.txt');
// try {
// if (fs.existsSync(filePath)) {
// const baseUrl = fs.readFileSync(filePath, 'utf8').trim();
// const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl;
// return fullUrl;
// } else {
// if (req) {
// // Assuming setBaseUrl is defined somewhere in this file
// const baseUrl = exports.setBaseUrl(req);
// return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`;
// }
// console.log('Base URL file does not exist.');
// return null;
// }
// } catch (error) {
// console.error('Error reading the base URL file:', error);
// return null;
// }
} else {
// Client-side logic
// Fetch the base URL from the server endpoint you've set up
const baseUrl = window.location.origin;
const fullUrl = relative ? `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}` : baseUrl;
//console.log("getBaseUrl()=", fullUrl);
return fullUrl.toString();
}
return process.env.NEXT_PUBLIC_PUBLIC_URL + relative;
};