vike-react 0.5.0 → 0.5.1

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
@@ -16,6 +16,7 @@ declare const _default: {
16
16
  env: {
17
17
  server: true;
18
18
  };
19
+ cumulative: true;
19
20
  };
20
21
  Layout: {
21
22
  env: {
@@ -91,11 +92,13 @@ declare const _default: {
91
92
  env: {
92
93
  client: true;
93
94
  };
95
+ cumulative: true;
94
96
  };
95
97
  onAfterRenderClient: {
96
98
  env: {
97
99
  client: true;
98
100
  };
101
+ cumulative: true;
99
102
  };
100
103
  Wrapper: {
101
104
  cumulative: true;
@@ -104,16 +107,6 @@ declare const _default: {
104
107
  server: true;
105
108
  };
106
109
  };
107
- name: {
108
- env: {
109
- config: true;
110
- };
111
- };
112
- require: {
113
- env: {
114
- config: true;
115
- };
116
- };
117
110
  reactStrictMode: {
118
111
  env: {
119
112
  client: true;
package/dist/+config.js CHANGED
@@ -23,7 +23,8 @@ export default {
23
23
  // https://vike.dev/meta
24
24
  meta: {
25
25
  Head: {
26
- env: { server: true }
26
+ env: { server: true },
27
+ cumulative: true
27
28
  },
28
29
  Layout: {
29
30
  env: { server: true, client: true },
@@ -69,23 +70,17 @@ export default {
69
70
  env: { server: true }
70
71
  },
71
72
  onBeforeRenderClient: {
72
- env: { client: true }
73
+ env: { client: true },
74
+ cumulative: true
73
75
  },
74
76
  onAfterRenderClient: {
75
- env: { client: true }
77
+ env: { client: true },
78
+ cumulative: true
76
79
  },
77
80
  Wrapper: {
78
81
  cumulative: true,
79
82
  env: { client: true, server: true }
80
83
  },
81
- // Vike already defines the setting 'name', but we redundantly define it here for older Vike versions (otherwise older Vike versions will complain that 'name` is an unknown config). TODO/eventually: remove this once <=0.4.172 versions become rare (also because we use the `require` setting starting from `0.4.173`).
82
- name: {
83
- env: { config: true }
84
- },
85
- // Vike already defines the setting 'require', but we redundantly define it here for older Vike versions (otherwise older Vike versions will complain that 'require` is an unknown config). TODO/eventually: remove this once <=0.4.172 versions become rare (also because we use the `require` setting starting from `0.4.173`).
86
- require: {
87
- env: { config: true }
88
- },
89
84
  reactStrictMode: {
90
85
  env: { client: true, server: true }
91
86
  },
@@ -1,8 +1,8 @@
1
1
  export { useConfig };
2
- import type { ConfigSetter } from './shared.js';
2
+ import type { ConfigFromHook } from '../../types/Config.js';
3
3
  /**
4
4
  * Set configurations inside React components and Vike hooks.
5
5
  *
6
6
  * https://vike.dev/useConfig
7
7
  */
8
- declare function useConfig(): ConfigSetter;
8
+ declare function useConfig(): (config: ConfigFromHook) => void;
@@ -1,8 +1,8 @@
1
1
  export { useConfig };
2
- import type { ConfigSetter } from './shared.js';
2
+ import type { ConfigFromHook } from '../../types/Config.js';
3
3
  /**
4
4
  * Set configurations inside React components and Vike hooks.
5
5
  *
6
6
  * https://vike.dev/useConfig
7
7
  */
8
- declare function useConfig(): ConfigSetter;
8
+ declare function useConfig(): (config: ConfigFromHook) => void;
@@ -1,4 +1,5 @@
1
1
  export { getHeadSetting };
2
+ import type { PageContext } from 'vike/types';
2
3
  import type { PageContextInternal } from '../types/PageContext.js';
3
4
  type HeadSetting = 'favicon' | 'lang' | 'title' | 'description' | 'image';
4
- declare function getHeadSetting(headSetting: HeadSetting, pageContext: PageContextInternal): undefined | null | string;
5
+ declare function getHeadSetting(headSetting: HeadSetting, pageContext: PageContext & PageContextInternal): undefined | null | string;
@@ -1,4 +1,4 @@
1
1
  export { onRenderClient };
2
- import type { OnRenderClientSync } from 'vike/types';
2
+ import type { OnRenderClientAsync } from 'vike/types';
3
3
  import './styles.css';
4
- declare const onRenderClient: OnRenderClientSync;
4
+ declare const onRenderClient: OnRenderClientAsync;
@@ -4,11 +4,12 @@ import ReactDOM from 'react-dom/client';
4
4
  import { getHeadSetting } from './getHeadSetting.js';
5
5
  import { getPageElement } from './getPageElement.js';
6
6
  import './styles.css';
7
+ import { callCumulativeHooks } from '../utils/callCumulativeHooks.js';
7
8
  let root;
8
- const onRenderClient = (pageContext) => {
9
+ const onRenderClient = async (pageContext) => {
9
10
  // Use case:
10
11
  // - Store hydration https://github.com/vikejs/vike-react/issues/110
11
- pageContext.config.onBeforeRenderClient?.(pageContext);
12
+ await callCumulativeHooks(pageContext.config.onBeforeRenderClient, pageContext);
12
13
  const page = getPageElement(pageContext);
13
14
  pageContext.page = page;
14
15
  // TODO: implement this? So that, upon errors, onRenderClient() throws an error and Vike can render the error. As of April 2024 it isn't released yet.
@@ -41,18 +42,18 @@ const onRenderClient = (pageContext) => {
41
42
  updateDocument(pageContext);
42
43
  }
43
44
  // Use cases:
44
- // - Custom user settings: https://vike.dev/head#custom-settings
45
+ // - Custom user settings: https://vike.dev/head-tags#custom-settings
45
46
  // - Testing tools: https://github.com/vikejs/vike-react/issues/95
46
- pageContext.config.onAfterRenderClient?.(pageContext);
47
+ await callCumulativeHooks(pageContext.config.onAfterRenderClient, pageContext);
47
48
  };
48
49
  function updateDocument(pageContext) {
49
- ;
50
50
  pageContext._headAlreadySet = true;
51
51
  const title = getHeadSetting('title', pageContext);
52
52
  const lang = getHeadSetting('lang', pageContext);
53
53
  // - We skip if `undefined` as we shouldn't remove values set by the Head setting.
54
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
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
56
57
  if (title !== undefined)
57
58
  document.title = title || '';
58
59
  if (lang !== undefined)
@@ -3,13 +3,12 @@ export { onRenderHtml };
3
3
  import React from 'react';
4
4
  import { renderToString, renderToStaticMarkup } from 'react-dom/server';
5
5
  import { renderToStream } from 'react-streaming/server';
6
- import { dangerouslySkipEscape, escapeInject, version } from 'vike/server';
6
+ import { dangerouslySkipEscape, escapeInject } from 'vike/server';
7
7
  import { PageContextProvider } from '../hooks/usePageContext.js';
8
8
  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
- checkVikeVersion();
13
12
  addEcosystemStamp();
14
13
  const onRenderHtml = async (pageContext) => {
15
14
  const pageHtml = await getPageHtml(pageContext);
@@ -66,9 +65,9 @@ function getHeadHtml(pageContext) {
66
65
  ? ''
67
66
  : escapeInject `<meta property="og:image" content="${image}"><meta name="twitter:card" content="summary_large_image">`;
68
67
  const headElementsHtml = dangerouslySkipEscape([
69
- // <Head> set by +Head
70
- pageContext.config.Head,
71
- // <Head> set by useConfig()
68
+ // Added by +Head
69
+ ...(pageContext.config.Head ?? []),
70
+ // Added by useConfig()
72
71
  ...(pageContext._configFromHook?.Head ?? [])
73
72
  ]
74
73
  .filter((Head) => Head !== null && Head !== undefined)
@@ -79,10 +78,10 @@ function getHeadHtml(pageContext) {
79
78
  const viewportTag = dangerouslySkipEscape(getViewportTag(pageContext.config.viewport));
80
79
  const headHtml = escapeInject `
81
80
  ${titleTags}
82
- ${headElementsHtml}
83
81
  ${viewportTag}
84
- ${descriptionTags}
82
+ ${headElementsHtml}
85
83
  ${faviconTag}
84
+ ${descriptionTags}
86
85
  ${imageTags}
87
86
  `;
88
87
  return headHtml;
@@ -128,21 +127,6 @@ function getViewportTag(viewport) {
128
127
  }
129
128
  return '';
130
129
  }
131
- // We don't need this anymore starting from vike@0.4.173 which added the `require` setting.
132
- // TODO/eventually: remove this once <=0.4.172 versions become rare.
133
- function checkVikeVersion() {
134
- if (version) {
135
- const versionParts = version.split('.').map((s) => parseInt(s, 10));
136
- if (versionParts[0] > 0)
137
- return;
138
- if (versionParts[1] > 4)
139
- return;
140
- if (versionParts[2] >= 173)
141
- return;
142
- }
143
- // We can leave it 0.4.173 until we entirely remove checkVikeVersion() (because starting vike@0.4.173 we use the new `require` setting).
144
- throw new Error('Update Vike to 0.4.173 or above');
145
- }
146
130
  // For improving error messages of:
147
131
  // - react-streaming https://github.com/brillout/react-streaming/blob/6a43dd20c27fb5d751dca41466b06ee3f4f35462/src/server/useStream.ts#L21
148
132
  // - vike https://github.com/vikejs/vike/blob/96c0155380ffebd4976ab076b58e86d8eb2d603a/vike/node/runtime/html/stream/react-streaming.ts#L31
@@ -1,10 +1,14 @@
1
- import type { ImportString, PageContextClient, PageContext } from 'vike/types';
1
+ import type { ImportString, PageContextClient, PageContext as PageContext_, PageContextServer } from 'vike/types';
2
2
  import type { TagAttributes } from '../utils/getTagAttributesString.js';
3
3
  import type { Viewport } from '../renderer/onRenderHtml.js';
4
4
  declare global {
5
5
  namespace Vike {
6
6
  interface Config {
7
- /** The page's root React component */
7
+ /**
8
+ * The page's root React component.
9
+ *
10
+ * https://vike.dev/Page
11
+ */
8
12
  Page?: () => React.ReactNode;
9
13
  /**
10
14
  * Add arbitrary `<head>` tags.
@@ -13,7 +17,7 @@ declare global {
13
17
  */
14
18
  Head?: Head;
15
19
  /**
16
- * A component that defines the visual layout of the page common to several pages.
20
+ * A component that defines the visual layout common to several pages.
17
21
  *
18
22
  * Technically: the `<Layout>` component wraps the root component `<Page>`.
19
23
  *
@@ -39,7 +43,7 @@ declare global {
39
43
  *
40
44
  * https://vike.dev/title
41
45
  */
42
- title?: PlainOrGetter<string>;
46
+ title?: string | ((pageContext: PageContext_) => string);
43
47
  /**
44
48
  * Set the page's description.
45
49
  *
@@ -53,7 +57,7 @@ declare global {
53
57
  *
54
58
  * https://vike.dev/description
55
59
  */
56
- description?: PlainOrGetter<string>;
60
+ description?: string | ((pageContext: PageContextServer) => string);
57
61
  /**
58
62
  * Set the page's preview image upon URL sharing.
59
63
  *
@@ -67,13 +71,13 @@ declare global {
67
71
  *
68
72
  * https://vike.dev/image
69
73
  */
70
- image?: PlainOrGetter<string>;
74
+ image?: string | ((pageContext: PageContextServer) => string);
71
75
  /**
72
76
  * Set the page's width shown to the user on mobile/tablet devices.
73
77
  *
74
- * https://vike.dev/viewport
75
- *
76
78
  * @default "responsive"
79
+ *
80
+ * https://vike.dev/viewport
77
81
  */
78
82
  viewport?: Viewport;
79
83
  /**
@@ -88,7 +92,7 @@ declare global {
88
92
  *
89
93
  * https://vike.dev/favicon
90
94
  */
91
- favicon?: PlainOrGetter<string>;
95
+ favicon?: string | ((pageContext: PageContextServer) => string);
92
96
  /**
93
97
  * Set the page's language (`<html lang>`).
94
98
  *
@@ -96,7 +100,7 @@ declare global {
96
100
  *
97
101
  * https://vike.dev/lang
98
102
  */
99
- lang?: PlainOrGetter<string> | null;
103
+ lang?: string | ((pageContext: PageContext_) => string);
100
104
  /**
101
105
  * Add tag attributes such as `<html class="dark">`.
102
106
  *
@@ -114,10 +118,9 @@ declare global {
114
118
  *
115
119
  * If `false`, the page is rendered only once in the browser.
116
120
  *
117
- * https://vike.dev/ssr
118
- *
119
121
  * @default true
120
122
  *
123
+ * https://vike.dev/ssr
121
124
  */
122
125
  ssr?: boolean;
123
126
  /**
@@ -132,7 +135,6 @@ declare global {
132
135
  * @default false
133
136
  *
134
137
  * https://vike.dev/stream
135
- *
136
138
  */
137
139
  stream?: boolean | 'node' | 'web';
138
140
  /**
@@ -144,9 +146,9 @@ declare global {
144
146
  /**
145
147
  * Whether to use `<StrictMode>`.
146
148
  *
147
- * https://vike.dev/reactStrictMode
148
- *
149
149
  * @default true
150
+ *
151
+ * https://vike.dev/reactStrictMode
150
152
  */
151
153
  reactStrictMode?: boolean;
152
154
  /**
@@ -161,17 +163,24 @@ declare global {
161
163
  * https://vike.dev/onAfterRenderClient
162
164
  */
163
165
  onAfterRenderClient?: (pageContext: PageContextClient) => void;
166
+ /**
167
+ * Define loading animations.
168
+ *
169
+ * https://vike.dev/Loading
170
+ */
164
171
  Loading?: Loading | ImportString;
165
172
  }
166
173
  interface ConfigResolved {
167
174
  Wrapper?: Wrapper[];
168
175
  Layout?: Layout[];
176
+ Head?: Head[];
169
177
  bodyAttributes?: TagAttributes[];
170
178
  htmlAttributes?: TagAttributes[];
179
+ onBeforeRenderClient?: Function[];
180
+ onAfterRenderClient?: Function[];
171
181
  }
172
182
  }
173
183
  }
174
- type PlainOrGetter<T> = T | ((pageContext: PageContext) => T);
175
184
  export type Head = React.ReactNode | (() => React.ReactNode);
176
185
  type Wrapper = (props: {
177
186
  children: React.ReactNode;
@@ -2,7 +2,6 @@ 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 { PageContext } from 'vike/types';
6
5
  declare global {
7
6
  namespace Vike {
8
7
  interface PageContext {
@@ -15,7 +14,7 @@ declare global {
15
14
  }
16
15
  }
17
16
  }
18
- export type PageContextInternal = PageContext & {
17
+ export type PageContextInternal = {
19
18
  _configFromHook?: ConfigFromHookResolved;
20
19
  _headAlreadySet?: true;
21
20
  };
@@ -0,0 +1 @@
1
+ export declare function callCumulativeHooks(values: undefined | unknown[], pageContext: unknown): Promise<unknown[]>;
@@ -0,0 +1,16 @@
1
+ export async function callCumulativeHooks(values, pageContext) {
2
+ if (!values)
3
+ return [];
4
+ const valuesPromises = values.map((val) => {
5
+ if (typeof val === 'function') {
6
+ // Hook
7
+ return val(pageContext);
8
+ }
9
+ else {
10
+ // Plain value
11
+ return val;
12
+ }
13
+ });
14
+ const valuesResolved = await Promise.all(valuesPromises);
15
+ return valuesResolved;
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-react",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1,3 +0,0 @@
1
- export type { ConfigSetter };
2
- import type { ConfigFromHook } from '../../types/Config.js';
3
- type ConfigSetter = (config: ConfigFromHook) => void;
@@ -1 +0,0 @@
1
- export {};