vike-react 0.3.8 → 0.3.9

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.
@@ -36,6 +36,7 @@ declare const _default: {
36
36
  lang: {
37
37
  env: {
38
38
  server: true;
39
+ client: true;
39
40
  };
40
41
  };
41
42
  ssr: {
@@ -29,7 +29,7 @@ export default {
29
29
  // be used by the renderers.
30
30
  // It is a cumulative config option, so a web app using vike-react can extend
31
31
  // this list.
32
- passToClient: ['pageProps', 'title'],
32
+ passToClient: ['pageProps', 'title', 'lang'],
33
33
  clientRouting: true,
34
34
  hydrationCanBeAborted: true,
35
35
  meta: {
@@ -49,7 +49,7 @@ export default {
49
49
  env: { server: true }
50
50
  },
51
51
  lang: {
52
- env: { server: true }
52
+ env: { server: true, client: true }
53
53
  },
54
54
  ssr: {
55
55
  env: { config: true },
@@ -0,0 +1,7 @@
1
+ export { getLang };
2
+ import type { PageContext } from 'vike/types';
3
+ /**
4
+ * Get the page's lang if defined, either from the config, the additional data fetched by
5
+ * the page's data() and onBeforeRender() hooks or from other hooks.
6
+ */
7
+ declare function getLang(pageContext: PageContext): null | string;
@@ -0,0 +1,32 @@
1
+ export { getLang };
2
+ import { isCallable } from './utils/isCallable.js';
3
+ /**
4
+ * Get the page's lang if defined, either from the config, the additional data fetched by
5
+ * the page's data() and onBeforeRender() hooks or from other hooks.
6
+ */
7
+ function getLang(pageContext) {
8
+ // from onBeforeRoute() hook & other hooks, e.g. onPrerenderStart() hook
9
+ if (pageContext.lang !== undefined) {
10
+ return pageContext.lang;
11
+ }
12
+ const langConfig = pageContext.configEntries.lang?.[0];
13
+ if (!langConfig) {
14
+ return null;
15
+ }
16
+ const lang = langConfig.configValue;
17
+ if (typeof lang === 'string') {
18
+ return lang;
19
+ }
20
+ if (!lang) {
21
+ return null;
22
+ }
23
+ const { configDefinedAt } = langConfig;
24
+ if (isCallable(lang)) {
25
+ const val = lang(pageContext);
26
+ if (typeof val !== 'string') {
27
+ throw new Error(configDefinedAt + ' should return a string');
28
+ }
29
+ return val;
30
+ }
31
+ throw new Error(configDefinedAt + ' should be a string or a function returning a string');
32
+ }
@@ -3,6 +3,7 @@ export { onRenderClient };
3
3
  import ReactDOM from 'react-dom/client';
4
4
  import { getTitle } from './getTitle.js';
5
5
  import { getPageElement } from './getPageElement.js';
6
+ import { getLang } from './getLang.js';
6
7
  let root;
7
8
  const onRenderClient = async (pageContext) => {
8
9
  const page = getPageElement(pageContext);
@@ -23,7 +24,9 @@ const onRenderClient = async (pageContext) => {
23
24
  // previous page. It can even be null, in which case we should unset the
24
25
  // document title.
25
26
  const title = getTitle(pageContext);
27
+ const lang = getLang(pageContext) || 'en';
26
28
  document.title = title || '';
29
+ document.documentElement.lang = lang;
27
30
  }
28
31
  root.render(page);
29
32
  }
@@ -7,14 +7,15 @@ import { getTitle } from './getTitle.js';
7
7
  import { getPageElement } from './getPageElement.js';
8
8
  import { PageContextProvider } from './PageContextProvider.js';
9
9
  import React from 'react';
10
+ import { getLang } from './getLang.js';
10
11
  checkVikeVersion();
11
12
  const onRenderHtml = async (pageContext) => {
12
- const lang = pageContext.config.lang || 'en';
13
13
  const { stream, favicon, description } = pageContext.config;
14
14
  const faviconTag = !favicon ? '' : escapeInject `<link rel="icon" href="${favicon}" />`;
15
15
  const descriptionTag = !description ? '' : escapeInject `<meta name="description" content="${description}" />`;
16
16
  const title = getTitle(pageContext);
17
17
  const titleTag = !title ? '' : escapeInject `<title>${title}</title>`;
18
+ const lang = getLang(pageContext) || 'en';
18
19
  const Head = pageContext.config.Head || (() => React.createElement(React.Fragment, null));
19
20
  const head = (React.createElement(React.StrictMode, null,
20
21
  React.createElement(PageContextProvider, { pageContext: pageContext },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-react",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "type": "module",
5
5
  "main": "./dist/renderer/+config.js",
6
6
  "types": "./dist/renderer/+config.d.ts",
@@ -22,20 +22,20 @@
22
22
  "react": "18.x.x",
23
23
  "react-dom": "18.x.x",
24
24
  "vike": "^0.4.151",
25
- "vite": "^4.3.8"
25
+ "vite": "^4.3.8 || ^5.0.10"
26
26
  },
27
27
  "devDependencies": {
28
- "@brillout/release-me": "^0.1.13",
29
- "@types/node": "^18.19.3",
30
- "@types/react": "^18.2.45",
28
+ "@brillout/release-me": "^0.1.14",
29
+ "@types/node": "^20.11.4",
30
+ "@types/react": "^18.2.48",
31
31
  "@types/react-dom": "^18.2.18",
32
32
  "react": "^18.2.0",
33
33
  "react-dom": "^18.2.0",
34
34
  "typescript": "^5.3.3",
35
- "vike": "^0.4.151"
35
+ "vike": "^0.4.156"
36
36
  },
37
37
  "dependencies": {
38
- "react-streaming": "^0.3.18"
38
+ "react-streaming": "^0.3.19"
39
39
  },
40
40
  "typesVersions": {
41
41
  "*": {