Skip to content

Commit f3f44f8

Browse files
committed
fix: enhance file retrieval with BCP 47 language support and improve fallback logic
1 parent ca0cf3d commit f3f44f8

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

src/index.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,58 @@ class CoCreateFileSystem {
9999
"/manifest.webmanifest",
100100
"/service-worker.js"
101101
].includes(pathname)
102-
)
102+
) {
103103
file = await getDefaultFile(pathname);
104-
else file = await crud.send(data);
104+
} else {
105+
file = await crud.send(data);
106+
}
105107

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+
153+
// --- Wildcard fallback ---
106154
if (!file || !file.object || !file.object[0]) {
107155
pathname = valideUrl.pathname;
108156
let lastIndex = pathname.lastIndexOf("/");
@@ -137,8 +185,9 @@ class CoCreateFileSystem {
137185
}
138186

139187
let src;
140-
if (file["src"]) src = file["src"];
141-
else {
188+
if (file["src"]) {
189+
src = file["src"];
190+
} else {
142191
let fileSrc = await crud.send({
143192
method: "object.read",
144193
host: hostname,
@@ -330,3 +379,4 @@ class CoCreateFileSystem {
330379
}
331380

332381
module.exports = CoCreateFileSystem;
382+
module.exports = CoCreateFileSystem;

0 commit comments

Comments
 (0)