@alpaca-headless/alpaca-headless-nextjs 1.0.2698 → 1.0.2699
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.
|
@@ -66,6 +66,10 @@ const fetchRouteData = (0, react_1.cache)((path, host, mode, itemId, language) =
|
|
|
66
66
|
pageState: "",
|
|
67
67
|
};
|
|
68
68
|
context.fields = data.page.fields;
|
|
69
|
+
context.dictionary = yield loadDictionary(context.pageContext.site.name, context.pageContext.language, mode);
|
|
70
|
+
context.translate = (text) => {
|
|
71
|
+
return context.dictionary[text] || text;
|
|
72
|
+
};
|
|
69
73
|
return Object.assign(Object.assign({}, data.page), { type: "page" });
|
|
70
74
|
}));
|
|
71
75
|
function loadRouteData(params, searchParams, host) {
|
|
@@ -76,3 +80,32 @@ function loadRouteData(params, searchParams, host) {
|
|
|
76
80
|
});
|
|
77
81
|
}
|
|
78
82
|
exports.loadRouteData = loadRouteData;
|
|
83
|
+
function loadDictionary(siteName, language, mode) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
const url = process.env.LAYOUT_SERVICE_URL +
|
|
86
|
+
"/alpaca/headless/dictionary?siteName=" +
|
|
87
|
+
siteName +
|
|
88
|
+
"&language=" +
|
|
89
|
+
language;
|
|
90
|
+
console.log(`Fetching dictionary from ${url}.`);
|
|
91
|
+
const response = yield fetch(url, {
|
|
92
|
+
method: "GET",
|
|
93
|
+
credentials: "include",
|
|
94
|
+
headers: {
|
|
95
|
+
"Content-Type": "application/json",
|
|
96
|
+
Cookie: (0, headers_1.cookies)().toString(),
|
|
97
|
+
},
|
|
98
|
+
next: {
|
|
99
|
+
tags: ["dictionary"],
|
|
100
|
+
revalidate: mode === "preview" || mode == "edit" ? 15 : 600,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
if (!response.ok) {
|
|
104
|
+
console.log("Could not load dictionary: Response code: " + response.status);
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
const data = yield response.json();
|
|
108
|
+
console.log(data);
|
|
109
|
+
return data;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -63,6 +63,10 @@ const fetchRouteData = cache((path, host, mode, itemId, language) => __awaiter(v
|
|
|
63
63
|
pageState: "",
|
|
64
64
|
};
|
|
65
65
|
context.fields = data.page.fields;
|
|
66
|
+
context.dictionary = yield loadDictionary(context.pageContext.site.name, context.pageContext.language, mode);
|
|
67
|
+
context.translate = (text) => {
|
|
68
|
+
return context.dictionary[text] || text;
|
|
69
|
+
};
|
|
66
70
|
return Object.assign(Object.assign({}, data.page), { type: "page" });
|
|
67
71
|
}));
|
|
68
72
|
export function loadRouteData(params, searchParams, host) {
|
|
@@ -72,3 +76,32 @@ export function loadRouteData(params, searchParams, host) {
|
|
|
72
76
|
return result;
|
|
73
77
|
});
|
|
74
78
|
}
|
|
79
|
+
function loadDictionary(siteName, language, mode) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const url = process.env.LAYOUT_SERVICE_URL +
|
|
82
|
+
"/alpaca/headless/dictionary?siteName=" +
|
|
83
|
+
siteName +
|
|
84
|
+
"&language=" +
|
|
85
|
+
language;
|
|
86
|
+
console.log(`Fetching dictionary from ${url}.`);
|
|
87
|
+
const response = yield fetch(url, {
|
|
88
|
+
method: "GET",
|
|
89
|
+
credentials: "include",
|
|
90
|
+
headers: {
|
|
91
|
+
"Content-Type": "application/json",
|
|
92
|
+
Cookie: cookies().toString(),
|
|
93
|
+
},
|
|
94
|
+
next: {
|
|
95
|
+
tags: ["dictionary"],
|
|
96
|
+
revalidate: mode === "preview" || mode == "edit" ? 15 : 600,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
console.log("Could not load dictionary: Response code: " + response.status);
|
|
101
|
+
return {};
|
|
102
|
+
}
|
|
103
|
+
const data = yield response.json();
|
|
104
|
+
console.log(data);
|
|
105
|
+
return data;
|
|
106
|
+
});
|
|
107
|
+
}
|
package/package.json
CHANGED
package/types/alpacaContext.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
type AlpacaContext = {
|
|
2
|
+
translate: (text: string) => string;
|
|
2
3
|
componentModuleLoader: (name: string) => Promise<any>;
|
|
3
4
|
pageContext: {
|
|
4
5
|
itemId: string;
|
|
@@ -10,6 +11,7 @@ type AlpacaContext = {
|
|
|
10
11
|
};
|
|
11
12
|
};
|
|
12
13
|
fields: unknown;
|
|
14
|
+
dictionary: any;
|
|
13
15
|
};
|
|
14
16
|
export declare function useAlpacaContext(): AlpacaContext;
|
|
15
17
|
export {};
|