@@ -26,9 +26,9 @@ class CoCreateFileSystem {
26
26
this . sitemap = sitemap ;
27
27
}
28
28
29
- async send ( req , res , crud , organization , valideUrl ) {
29
+ async send ( req , res , crud , organization , urlObject ) {
30
30
try {
31
- const hostname = valideUrl . hostname ;
31
+ const hostname = urlObject . hostname ;
32
32
33
33
let data = {
34
34
method : "object.read" ,
@@ -72,12 +72,12 @@ class CoCreateFileSystem {
72
72
} ) ;
73
73
}
74
74
75
- let parameters = valideUrl . searchParams ;
75
+ let parameters = urlObject . searchParams ;
76
76
if ( parameters . size ) {
77
77
console . log ( "parameters" , parameters ) ;
78
78
}
79
79
80
- let pathname = valideUrl . pathname ;
80
+ let pathname = urlObject . pathname ;
81
81
82
82
if ( pathname . endsWith ( "/" ) ) {
83
83
pathname += "index.html" ;
@@ -86,7 +86,17 @@ class CoCreateFileSystem {
86
86
if ( ! directory . includes ( "." ) ) pathname += "/index.html" ;
87
87
}
88
88
89
- data . $filter . query . pathname = pathname ;
89
+ const bcp47Regex = / ^ \/ ( [ a - z A - Z ] { 2 , 3 } (?: - [ a - z A - Z 0 - 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
+ }
90
100
91
101
let file ;
92
102
if (
@@ -105,54 +115,9 @@ class CoCreateFileSystem {
105
115
file = await crud . send ( data ) ;
106
116
}
107
117
108
- // --- Only check for BCP 47 if file not found ---
109
- const bcp47Regex = / ^ \/ ( [ a - z A - Z ] { 2 , 3 } (?: - [ a - z A - Z 0 - 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
118
// --- Wildcard fallback ---
154
119
if ( ! file || ! file . object || ! file . object [ 0 ] ) {
155
- pathname = valideUrl . pathname ;
120
+ pathname = urlObject . pathname ;
156
121
let lastIndex = pathname . lastIndexOf ( "/" ) ;
157
122
let wildcardPath = pathname . substring ( 0 , lastIndex + 1 ) ;
158
123
let wildcard = pathname . substring ( lastIndex + 1 ) ;
@@ -228,11 +193,10 @@ class CoCreateFileSystem {
228
193
src = Buffer . from ( src , "base64" ) ;
229
194
} else if ( contentType === "text/html" ) {
230
195
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 ) ;
236
200
} catch ( err ) {
237
201
console . warn ( "server-side-render: " + err . message ) ;
238
202
}
@@ -379,4 +343,3 @@ class CoCreateFileSystem {
379
343
}
380
344
381
345
module . exports = CoCreateFileSystem ;
382
- module . exports = CoCreateFileSystem ;
0 commit comments