Skip to content

Commit c9f8591

Browse files
committed
fix: refactor send method to use urlObject and enhance BCP 47 language handling
1 parent f3f44f8 commit c9f8591

File tree

1 file changed

+20
-57
lines changed

1 file changed

+20
-57
lines changed

src/index.js

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class CoCreateFileSystem {
2626
this.sitemap = sitemap;
2727
}
2828

29-
async send(req, res, crud, organization, valideUrl) {
29+
async send(req, res, crud, organization, urlObject) {
3030
try {
31-
const hostname = valideUrl.hostname;
31+
const hostname = urlObject.hostname;
3232

3333
let data = {
3434
method: "object.read",
@@ -72,12 +72,12 @@ class CoCreateFileSystem {
7272
});
7373
}
7474

75-
let parameters = valideUrl.searchParams;
75+
let parameters = urlObject.searchParams;
7676
if (parameters.size) {
7777
console.log("parameters", parameters);
7878
}
7979

80-
let pathname = valideUrl.pathname;
80+
let pathname = urlObject.pathname;
8181

8282
if (pathname.endsWith("/")) {
8383
pathname += "index.html";
@@ -86,7 +86,17 @@ class CoCreateFileSystem {
8686
if (!directory.includes(".")) pathname += "/index.html";
8787
}
8888

89-
data.$filter.query.pathname = pathname;
89+
const bcp47Regex = /^\/([a-zA-Z]{2,3}(?:-[a-zA-Z0-9]{2,8})+)\//;
90+
const match = pathname.match(bcp47Regex);
91+
let lang, langRegion;
92+
if (match) {
93+
langRegion = match[1];
94+
lang = langRegion.split("-")[0]; // Get just the base language (e.g., 'en', 'es', 'fr')
95+
let newPathname = pathname.replace(langRegion, lang);
96+
data.$filter.query.pathname = { $in: [newPathname, pathname] };
97+
} else {
98+
data.$filter.query.pathname = pathname;
99+
}
90100

91101
let file;
92102
if (
@@ -105,54 +115,9 @@ class CoCreateFileSystem {
105115
file = await crud.send(data);
106116
}
107117

108-
// --- Only check for BCP 47 if file not found ---
109-
const bcp47Regex = /^\/([a-zA-Z]{2,3}(?:-[a-zA-Z0-9]{2,8})+)\//;
110-
if (!file || !file.object || !file.object[0]) {
111-
const match = pathname.match(bcp47Regex);
112-
if (match) {
113-
const langCode = match[1];
114-
// Remove BCP 47 segment from pathname
115-
const strippedPathname = pathname.replace(bcp47Regex, "/");
116-
// Query for a file where languages[langCode].href matches strippedPathname
117-
data.$filter.query = {
118-
...data.$filter.query,
119-
[`languages.${langCode}.href`]: strippedPathname
120-
};
121-
file = await crud.send(data);
122-
if (file && file.object && file.object[0]) {
123-
let language = file.languages[langCode];
124-
let translation = language.translation;
125-
if (typeof translation === "object") {
126-
// Handle translation object case
127-
} else {
128-
translation = await crud.send({
129-
method: "object.read",
130-
host: hostname,
131-
array: "translation",
132-
object: {
133-
_id: translation
134-
},
135-
organization_id
136-
});
137-
}
138-
if (translation) {
139-
// ToDo: apply translation to file
140-
}
141-
142-
if (file.languages) {
143-
// ToDo: Apply languages as links to to head of file
144-
file.languages.forEach((lang) => {
145-
let link = `<link rel="alternate" hreflang="${lang.code}" href="${lang.href}">`;
146-
// Insert link into head of file
147-
});
148-
}
149-
}
150-
}
151-
}
152-
153118
// --- Wildcard fallback ---
154119
if (!file || !file.object || !file.object[0]) {
155-
pathname = valideUrl.pathname;
120+
pathname = urlObject.pathname;
156121
let lastIndex = pathname.lastIndexOf("/");
157122
let wildcardPath = pathname.substring(0, lastIndex + 1);
158123
let wildcard = pathname.substring(lastIndex + 1);
@@ -228,11 +193,10 @@ class CoCreateFileSystem {
228193
src = Buffer.from(src, "base64");
229194
} else if (contentType === "text/html") {
230195
try {
231-
src = await this.render.HTML(
232-
src,
233-
organization_id,
234-
valideUrl
235-
);
196+
file.urlObject = urlObject;
197+
file.langRegion = langRegion;
198+
file.lang = lang;
199+
src = await this.render.HTML(file);
236200
} catch (err) {
237201
console.warn("server-side-render: " + err.message);
238202
}
@@ -379,4 +343,3 @@ class CoCreateFileSystem {
379343
}
380344

381345
module.exports = CoCreateFileSystem;
382-
module.exports = CoCreateFileSystem;

0 commit comments

Comments
 (0)