vike-react 0.5.2 → 0.5.3-commit-acd5466
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/+config.d.ts +10 -3
- package/dist/+config.js +10 -4
- package/dist/components/Config/Config-client.d.ts +0 -5
- package/dist/components/Config/Config-client.js +0 -6
- package/dist/components/Config/Config-server.js +0 -1
- package/dist/hooks/useConfig/configsCumulative.d.ts +2 -0
- package/dist/hooks/useConfig/configsCumulative.js +1 -0
- package/dist/hooks/useConfig/useConfig-client.d.ts +0 -5
- package/dist/hooks/useConfig/useConfig-client.js +6 -11
- package/dist/hooks/useConfig/useConfig-server.d.ts +1 -3
- package/dist/hooks/useConfig/useConfig-server.js +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/renderer/applyHeadSettings.d.ts +3 -0
- package/dist/renderer/applyHeadSettings.js +11 -0
- package/dist/renderer/getHeadSetting.d.ts +1 -2
- package/dist/renderer/getHeadSetting.js +16 -10
- package/dist/renderer/onRenderClient.js +6 -12
- package/dist/renderer/onRenderHtml.d.ts +2 -0
- package/dist/renderer/onRenderHtml.js +11 -4
- package/dist/types/Config.d.ts +11 -3
- package/dist/types/PageContext.d.ts +4 -1
- package/package.json +1 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.js +0 -2
package/dist/+config.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
+
export { config };
|
1
2
|
import { ssrEffect } from './renderer/ssrEffect.js';
|
2
|
-
|
3
|
-
declare const _default: {
|
3
|
+
declare const config: {
|
4
4
|
name: string;
|
5
5
|
require: {
|
6
6
|
vike: string;
|
@@ -88,6 +88,12 @@ declare const _default: {
|
|
88
88
|
server: true;
|
89
89
|
};
|
90
90
|
};
|
91
|
+
onAfterRenderHtml: {
|
92
|
+
env: {
|
93
|
+
server: true;
|
94
|
+
};
|
95
|
+
cumulative: true;
|
96
|
+
};
|
91
97
|
onBeforeRenderClient: {
|
92
98
|
env: {
|
93
99
|
client: true;
|
@@ -121,4 +127,5 @@ declare const _default: {
|
|
121
127
|
};
|
122
128
|
};
|
123
129
|
};
|
124
|
-
|
130
|
+
import './types/Config.js';
|
131
|
+
import './types/PageContext.js';
|
package/dist/+config.js
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
|
1
|
+
export { config };
|
2
2
|
import { ssrEffect } from './renderer/ssrEffect.js';
|
3
|
-
|
4
|
-
|
5
|
-
export default {
|
3
|
+
import { isNotFalse } from './utils/isNotFalse.js';
|
4
|
+
const config = {
|
6
5
|
name: 'vike-react',
|
7
6
|
require: {
|
8
7
|
vike: '>=0.4.182'
|
@@ -69,6 +68,10 @@ export default {
|
|
69
68
|
streamIsRequired: {
|
70
69
|
env: { server: true }
|
71
70
|
},
|
71
|
+
onAfterRenderHtml: {
|
72
|
+
env: { server: true },
|
73
|
+
cumulative: true
|
74
|
+
},
|
72
75
|
onBeforeRenderClient: {
|
73
76
|
env: { client: true },
|
74
77
|
cumulative: true
|
@@ -89,3 +92,6 @@ export default {
|
|
89
92
|
}
|
90
93
|
}
|
91
94
|
};
|
95
|
+
// This is required to make TypeScript load the global interfaces Vike.Config and Vike.PageContext so that they're always loaded: we can assume that the user always imports this file over `import vikeReact from 'vike-react/config'`
|
96
|
+
import './types/Config.js';
|
97
|
+
import './types/PageContext.js';
|
@@ -1,11 +1,5 @@
|
|
1
1
|
export { Config };
|
2
|
-
// Same as ./Config-server.ts but importing useConfig-client.js
|
3
2
|
import { useConfig } from '../../hooks/useConfig/useConfig-client.js';
|
4
|
-
/**
|
5
|
-
* Set configurations inside React components.
|
6
|
-
*
|
7
|
-
* https://vike.dev/useConfig
|
8
|
-
*/
|
9
3
|
function Config(props) {
|
10
4
|
const config = useConfig();
|
11
5
|
config(props);
|
@@ -0,0 +1 @@
|
|
1
|
+
export const configsCumulative = ['Head', 'bodyAttributes', 'htmlAttributes'];
|
@@ -1,24 +1,20 @@
|
|
1
1
|
export { useConfig };
|
2
2
|
import { usePageContext } from '../usePageContext.js';
|
3
3
|
import { getPageContext } from 'vike/getPageContext';
|
4
|
-
|
5
|
-
* Set configurations inside React components and Vike hooks.
|
6
|
-
*
|
7
|
-
* https://vike.dev/useConfig
|
8
|
-
*/
|
4
|
+
import { applyHeadSettings } from '../../renderer/applyHeadSettings.js';
|
9
5
|
function useConfig() {
|
10
6
|
// Vike hook
|
11
7
|
let pageContext = getPageContext();
|
12
8
|
if (pageContext)
|
13
9
|
return (config) => setPageContextConfigFromHook(config, pageContext);
|
14
|
-
//
|
10
|
+
// Component
|
15
11
|
pageContext = usePageContext();
|
16
12
|
return (config) => {
|
17
13
|
if (!('_headAlreadySet' in pageContext)) {
|
18
14
|
setPageContextConfigFromHook(config, pageContext);
|
19
15
|
}
|
20
16
|
else {
|
21
|
-
|
17
|
+
applyHead(config);
|
22
18
|
}
|
23
19
|
};
|
24
20
|
}
|
@@ -26,8 +22,7 @@ function setPageContextConfigFromHook(config, pageContext) {
|
|
26
22
|
pageContext._configFromHook ?? (pageContext._configFromHook = {});
|
27
23
|
Object.assign(pageContext._configFromHook, config);
|
28
24
|
}
|
29
|
-
function
|
30
|
-
const { title } = config;
|
31
|
-
|
32
|
-
window.document.title = title;
|
25
|
+
function applyHead(config) {
|
26
|
+
const { title, lang } = config;
|
27
|
+
applyHeadSettings(title, lang);
|
33
28
|
}
|
@@ -1,10 +1,8 @@
|
|
1
1
|
export { useConfig };
|
2
|
-
export type ConfigFromHookCumulative = (typeof configsCumulative)[number];
|
3
2
|
import type { ConfigFromHook } from '../../types/Config.js';
|
4
3
|
/**
|
5
|
-
* Set configurations inside
|
4
|
+
* Set configurations inside components and Vike hooks.
|
6
5
|
*
|
7
6
|
* https://vike.dev/useConfig
|
8
7
|
*/
|
9
8
|
declare function useConfig(): (config: ConfigFromHook) => void;
|
10
|
-
declare const configsCumulative: readonly ["Head", "bodyAttributes", "htmlAttributes"];
|
@@ -4,8 +4,9 @@ import { getPageContext } from 'vike/getPageContext';
|
|
4
4
|
import { useStream } from 'react-streaming';
|
5
5
|
import { objectKeys } from '../../utils/objectKeys.js';
|
6
6
|
import { includes } from '../../utils/includes.js';
|
7
|
+
import { configsCumulative } from './configsCumulative.js';
|
7
8
|
/**
|
8
|
-
* Set configurations inside
|
9
|
+
* Set configurations inside components and Vike hooks.
|
9
10
|
*
|
10
11
|
* https://vike.dev/useConfig
|
11
12
|
*/
|
@@ -14,7 +15,7 @@ function useConfig() {
|
|
14
15
|
let pageContext = getPageContext();
|
15
16
|
if (pageContext)
|
16
17
|
return (config) => setPageContextConfigFromHook(config, pageContext);
|
17
|
-
//
|
18
|
+
// Component
|
18
19
|
pageContext = usePageContext();
|
19
20
|
const stream = useStream();
|
20
21
|
return (config) => {
|
@@ -28,7 +29,6 @@ function useConfig() {
|
|
28
29
|
};
|
29
30
|
}
|
30
31
|
const configsClientSide = ['title'];
|
31
|
-
const configsCumulative = ['Head', 'bodyAttributes', 'htmlAttributes'];
|
32
32
|
function setPageContextConfigFromHook(config, pageContext) {
|
33
33
|
pageContext._configFromHook ?? (pageContext._configFromHook = {});
|
34
34
|
objectKeys(config).forEach((configName) => {
|
package/dist/index.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export { default } from './+config.js';
|
1
|
+
export { config as default } from './+config.js';
|
package/dist/index.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
// TODO/next-major-release: remove this file/export
|
2
2
|
console.warn("[vike-react][warning][deprecation] Replace `import vikeReact from 'vike-react'` with `import vikeReact from 'vike-react/config'` (typically in your /pages/+config.js)");
|
3
|
-
export { default } from './+config.js';
|
3
|
+
export { config as default } from './+config.js';
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export { applyHeadSettings };
|
2
|
+
// - We skip if `undefined` as we shouldn't remove values set by the Head setting.
|
3
|
+
// - Setting a default prevents the previous value to be leaked: upon client-side navigation, the value set by the previous page won't be removed if the next page doesn't override it.
|
4
|
+
// - Most of the time, the user sets a default himself (i.e. a value defined at /pages/+config.js)
|
5
|
+
// - If he doesn't have a default then he can use `null` to opt into Vike's defaults
|
6
|
+
function applyHeadSettings(title, lang) {
|
7
|
+
if (title !== undefined)
|
8
|
+
document.title = title || '';
|
9
|
+
if (lang !== undefined)
|
10
|
+
document.documentElement.lang = lang || 'en';
|
11
|
+
}
|
@@ -2,5 +2,4 @@ export { getHeadSetting };
|
|
2
2
|
import type { PageContext } from 'vike/types';
|
3
3
|
import type { PageContextInternal } from '../types/PageContext.js';
|
4
4
|
import type { ConfigFromHookResolved } from '../types/Config.js';
|
5
|
-
|
6
|
-
declare function getHeadSetting<T>(headSetting: HeadSetting, pageContext: PageContext & PageContextInternal): undefined | T;
|
5
|
+
declare function getHeadSetting<T>(configName: keyof ConfigFromHookResolved, pageContext: PageContext & PageContextInternal): undefined | T;
|
@@ -1,18 +1,24 @@
|
|
1
1
|
export { getHeadSetting };
|
2
2
|
import { isCallable } from '../utils/isCallable.js';
|
3
|
-
|
3
|
+
import { configsCumulative } from '../hooks/useConfig/configsCumulative.js';
|
4
|
+
import { includes } from '../utils/includes.js';
|
5
|
+
// We use `any` instead of doing proper validation in order to save KBs sent to the client-side.
|
6
|
+
function getHeadSetting(configName, pageContext) {
|
4
7
|
// Set by useConfig()
|
5
|
-
|
6
|
-
const val = pageContext._configFromHook?.[headSetting];
|
7
|
-
if (val !== undefined)
|
8
|
-
return val;
|
9
|
-
}
|
8
|
+
const valFromUseConfig = pageContext._configFromHook?.[configName];
|
10
9
|
// Set by +configName.js
|
11
|
-
const
|
12
|
-
|
13
|
-
|
10
|
+
const valFromConfig = pageContext.config[configName];
|
11
|
+
const getCallable = (val) => (isCallable(val) ? val(pageContext) : val);
|
12
|
+
if (!includes(configsCumulative, configName)) {
|
13
|
+
if (valFromUseConfig !== undefined)
|
14
|
+
return valFromUseConfig;
|
15
|
+
return getCallable(valFromConfig);
|
14
16
|
}
|
15
17
|
else {
|
16
|
-
return
|
18
|
+
return [
|
19
|
+
//
|
20
|
+
...(valFromConfig ?? []).map(getCallable),
|
21
|
+
...(valFromUseConfig ?? [])
|
22
|
+
];
|
17
23
|
}
|
18
24
|
}
|
@@ -5,8 +5,10 @@ import { getHeadSetting } from './getHeadSetting.js';
|
|
5
5
|
import { getPageElement } from './getPageElement.js';
|
6
6
|
import './styles.css';
|
7
7
|
import { callCumulativeHooks } from '../utils/callCumulativeHooks.js';
|
8
|
+
import { applyHeadSettings } from './applyHeadSettings.js';
|
8
9
|
let root;
|
9
10
|
const onRenderClient = async (pageContext) => {
|
11
|
+
pageContext._headAlreadySet = pageContext.isHydration;
|
10
12
|
// Use case:
|
11
13
|
// - Store hydration https://github.com/vikejs/vike-react/issues/110
|
12
14
|
await callCumulativeHooks(pageContext.config.onBeforeRenderClient, pageContext);
|
@@ -38,24 +40,16 @@ const onRenderClient = async (pageContext) => {
|
|
38
40
|
}
|
39
41
|
pageContext.root = root;
|
40
42
|
if (!pageContext.isHydration) {
|
41
|
-
|
42
|
-
|
43
|
+
pageContext._headAlreadySet = true;
|
44
|
+
applyHead(pageContext);
|
43
45
|
}
|
44
46
|
// Use cases:
|
45
47
|
// - Custom user settings: https://vike.dev/head-tags#custom-settings
|
46
48
|
// - Testing tools: https://github.com/vikejs/vike-react/issues/95
|
47
49
|
await callCumulativeHooks(pageContext.config.onAfterRenderClient, pageContext);
|
48
50
|
};
|
49
|
-
function
|
50
|
-
pageContext._headAlreadySet = true;
|
51
|
+
function applyHead(pageContext) {
|
51
52
|
const title = getHeadSetting('title', pageContext);
|
52
53
|
const lang = getHeadSetting('lang', pageContext);
|
53
|
-
|
54
|
-
// - Setting a default prevents the previous value to be leaked: upon client-side navigation, the value set by the previous page won't be removed if the next page doesn't override it.
|
55
|
-
// - Most of the time, the user sets a default himself (i.e. a value defined at /pages/+config.js)
|
56
|
-
// - If he doesn't have a default then he can use `null` to opt into Vike's defaults
|
57
|
-
if (title !== undefined)
|
58
|
-
document.title = title || '';
|
59
|
-
if (lang !== undefined)
|
60
|
-
document.documentElement.lang = lang || 'en';
|
54
|
+
applyHeadSettings(title, lang);
|
61
55
|
}
|
@@ -1,4 +1,6 @@
|
|
1
1
|
export { onRenderHtml };
|
2
|
+
import { renderToStream } from 'react-streaming/server';
|
2
3
|
import type { OnRenderHtmlAsync } from 'vike/types';
|
3
4
|
declare const onRenderHtml: OnRenderHtmlAsync;
|
5
|
+
export type PageHtmlStream = Awaited<ReturnType<typeof renderToStream>>;
|
4
6
|
export type Viewport = 'responsive' | number | null;
|
@@ -9,11 +9,14 @@ import { getHeadSetting } from './getHeadSetting.js';
|
|
9
9
|
import { getPageElement } from './getPageElement.js';
|
10
10
|
import { isReactElement } from '../utils/isReactElement.js';
|
11
11
|
import { getTagAttributesString } from '../utils/getTagAttributesString.js';
|
12
|
+
import { callCumulativeHooks } from '../utils/callCumulativeHooks.js';
|
12
13
|
addEcosystemStamp();
|
13
14
|
const onRenderHtml = async (pageContext) => {
|
14
15
|
const pageHtml = await getPageHtml(pageContext);
|
15
16
|
const headHtml = getHeadHtml(pageContext);
|
16
17
|
const { htmlAttributesString, bodyAttributesString } = getTagAttributes(pageContext);
|
18
|
+
// Not needed on the client-side, thus we remove it to save KBs sent to the client
|
19
|
+
delete pageContext._configFromHook;
|
17
20
|
return escapeInject `<!DOCTYPE html>
|
18
21
|
<html${dangerouslySkipEscape(htmlAttributesString)}>
|
19
22
|
<head>
|
@@ -34,11 +37,13 @@ async function getPageHtml(pageContext) {
|
|
34
37
|
const page = getPageElement(pageContext);
|
35
38
|
const { stream, streamIsRequired } = pageContext.config;
|
36
39
|
if (!stream && !streamIsRequired) {
|
37
|
-
|
40
|
+
const pageHtmlString = renderToString(page);
|
41
|
+
pageContext.pageHtmlString = pageHtmlString;
|
42
|
+
pageHtml = dangerouslySkipEscape(pageHtmlString);
|
38
43
|
}
|
39
44
|
else {
|
40
45
|
const disable = stream === false ? true : undefined;
|
41
|
-
|
46
|
+
const pageHtmlStream = await renderToStream(page, {
|
42
47
|
webStream: typeof stream === 'string' ? stream === 'web' : undefined,
|
43
48
|
userAgent: pageContext.headers?.['user-agent'] ||
|
44
49
|
// TODO/eventually: remove old way of acccessing the User Agent header.
|
@@ -46,8 +51,12 @@ async function getPageHtml(pageContext) {
|
|
46
51
|
pageContext.userAgent,
|
47
52
|
disable
|
48
53
|
});
|
54
|
+
pageContext.pageHtmlStream = pageHtmlStream;
|
55
|
+
pageHtml = pageHtmlStream;
|
49
56
|
}
|
50
57
|
}
|
58
|
+
// https://github.com/vikejs/vike/discussions/1804#discussioncomment-10394481
|
59
|
+
await callCumulativeHooks(pageContext.config.onAfterRenderHtml, pageContext);
|
51
60
|
return pageHtml;
|
52
61
|
}
|
53
62
|
function getHeadHtml(pageContext) {
|
@@ -74,8 +83,6 @@ function getHeadHtml(pageContext) {
|
|
74
83
|
.filter((Head) => Head !== null && Head !== undefined)
|
75
84
|
.map((Head) => getHeadElementHtml(Head, pageContext))
|
76
85
|
.join('\n'));
|
77
|
-
// Not needed on the client-side, thus we remove it to save KBs sent to the client
|
78
|
-
delete pageContext._configFromHook;
|
79
86
|
const headHtml = escapeInject `
|
80
87
|
${titleTags}
|
81
88
|
${viewportTag}
|
package/dist/types/Config.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import type { ImportString,
|
1
|
+
import type { ImportString, PageContextServer, PageContext, PageContextClient } from 'vike/types';
|
2
2
|
import type { TagAttributes } from '../utils/getTagAttributesString.js';
|
3
3
|
import type { Viewport } from '../renderer/onRenderHtml.js';
|
4
|
-
import type {
|
4
|
+
import type { ConfigsCumulative } from '../hooks/useConfig/configsCumulative.js';
|
5
5
|
declare global {
|
6
6
|
namespace Vike {
|
7
7
|
interface Config {
|
@@ -152,6 +152,12 @@ declare global {
|
|
152
152
|
* https://vike.dev/reactStrictMode
|
153
153
|
*/
|
154
154
|
reactStrictMode?: boolean;
|
155
|
+
/**
|
156
|
+
* Server-side hook called right after rendering the page's root React component to HTML.
|
157
|
+
*
|
158
|
+
* https://vike.dev/onAfterRenderHtml
|
159
|
+
*/
|
160
|
+
onAfterRenderHtml?: (pageContext: PageContextServer) => void;
|
155
161
|
/**
|
156
162
|
* Client-side hook called before the page is rendered.
|
157
163
|
*
|
@@ -177,11 +183,13 @@ declare global {
|
|
177
183
|
Head?: Head[];
|
178
184
|
bodyAttributes?: TagAttributes[];
|
179
185
|
htmlAttributes?: TagAttributes[];
|
186
|
+
onAfterRenderHtml?: Function[];
|
180
187
|
onBeforeRenderClient?: Function[];
|
181
188
|
onAfterRenderClient?: Function[];
|
182
189
|
}
|
183
190
|
}
|
184
191
|
}
|
192
|
+
type PageContext_ = PageContext;
|
185
193
|
export type Head = React.ReactNode | (() => React.ReactNode);
|
186
194
|
type Wrapper = (props: {
|
187
195
|
children: React.ReactNode;
|
@@ -195,5 +203,5 @@ type PickWithoutGetter<T, K extends keyof T> = {
|
|
195
203
|
[P in K]: Exclude<T[P], Function>;
|
196
204
|
};
|
197
205
|
export type ConfigFromHook = PickWithoutGetter<Vike.Config, 'Head' | 'title' | 'description' | 'image' | 'favicon' | 'lang' | 'viewport' | 'bodyAttributes' | 'htmlAttributes'>;
|
198
|
-
export type ConfigFromHookResolved = Omit<ConfigFromHook,
|
206
|
+
export type ConfigFromHookResolved = Omit<ConfigFromHook, ConfigsCumulative> & Pick<Vike.ConfigResolved, ConfigsCumulative>;
|
199
207
|
export {};
|
@@ -2,6 +2,7 @@ import type React from 'react';
|
|
2
2
|
import type { JSX } from 'react';
|
3
3
|
import type ReactDOM from 'react-dom/client';
|
4
4
|
import type { ConfigFromHookResolved } from './Config.js';
|
5
|
+
import type { PageHtmlStream } from '../renderer/onRenderHtml.js';
|
5
6
|
declare global {
|
6
7
|
namespace Vike {
|
7
8
|
interface PageContext {
|
@@ -11,10 +12,12 @@ declare global {
|
|
11
12
|
page?: JSX.Element;
|
12
13
|
/** The React root DOM container */
|
13
14
|
root?: ReactDOM.Root;
|
15
|
+
pageHtmlString?: string;
|
16
|
+
pageHtmlStream?: PageHtmlStream;
|
14
17
|
}
|
15
18
|
}
|
16
19
|
}
|
17
20
|
export type PageContextInternal = {
|
18
21
|
_configFromHook?: ConfigFromHookResolved;
|
19
|
-
_headAlreadySet?:
|
22
|
+
_headAlreadySet?: boolean;
|
20
23
|
};
|
package/package.json
CHANGED
package/dist/types/index.d.ts
DELETED
package/dist/types/index.js
DELETED