vike-react 0.6.10 → 0.6.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/components/Config/Config-client.d.ts +2 -2
- package/dist/components/Config/Config-server.d.ts +2 -2
- package/dist/config.d.ts +14 -0
- package/dist/config.js +11 -1
- package/dist/hooks/useConfig/useConfig-client.d.ts +2 -2
- package/dist/hooks/useConfig/useConfig-client.js +5 -5
- package/dist/hooks/useConfig/useConfig-server.d.ts +2 -2
- package/dist/hooks/useConfig/useConfig-server.js +7 -7
- package/dist/integration/getHeadSetting.d.ts +2 -2
- package/dist/integration/getHeadSetting.js +4 -4
- package/dist/integration/onRenderHtml.d.ts +1 -0
- package/dist/integration/onRenderHtml.js +18 -7
- package/dist/types/Config.d.ts +21 -8
- package/dist/types/PageContext.d.ts +2 -2
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Config };
|
|
2
|
-
import type {
|
|
3
|
-
declare function Config(props:
|
|
2
|
+
import type { ConfigViaHook } from '../../types/Config.js';
|
|
3
|
+
declare function Config(props: ConfigViaHook): null;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { Config };
|
|
2
|
-
import type {
|
|
2
|
+
import type { ConfigViaHook } from '../../types/Config.js';
|
|
3
3
|
/**
|
|
4
4
|
* Set configurations inside React components.
|
|
5
5
|
*
|
|
6
6
|
* https://vike.dev/useConfig
|
|
7
7
|
*/
|
|
8
|
-
declare function Config(props:
|
|
8
|
+
declare function Config(props: ConfigViaHook): null;
|
package/dist/config.d.ts
CHANGED
|
@@ -72,6 +72,20 @@ declare const config: {
|
|
|
72
72
|
cumulative: true;
|
|
73
73
|
global: true;
|
|
74
74
|
};
|
|
75
|
+
headHtmlBegin: {
|
|
76
|
+
env: {
|
|
77
|
+
server: true;
|
|
78
|
+
};
|
|
79
|
+
cumulative: true;
|
|
80
|
+
global: true;
|
|
81
|
+
};
|
|
82
|
+
headHtmlEnd: {
|
|
83
|
+
env: {
|
|
84
|
+
server: true;
|
|
85
|
+
};
|
|
86
|
+
cumulative: true;
|
|
87
|
+
global: true;
|
|
88
|
+
};
|
|
75
89
|
htmlAttributes: {
|
|
76
90
|
env: {
|
|
77
91
|
server: true;
|
package/dist/config.js
CHANGED
|
@@ -14,7 +14,7 @@ const config = {
|
|
|
14
14
|
onRenderClient: 'import:vike-react/__internal/integration/onRenderClient:onRenderClient',
|
|
15
15
|
// @eject-remove end
|
|
16
16
|
passToClient: [
|
|
17
|
-
'
|
|
17
|
+
'_configViaHook',
|
|
18
18
|
// https://github.com/vikejs/vike-react/issues/25
|
|
19
19
|
process.env.NODE_ENV !== 'production' && '$$typeof',
|
|
20
20
|
].filter(isNotFalse),
|
|
@@ -60,6 +60,16 @@ const config = {
|
|
|
60
60
|
cumulative: true,
|
|
61
61
|
global: true,
|
|
62
62
|
},
|
|
63
|
+
headHtmlBegin: {
|
|
64
|
+
env: { server: true },
|
|
65
|
+
cumulative: true,
|
|
66
|
+
global: true,
|
|
67
|
+
},
|
|
68
|
+
headHtmlEnd: {
|
|
69
|
+
env: { server: true },
|
|
70
|
+
cumulative: true,
|
|
71
|
+
global: true,
|
|
72
|
+
},
|
|
63
73
|
htmlAttributes: {
|
|
64
74
|
env: { server: true },
|
|
65
75
|
global: true,
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { useConfig };
|
|
2
|
-
import type {
|
|
3
|
-
declare function useConfig(): (config:
|
|
2
|
+
import type { ConfigViaHook } from '../../types/Config.js';
|
|
3
|
+
declare function useConfig(): (config: ConfigViaHook) => void;
|
|
@@ -6,21 +6,21 @@ function useConfig() {
|
|
|
6
6
|
// Vike hook
|
|
7
7
|
let pageContext = getPageContext();
|
|
8
8
|
if (pageContext)
|
|
9
|
-
return (config) =>
|
|
9
|
+
return (config) => setPageContextConfigViaHook(config, pageContext);
|
|
10
10
|
// Component
|
|
11
11
|
pageContext = usePageContext();
|
|
12
12
|
return (config) => {
|
|
13
13
|
if (!('_headAlreadySet' in pageContext)) {
|
|
14
|
-
|
|
14
|
+
setPageContextConfigViaHook(config, pageContext);
|
|
15
15
|
}
|
|
16
16
|
else {
|
|
17
17
|
applyHead(config);
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
function
|
|
22
|
-
pageContext.
|
|
23
|
-
Object.assign(pageContext.
|
|
21
|
+
function setPageContextConfigViaHook(config, pageContext) {
|
|
22
|
+
pageContext._configViaHook ?? (pageContext._configViaHook = {});
|
|
23
|
+
Object.assign(pageContext._configViaHook, config);
|
|
24
24
|
}
|
|
25
25
|
function applyHead(config) {
|
|
26
26
|
const { title, lang } = config;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { useConfig };
|
|
2
|
-
import type {
|
|
2
|
+
import type { ConfigViaHook } from '../../types/Config.js';
|
|
3
3
|
/**
|
|
4
4
|
* Set configurations inside components and Vike hooks.
|
|
5
5
|
*
|
|
6
6
|
* https://vike.dev/useConfig
|
|
7
7
|
*/
|
|
8
|
-
declare function useConfig(): (config:
|
|
8
|
+
declare function useConfig(): (config: ConfigViaHook) => void;
|
|
@@ -15,13 +15,13 @@ function useConfig() {
|
|
|
15
15
|
// Vike hook
|
|
16
16
|
let pageContext = getPageContext();
|
|
17
17
|
if (pageContext)
|
|
18
|
-
return (config) =>
|
|
18
|
+
return (config) => setPageContextConfigViaHook(config, pageContext);
|
|
19
19
|
// Component
|
|
20
20
|
pageContext = usePageContext();
|
|
21
21
|
const stream = useStreamOptional();
|
|
22
22
|
return (config) => {
|
|
23
23
|
if (!pageContext._headAlreadySet) {
|
|
24
|
-
|
|
24
|
+
setPageContextConfigViaHook(config, pageContext);
|
|
25
25
|
}
|
|
26
26
|
else {
|
|
27
27
|
assert(stream);
|
|
@@ -31,8 +31,8 @@ function useConfig() {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
const configsClientSide = ['title'];
|
|
34
|
-
function
|
|
35
|
-
pageContext.
|
|
34
|
+
function setPageContextConfigViaHook(config, pageContext) {
|
|
35
|
+
pageContext._configViaHook ?? (pageContext._configViaHook = {});
|
|
36
36
|
objectKeys(config).forEach((configName) => {
|
|
37
37
|
var _a;
|
|
38
38
|
// Skip HTML only configs which the client-side doesn't need, saving KBs sent to the client as well as avoiding serialization errors.
|
|
@@ -43,15 +43,15 @@ function setPageContextConfigFromHook(config, pageContext) {
|
|
|
43
43
|
const configValue = config[configName];
|
|
44
44
|
if (configValue === undefined)
|
|
45
45
|
return;
|
|
46
|
-
pageContext.
|
|
46
|
+
pageContext._configViaHook[configName] = configValue;
|
|
47
47
|
}
|
|
48
48
|
else {
|
|
49
49
|
// Cumulative config
|
|
50
50
|
const configValue = config[configName];
|
|
51
51
|
if (!configValue)
|
|
52
52
|
return;
|
|
53
|
-
(_a = pageContext.
|
|
54
|
-
pageContext.
|
|
53
|
+
(_a = pageContext._configViaHook)[configName] ?? (_a[configName] = []);
|
|
54
|
+
pageContext._configViaHook[configName].push(configValue);
|
|
55
55
|
}
|
|
56
56
|
});
|
|
57
57
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { getHeadSetting };
|
|
2
2
|
import type { PageContext } from 'vike/types';
|
|
3
3
|
import type { PageContextInternal } from '../types/PageContext.js';
|
|
4
|
-
import type {
|
|
5
|
-
declare function getHeadSetting<T>(configName: keyof
|
|
4
|
+
import type { ConfigViaHookResolved } from '../types/Config.js';
|
|
5
|
+
declare function getHeadSetting<T>(configName: keyof ConfigViaHookResolved, pageContext: PageContext & PageContextInternal): undefined | T;
|
|
@@ -5,20 +5,20 @@ import { includes } from '../utils/includes.js';
|
|
|
5
5
|
// We use `any` instead of doing proper validation in order to save KBs sent to the client-side.
|
|
6
6
|
function getHeadSetting(configName, pageContext) {
|
|
7
7
|
// Set by useConfig()
|
|
8
|
-
const
|
|
8
|
+
const valFromHook = pageContext._configViaHook?.[configName];
|
|
9
9
|
// Set by +configName.js
|
|
10
10
|
const valFromConfig = pageContext.config[configName];
|
|
11
11
|
const getCallable = (val) => (isCallable(val) ? val(pageContext) : val);
|
|
12
12
|
if (!includes(configsCumulative, configName)) {
|
|
13
|
-
if (
|
|
14
|
-
return
|
|
13
|
+
if (valFromHook !== undefined)
|
|
14
|
+
return valFromHook;
|
|
15
15
|
return getCallable(valFromConfig);
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
18
18
|
return [
|
|
19
19
|
//
|
|
20
20
|
...(valFromConfig ?? []).map(getCallable),
|
|
21
|
-
...(
|
|
21
|
+
...(valFromHook ?? []),
|
|
22
22
|
];
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -6,3 +6,4 @@ import type { PageContextInternal } from '../types/PageContext.js';
|
|
|
6
6
|
declare function onRenderHtml(pageContext: PageContextServer & PageContextInternal): Promise<ReturnType<typeof escapeInject>>;
|
|
7
7
|
export type PageHtmlStream = Awaited<ReturnType<typeof renderToStream>>;
|
|
8
8
|
export type Viewport = 'responsive' | number | null;
|
|
9
|
+
export type HtmlInjection = string | ((pageContext: PageContextServer) => string);
|
|
@@ -19,10 +19,10 @@ addEcosystemStamp();
|
|
|
19
19
|
async function onRenderHtml(pageContext) {
|
|
20
20
|
await renderPageToHtml(pageContext);
|
|
21
21
|
const headHtml = getHeadHtml(pageContext);
|
|
22
|
-
const { bodyHtmlBegin, bodyHtmlEnd } = await
|
|
22
|
+
const { headHtmlBegin, headHtmlEnd, bodyHtmlBegin, bodyHtmlEnd } = await getHtmlInjections(pageContext);
|
|
23
23
|
const { htmlAttributesString, bodyAttributesString } = getTagAttributes(pageContext);
|
|
24
24
|
// Not needed on the client-side, thus we remove it to save KBs sent to the client
|
|
25
|
-
delete pageContext.
|
|
25
|
+
delete pageContext._configViaHook;
|
|
26
26
|
// pageContext.{pageHtmlString,pageHtmlStream} is set by renderPageToHtml() and can be overridden by user at onAfterRenderHtml()
|
|
27
27
|
let pageHtmlStringOrStream =
|
|
28
28
|
// Set to empty string if SSR is disabled
|
|
@@ -39,7 +39,9 @@ async function onRenderHtml(pageContext) {
|
|
|
39
39
|
<html${dangerouslySkipEscape(htmlAttributesString)}>
|
|
40
40
|
<head>
|
|
41
41
|
<meta charset="UTF-8" />
|
|
42
|
+
${headHtmlBegin}
|
|
42
43
|
${headHtml}
|
|
44
|
+
${headHtmlEnd}
|
|
43
45
|
</head>
|
|
44
46
|
<body${dangerouslySkipEscape(bodyAttributesString)}>
|
|
45
47
|
${bodyHtmlBegin}
|
|
@@ -107,7 +109,7 @@ function getHeadHtml(pageContext) {
|
|
|
107
109
|
// Added by +Head
|
|
108
110
|
...(pageContext.config.Head ?? []),
|
|
109
111
|
// Added by useConfig()
|
|
110
|
-
...(pageContext.
|
|
112
|
+
...(pageContext._configViaHook?.Head ?? []),
|
|
111
113
|
]
|
|
112
114
|
.filter((Head) => Head !== null && Head !== undefined)
|
|
113
115
|
.map((Head) => getHeadElementHtml(Head, pageContext))
|
|
@@ -175,10 +177,19 @@ function addEcosystemStamp() {
|
|
|
175
177
|
// We use an object so that we can eventually, in the future, add helpful information as needed. (E.g. the vike-react version.)
|
|
176
178
|
{};
|
|
177
179
|
}
|
|
178
|
-
async function
|
|
179
|
-
const
|
|
180
|
-
const
|
|
181
|
-
|
|
180
|
+
async function getHtmlInjections(pageContext) {
|
|
181
|
+
const { config } = pageContext;
|
|
182
|
+
const renderHooks = async (hooks) => {
|
|
183
|
+
const values = await callCumulativeHooks(hooks, pageContext);
|
|
184
|
+
return dangerouslySkipEscape(values.join(''));
|
|
185
|
+
};
|
|
186
|
+
const [headHtmlBegin, headHtmlEnd, bodyHtmlBegin, bodyHtmlEnd] = await Promise.all([
|
|
187
|
+
renderHooks(config.headHtmlBegin),
|
|
188
|
+
renderHooks(config.headHtmlEnd),
|
|
189
|
+
renderHooks(config.bodyHtmlBegin),
|
|
190
|
+
renderHooks(config.bodyHtmlEnd),
|
|
191
|
+
]);
|
|
192
|
+
return { bodyHtmlBegin, bodyHtmlEnd, headHtmlBegin, headHtmlEnd };
|
|
182
193
|
}
|
|
183
194
|
function resolveStreamSetting(pageContext) {
|
|
184
195
|
const { stream,
|
package/dist/types/Config.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ImportString, PageContext } from 'vike/types';
|
|
2
2
|
import type { TagAttributes } from '../utils/getTagAttributesString.js';
|
|
3
|
-
import type { Viewport } from '../integration/onRenderHtml.js';
|
|
3
|
+
import type { Viewport, HtmlInjection } from '../integration/onRenderHtml.js';
|
|
4
4
|
import type { ConfigsCumulative } from '../hooks/useConfig/configsCumulative.js';
|
|
5
5
|
import type React from 'react';
|
|
6
6
|
import type { HydrationOptions, RootOptions } from 'react-dom/client';
|
|
@@ -110,13 +110,25 @@ declare global {
|
|
|
110
110
|
*
|
|
111
111
|
* https://vike.dev/bodyHtmlBegin
|
|
112
112
|
*/
|
|
113
|
-
bodyHtmlBegin?:
|
|
113
|
+
bodyHtmlBegin?: HtmlInjection;
|
|
114
114
|
/**
|
|
115
115
|
* Raw HTML injected at the end of `<body>`.
|
|
116
116
|
*
|
|
117
117
|
* https://vike.dev/bodyHtmlEnd
|
|
118
118
|
*/
|
|
119
|
-
bodyHtmlEnd?:
|
|
119
|
+
bodyHtmlEnd?: HtmlInjection;
|
|
120
|
+
/**
|
|
121
|
+
* Raw HTML injected at the start of `<head>`.
|
|
122
|
+
*
|
|
123
|
+
* https://vike.dev/headHtmlBegin
|
|
124
|
+
*/
|
|
125
|
+
headHtmlBegin?: HtmlInjection;
|
|
126
|
+
/**
|
|
127
|
+
* Raw HTML injected at the end of `<head>`.
|
|
128
|
+
*
|
|
129
|
+
* https://vike.dev/headHtmlEnd
|
|
130
|
+
*/
|
|
131
|
+
headHtmlEnd?: HtmlInjection;
|
|
120
132
|
/**
|
|
121
133
|
* Add tag attributes such as `<html class="dark">`.
|
|
122
134
|
*
|
|
@@ -219,8 +231,10 @@ declare global {
|
|
|
219
231
|
Wrapper?: Wrapper[];
|
|
220
232
|
Layout?: Layout[];
|
|
221
233
|
Head?: Head[];
|
|
222
|
-
bodyHtmlBegin?:
|
|
223
|
-
bodyHtmlEnd?:
|
|
234
|
+
bodyHtmlBegin?: HtmlInjection[];
|
|
235
|
+
bodyHtmlEnd?: HtmlInjection[];
|
|
236
|
+
headHtmlBegin?: HtmlInjection[];
|
|
237
|
+
headHtmlEnd?: HtmlInjection[];
|
|
224
238
|
bodyAttributes?: TagAttributes[];
|
|
225
239
|
htmlAttributes?: TagAttributes[];
|
|
226
240
|
onBeforeRenderHtml?: Function[];
|
|
@@ -233,7 +247,6 @@ declare global {
|
|
|
233
247
|
}
|
|
234
248
|
}
|
|
235
249
|
type PageContext_ = PageContext;
|
|
236
|
-
type BodyHtmlBoundary = string | ((pageContext: PageContext) => string);
|
|
237
250
|
export type Head = React.ReactNode | (() => React.ReactNode);
|
|
238
251
|
type Wrapper = (props: {
|
|
239
252
|
children: React.ReactNode;
|
|
@@ -246,8 +259,8 @@ type Loading = {
|
|
|
246
259
|
type PickWithoutGetter<T, K extends keyof T> = {
|
|
247
260
|
[P in K]: Exclude<T[P], Function>;
|
|
248
261
|
};
|
|
249
|
-
export type
|
|
250
|
-
export type
|
|
262
|
+
export type ConfigViaHook = PickWithoutGetter<Vike.Config, 'Head' | 'title' | 'description' | 'image' | 'favicon' | 'lang' | 'viewport' | 'bodyAttributes' | 'htmlAttributes'>;
|
|
263
|
+
export type ConfigViaHookResolved = Omit<ConfigViaHook, ConfigsCumulative> & Pick<Vike.ConfigResolved, ConfigsCumulative>;
|
|
251
264
|
export type ReactOptions = {
|
|
252
265
|
hydrateRootOptions?: HydrationOptions;
|
|
253
266
|
createRootOptions?: RootOptions;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
2
|
import type ReactDOM from 'react-dom/client';
|
|
3
|
-
import type {
|
|
3
|
+
import type { ConfigViaHookResolved } from './Config.js';
|
|
4
4
|
import type { PageHtmlStream } from '../integration/onRenderHtml.js';
|
|
5
5
|
declare global {
|
|
6
6
|
namespace Vike {
|
|
@@ -19,6 +19,6 @@ declare global {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
export type PageContextInternal = {
|
|
22
|
-
|
|
22
|
+
_configViaHook?: ConfigViaHookResolved;
|
|
23
23
|
_headAlreadySet?: boolean;
|
|
24
24
|
};
|