vike-react 0.5.2-commit-a53b7c1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
+ export { config };
1
2
  import { ssrEffect } from './renderer/ssrEffect.js';
2
- import './types/index.js';
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
- export default _default;
130
+ import './types/Config.js';
131
+ import './types/PageContext.js';
package/dist/+config.js CHANGED
@@ -1,8 +1,7 @@
1
- import { isNotFalse } from './utils/isNotFalse.js';
1
+ export { config };
2
2
  import { ssrEffect } from './renderer/ssrEffect.js';
3
- // This is required to make TypeScript load the global interfaces such as 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'`
4
- import './types/index.js';
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,8 +1,3 @@
1
1
  export { Config };
2
2
  import type { ConfigFromHook } from '../../types/Config.js';
3
- /**
4
- * Set configurations inside React components.
5
- *
6
- * https://vike.dev/useConfig
7
- */
8
3
  declare function Config(props: ConfigFromHook): null;
@@ -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);
@@ -1,5 +1,4 @@
1
1
  export { Config };
2
- // Same as ./Config-client.ts but importing useConfig-server.js
3
2
  import { useConfig } from '../../hooks/useConfig/useConfig-server.js';
4
3
  /**
5
4
  * Set configurations inside React components.
@@ -0,0 +1,2 @@
1
+ export declare const configsCumulative: readonly ["Head", "bodyAttributes", "htmlAttributes"];
2
+ export type ConfigsCumulative = (typeof configsCumulative)[number];
@@ -0,0 +1 @@
1
+ export const configsCumulative = ['Head', 'bodyAttributes', 'htmlAttributes'];
@@ -1,6 +1,7 @@
1
1
  export { useConfig };
2
2
  import { usePageContext } from '../usePageContext.js';
3
3
  import { getPageContext } from 'vike/getPageContext';
4
+ import { applyHeadSettings } from '../../renderer/applyHeadSettings.js';
4
5
  function useConfig() {
5
6
  // Vike hook
6
7
  let pageContext = getPageContext();
@@ -13,7 +14,7 @@ function useConfig() {
13
14
  setPageContextConfigFromHook(config, pageContext);
14
15
  }
15
16
  else {
16
- apply(config);
17
+ applyHead(config);
17
18
  }
18
19
  };
19
20
  }
@@ -21,8 +22,7 @@ function setPageContextConfigFromHook(config, pageContext) {
21
22
  pageContext._configFromHook ?? (pageContext._configFromHook = {});
22
23
  Object.assign(pageContext._configFromHook, config);
23
24
  }
24
- function apply(config) {
25
- const { title } = config;
26
- if (title)
27
- window.document.title = title;
25
+ function applyHead(config) {
26
+ const { title, lang } = config;
27
+ applyHeadSettings(title, lang);
28
28
  }
@@ -1,5 +1,4 @@
1
1
  export { useConfig };
2
- export type ConfigFromHookCumulative = (typeof configsCumulative)[number];
3
2
  import type { ConfigFromHook } from '../../types/Config.js';
4
3
  /**
5
4
  * Set configurations inside components and Vike hooks.
@@ -7,4 +6,3 @@ import type { ConfigFromHook } from '../../types/Config.js';
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,6 +4,7 @@ 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
9
  * Set configurations inside components and Vike hooks.
9
10
  *
@@ -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,3 @@
1
+ export { applyHeadSettings };
2
+ type Value = string | null | undefined;
3
+ declare function applyHeadSettings(title: Value, lang: Value): void;
@@ -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
- type HeadSetting = Exclude<keyof ConfigFromHookResolved, 'Head'>;
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,16 +1,24 @@
1
1
  export { getHeadSetting };
2
2
  import { isCallable } from '../utils/isCallable.js';
3
- function getHeadSetting(headSetting, pageContext) {
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
10
+ const valFromConfig = pageContext.config[configName];
11
11
  const getCallable = (val) => (isCallable(val) ? val(pageContext) : val);
12
- const val = pageContext.config[headSetting];
13
- if (Array.isArray(val))
14
- return val.map(getCallable); // cumulative configs
15
- return getCallable(val);
12
+ if (!includes(configsCumulative, configName)) {
13
+ if (valFromUseConfig !== undefined)
14
+ return valFromUseConfig;
15
+ return getCallable(valFromConfig);
16
+ }
17
+ else {
18
+ return [
19
+ //
20
+ ...(valFromConfig ?? []).map(getCallable),
21
+ ...(valFromUseConfig ?? [])
22
+ ];
23
+ }
16
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
- // E.g. document.title
42
- updateDocument(pageContext);
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 updateDocument(pageContext) {
50
- pageContext._headAlreadySet = true;
51
+ function applyHead(pageContext) {
51
52
  const title = getHeadSetting('title', pageContext);
52
53
  const lang = getHeadSetting('lang', pageContext);
53
- // - We skip if `undefined` as we shouldn't remove values set by the Head setting.
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,6 +9,7 @@ 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);
@@ -36,11 +37,13 @@ async function getPageHtml(pageContext) {
36
37
  const page = getPageElement(pageContext);
37
38
  const { stream, streamIsRequired } = pageContext.config;
38
39
  if (!stream && !streamIsRequired) {
39
- pageHtml = dangerouslySkipEscape(renderToString(page));
40
+ const pageHtmlString = renderToString(page);
41
+ pageContext.pageHtmlString = pageHtmlString;
42
+ pageHtml = dangerouslySkipEscape(pageHtmlString);
40
43
  }
41
44
  else {
42
45
  const disable = stream === false ? true : undefined;
43
- pageHtml = await renderToStream(page, {
46
+ const pageHtmlStream = await renderToStream(page, {
44
47
  webStream: typeof stream === 'string' ? stream === 'web' : undefined,
45
48
  userAgent: pageContext.headers?.['user-agent'] ||
46
49
  // TODO/eventually: remove old way of acccessing the User Agent header.
@@ -48,8 +51,12 @@ async function getPageHtml(pageContext) {
48
51
  pageContext.userAgent,
49
52
  disable
50
53
  });
54
+ pageContext.pageHtmlStream = pageHtmlStream;
55
+ pageHtml = pageHtmlStream;
51
56
  }
52
57
  }
58
+ // https://github.com/vikejs/vike/discussions/1804#discussioncomment-10394481
59
+ await callCumulativeHooks(pageContext.config.onAfterRenderHtml, pageContext);
53
60
  return pageHtml;
54
61
  }
55
62
  function getHeadHtml(pageContext) {
@@ -1,7 +1,7 @@
1
- import type { ImportString, PageContextClient, PageContext as PageContext_, PageContextServer } from 'vike/types';
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 { ConfigFromHookCumulative } from '../hooks/useConfig/useConfig-server.js';
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, ConfigFromHookCumulative> & Pick<Vike.ConfigResolved, ConfigFromHookCumulative>;
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?: true;
22
+ _headAlreadySet?: boolean;
20
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-react",
3
- "version": "0.5.2-commit-a53b7c1",
3
+ "version": "0.5.3-commit-acd5466",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1,2 +0,0 @@
1
- import './PageContext.js';
2
- import './Config.js';
@@ -1,2 +0,0 @@
1
- import './PageContext.js';
2
- import './Config.js';