@zachhandley/ez-i18n 0.3.10 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +23 -6
- package/dist/middleware.js +3 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -894,15 +894,32 @@ function __deepMerge(target, ...sources) {
|
|
|
894
894
|
function getPublicLoaderCode() {
|
|
895
895
|
return `
|
|
896
896
|
async function __loadPublicJson(url, absolutePath) {
|
|
897
|
+
// Browser environment - use fetch with relative URL
|
|
897
898
|
if (typeof window !== 'undefined') {
|
|
898
|
-
// Browser - use fetch with relative URL
|
|
899
899
|
return fetch(url).then(r => r.json());
|
|
900
|
-
} else {
|
|
901
|
-
// SSR/Node - read from filesystem
|
|
902
|
-
const fs = await import('node:fs');
|
|
903
|
-
const content = fs.readFileSync(absolutePath, 'utf-8');
|
|
904
|
-
return JSON.parse(content);
|
|
905
900
|
}
|
|
901
|
+
|
|
902
|
+
// Edge runtime detection (Cloudflare Workers, Vercel Edge, Deno, etc.)
|
|
903
|
+
// These environments have fetch but no filesystem access
|
|
904
|
+
const isEdgeRuntime = typeof globalThis.caches !== 'undefined' || // CF Workers / Service Workers
|
|
905
|
+
typeof globalThis.EdgeRuntime !== 'undefined' || // Vercel Edge
|
|
906
|
+
typeof Deno !== 'undefined' || // Deno
|
|
907
|
+
!globalThis.process?.versions?.node; // No Node.js process
|
|
908
|
+
|
|
909
|
+
if (isEdgeRuntime) {
|
|
910
|
+
// Edge runtime - need absolute URL for fetch
|
|
911
|
+
// Priority: Astro site config > middleware-set origin > relative (hope for the best)
|
|
912
|
+
const origin = import.meta.env.SITE?.replace(/\\/$/, '') ||
|
|
913
|
+
globalThis.__EZ_I18N_ORIGIN__ ||
|
|
914
|
+
'';
|
|
915
|
+
const fetchUrl = origin ? origin + url : url;
|
|
916
|
+
return fetch(fetchUrl).then(r => r.json());
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
// Traditional Node.js SSR - read from filesystem
|
|
920
|
+
const fs = await import('node:fs');
|
|
921
|
+
const content = fs.readFileSync(absolutePath, 'utf-8');
|
|
922
|
+
return JSON.parse(content);
|
|
906
923
|
}`;
|
|
907
924
|
}
|
|
908
925
|
function resolveConfig(config) {
|
package/dist/middleware.js
CHANGED
|
@@ -40,6 +40,9 @@ var onRequest = defineMiddleware(async ({ cookies, request, locals, redirect },
|
|
|
40
40
|
}
|
|
41
41
|
locals.locale = locale;
|
|
42
42
|
try {
|
|
43
|
+
if (!import.meta.env.SITE && !globalThis.__EZ_I18N_ORIGIN__) {
|
|
44
|
+
globalThis.__EZ_I18N_ORIGIN__ = url.origin;
|
|
45
|
+
}
|
|
43
46
|
const { loadTranslations } = await import("ez-i18n:translations");
|
|
44
47
|
locals.translations = await loadTranslations(locale);
|
|
45
48
|
globalThis.__EZ_I18N_SSR__ = {
|