vike-solid 0.2.7 → 0.2.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.
package/dist/+config.js CHANGED
@@ -28,13 +28,15 @@ const toggleSsrRelatedConfig = ({
28
28
  var _config = {
29
29
  onRenderHtml: "import:vike-solid/renderer/onRenderHtml:onRenderHtml",
30
30
  onRenderClient: "import:vike-solid/renderer/onRenderClient:onRenderClient",
31
+ // TODO/next-major-release: remove pageProps (i.e. tell users to use data() instead of onBeforeRender() to fetch data)
32
+ // TODO/next-major-release: remove support for setting title over onBeforeRender()
31
33
  // A page can define an onBeforeRender() hook to be run on the server, which
32
34
  // can fetch data and return it as additional page context. Typically it will
33
35
  // return the page's root Solid component's props and additional data that can
34
36
  // be used by the renderers.
35
37
  // It is a cumulative config option, so a web app using vike-solid can extend
36
38
  // this list.
37
- passToClient: ["pageProps", "title"],
39
+ passToClient: ["pageProps", "title", "lang"],
38
40
  clientRouting: true,
39
41
  hydrationCanBeAborted: true,
40
42
  meta: {
@@ -67,7 +69,8 @@ var _config = {
67
69
  },
68
70
  lang: {
69
71
  env: {
70
- server: true
72
+ server: true,
73
+ client: true
71
74
  }
72
75
  },
73
76
  ssr: {
@@ -1,13 +1,24 @@
1
1
  import { createComponent, Dynamic, mergeProps, memo, hydrate, render } from 'solid-js/web';
2
- import { P as PageContextProvider, u as usePageContext } from './PageContextProvider-gFteWsFt.js';
2
+ import { P as PageContextProvider, u as usePageContext } from './PageContextProvider-OTjP33FZ.js';
3
3
  import { createStore, reconcile } from 'solid-js/store';
4
4
  import 'solid-js';
5
5
 
6
+ function isCallable(thing) {
7
+ return thing instanceof Function || typeof thing === 'function';
8
+ }
9
+
6
10
  /**
7
11
  * Get the page's title if defined, either from the additional data fetched by
8
- * the page's onBeforeRender() hook or from the config.
12
+ * the page's data() and onBeforeRender() hook or from the config.
9
13
  */
10
14
  function getTitle(pageContext) {
15
+ // from data() hook
16
+ if (pageContext.data?.title !== undefined) {
17
+ return pageContext.data.title;
18
+ }
19
+
20
+ // TODO/next-major-release: remove support for setting title over onBeforeRender()
21
+ // from onBeforeRender() hook
11
22
  if (pageContext.title !== undefined) {
12
23
  return pageContext.title;
13
24
  }
@@ -25,14 +36,12 @@ function getTitle(pageContext) {
25
36
  const {
26
37
  configDefinedAt
27
38
  } = titleConfig;
28
- if (typeof title === "function") {
39
+ if (isCallable(title)) {
29
40
  const val = title(pageContext);
30
- if (typeof val === "string") {
31
- return val;
32
- }
33
- if (val) {
41
+ if (typeof val !== "string") {
34
42
  throw new Error(configDefinedAt + " should return a string");
35
43
  }
44
+ return val;
36
45
  }
37
46
  throw new Error(configDefinedAt + " should be a string or a function returning a string");
38
47
  }
@@ -63,6 +72,7 @@ function Layout(props) {
63
72
  }
64
73
  function Page() {
65
74
  const pageContext = usePageContext();
75
+ // TODO/next-major-release: remove pageProps (i.e. tell users to use data() instead of onBeforeRender() to fetch data)
66
76
  return createComponent(Dynamic, mergeProps({
67
77
  get component() {
68
78
  return pageContext.Page;
@@ -73,6 +83,39 @@ function Passthrough(props) {
73
83
  return memo(() => props.children);
74
84
  }
75
85
 
86
+ /**
87
+ * Get the page's lang if defined, either from the config, the additional data fetched by
88
+ * the page's data() and onBeforeRender() hooks or from other hooks.
89
+ */
90
+ function getLang(pageContext) {
91
+ // from onBeforeRoute() hook & other hooks, e.g. onPrerenderStart() hook
92
+ if (pageContext.lang !== undefined) {
93
+ return pageContext.lang;
94
+ }
95
+ const langConfig = pageContext.configEntries.lang?.[0];
96
+ if (!langConfig) {
97
+ return null;
98
+ }
99
+ const lang = langConfig.configValue;
100
+ if (typeof lang === 'string') {
101
+ return lang;
102
+ }
103
+ if (!lang) {
104
+ return null;
105
+ }
106
+ const {
107
+ configDefinedAt
108
+ } = langConfig;
109
+ if (isCallable(lang)) {
110
+ const val = lang(pageContext);
111
+ if (typeof val !== 'string') {
112
+ throw new Error(configDefinedAt + ' should return a string');
113
+ }
114
+ return val;
115
+ }
116
+ throw new Error(configDefinedAt + ' should be a string or a function returning a string');
117
+ }
118
+
76
119
  // https://vike.dev/onRenderClient
77
120
  const [pageContextStore, setPageContext] = createStore({});
78
121
  let dispose;
@@ -101,7 +144,9 @@ const onRenderClient = async pageContext => {
101
144
  // previous page. It can even be null, in which case we should unset the
102
145
  // document title.
103
146
  const title = getTitle(pageContext);
147
+ const lang = getLang(pageContext) || 'en';
104
148
  document.title = title || "";
149
+ document.documentElement.lang = lang;
105
150
  }
106
151
  };
107
152
 
@@ -1,13 +1,24 @@
1
1
  import { createComponent, Dynamic, mergeProps, renderToString, renderToStream, generateHydrationScript } from 'solid-js/web';
2
2
  import { version, escapeInject, dangerouslySkipEscape, stampPipe } from 'vike/server';
3
- import { P as PageContextProvider, u as usePageContext } from './PageContextProvider-gFteWsFt.js';
3
+ import { P as PageContextProvider, u as usePageContext } from './PageContextProvider-OTjP33FZ.js';
4
4
  import 'solid-js';
5
5
 
6
+ function isCallable(thing) {
7
+ return thing instanceof Function || typeof thing === 'function';
8
+ }
9
+
6
10
  /**
7
11
  * Get the page's title if defined, either from the additional data fetched by
8
- * the page's onBeforeRender() hook or from the config.
12
+ * the page's data() and onBeforeRender() hook or from the config.
9
13
  */
10
14
  function getTitle(pageContext) {
15
+ // from data() hook
16
+ if (pageContext.data?.title !== undefined) {
17
+ return pageContext.data.title;
18
+ }
19
+
20
+ // TODO/next-major-release: remove support for setting title over onBeforeRender()
21
+ // from onBeforeRender() hook
11
22
  if (pageContext.title !== undefined) {
12
23
  return pageContext.title;
13
24
  }
@@ -25,14 +36,12 @@ function getTitle(pageContext) {
25
36
  const {
26
37
  configDefinedAt
27
38
  } = titleConfig;
28
- if (typeof title === "function") {
39
+ if (isCallable(title)) {
29
40
  const val = title(pageContext);
30
- if (typeof val === "string") {
31
- return val;
32
- }
33
- if (val) {
41
+ if (typeof val !== "string") {
34
42
  throw new Error(configDefinedAt + " should return a string");
35
43
  }
44
+ return val;
36
45
  }
37
46
  throw new Error(configDefinedAt + " should be a string or a function returning a string");
38
47
  }
@@ -63,6 +72,7 @@ function Layout(props) {
63
72
  }
64
73
  function Page() {
65
74
  const pageContext = usePageContext();
75
+ // TODO/next-major-release: remove pageProps (i.e. tell users to use data() instead of onBeforeRender() to fetch data)
66
76
  return createComponent(Dynamic, mergeProps({
67
77
  get component() {
68
78
  return pageContext.Page;
@@ -73,6 +83,39 @@ function Passthrough(props) {
73
83
  return props.children;
74
84
  }
75
85
 
86
+ /**
87
+ * Get the page's lang if defined, either from the config, the additional data fetched by
88
+ * the page's data() and onBeforeRender() hooks or from other hooks.
89
+ */
90
+ function getLang(pageContext) {
91
+ // from onBeforeRoute() hook & other hooks, e.g. onPrerenderStart() hook
92
+ if (pageContext.lang !== undefined) {
93
+ return pageContext.lang;
94
+ }
95
+ const langConfig = pageContext.configEntries.lang?.[0];
96
+ if (!langConfig) {
97
+ return null;
98
+ }
99
+ const lang = langConfig.configValue;
100
+ if (typeof lang === 'string') {
101
+ return lang;
102
+ }
103
+ if (!lang) {
104
+ return null;
105
+ }
106
+ const {
107
+ configDefinedAt
108
+ } = langConfig;
109
+ if (isCallable(lang)) {
110
+ const val = lang(pageContext);
111
+ if (typeof val !== 'string') {
112
+ throw new Error(configDefinedAt + ' should return a string');
113
+ }
114
+ return val;
115
+ }
116
+ throw new Error(configDefinedAt + ' should be a string or a function returning a string');
117
+ }
118
+
76
119
  checkVikeVersion();
77
120
  const onRenderHtml = async pageContext => {
78
121
  const {
@@ -100,7 +143,7 @@ const onRenderHtml = async pageContext => {
100
143
  stampPipe(pageView, "node-stream");
101
144
  }
102
145
  }
103
- const lang = pageContext.config.lang || "en";
146
+ const lang = getLang(pageContext) || "en";
104
147
  const documentHtml = escapeInject`<!DOCTYPE html>
105
148
  <html lang='${lang}'>
106
149
  <head>
@@ -1,7 +1,7 @@
1
1
  import { createComponent, Dynamic, ssr, ssrHydrationKey } from 'solid-js/web';
2
2
  import { createSignal, createEffect, Suspense, lazy } from 'solid-js';
3
3
 
4
- const _tmpl$ = ["<p", ">Error loading component.</p>"];
4
+ var _tmpl$ = ["<p", ">Error loading component.</p>"];
5
5
  function ClientOnlyError() {
6
6
  return ssr(_tmpl$, ssrHydrationKey());
7
7
  }
@@ -32,5 +32,17 @@ function usePageContext() {
32
32
  if (!pageContext) throw new Error("<PageContextProvider> is needed for being able to use usePageContext()");
33
33
  return pageContext;
34
34
  }
35
+ /** Access `pageContext.data` from any SolidJS component
36
+ *
37
+ * See
38
+ * - https://vike.dev/data
39
+ * - https://vike.dev/pageContext-anywhere
40
+ */
41
+ function useData() {
42
+ const {
43
+ data
44
+ } = usePageContext();
45
+ return data;
46
+ }
35
47
 
36
- export { PageContextProvider as P, usePageContext as u };
48
+ export { PageContextProvider as P, useData as a, usePageContext as u };
@@ -0,0 +1,2 @@
1
+ export { a as useData } from '../useData-UNVtqljj.js';
2
+ import 'vike/types';
@@ -1,6 +1,2 @@
1
- import { PageContext } from 'vike/types';
2
-
3
- /** Access the pageContext from any SolidJS component */
4
- declare function usePageContext(): PageContext;
5
-
6
- export { usePageContext };
1
+ export { u as usePageContext } from '../useData-UNVtqljj.js';
2
+ import 'vike/types';
@@ -7,10 +7,15 @@ declare global {
7
7
  namespace Vike {
8
8
  interface PageContext {
9
9
  Page?: Page;
10
- /** Properties of the page's root Solid component. */
10
+ /** Properties of the page's root Solid component - e.g. set by onBeforeRender() hook */
11
11
  pageProps?: Record<string, unknown>;
12
- /** &lt;title>${title}&lt;/title> - has precedence over the config */
12
+ /** &lt;title>${title}&lt;/title> - set by onBeforeRender() hook, has precedence over the config */
13
13
  title?: string;
14
+ lang?: string;
15
+ data?: {
16
+ /** &lt;title>${title}&lt;/title> - set by data() hook, has precedence over the onBeforeRender() hook */
17
+ title?: string;
18
+ };
14
19
  }
15
20
  }
16
21
  }
@@ -52,6 +57,7 @@ declare const _default: {
52
57
  lang: {
53
58
  env: {
54
59
  server: true;
60
+ client: true;
55
61
  };
56
62
  };
57
63
  ssr: {
@@ -0,0 +1,13 @@
1
+ import { PageContext } from 'vike/types';
2
+
3
+ /** Access the pageContext from any SolidJS component */
4
+ declare function usePageContext(): PageContext;
5
+ /** Access `pageContext.data` from any SolidJS component
6
+ *
7
+ * See
8
+ * - https://vike.dev/data
9
+ * - https://vike.dev/pageContext-anywhere
10
+ */
11
+ declare function useData<Data>(): Data;
12
+
13
+ export { useData as a, usePageContext as u };
@@ -0,0 +1,3 @@
1
+ export { a as useData } from './PageContextProvider-OTjP33FZ.js';
2
+ import 'solid-js/web';
3
+ import 'solid-js';
@@ -1,3 +1,3 @@
1
- export { u as usePageContext } from './PageContextProvider-gFteWsFt.js';
1
+ export { u as usePageContext } from './PageContextProvider-OTjP33FZ.js';
2
2
  import 'solid-js/web';
3
3
  import 'solid-js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-solid",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "vite-plugin-solid": "^2.8.0"
@@ -11,26 +11,27 @@
11
11
  "vike": "^0.4.152"
12
12
  },
13
13
  "devDependencies": {
14
- "@babel/core": "^7.23.6",
15
- "@babel/preset-env": "^7.23.6",
14
+ "@babel/core": "^7.23.7",
15
+ "@babel/preset-env": "^7.23.8",
16
16
  "@babel/preset-typescript": "^7.23.3",
17
17
  "@rollup/plugin-babel": "^6.0.4",
18
18
  "@rollup/plugin-node-resolve": "^15.2.3",
19
19
  "@types/node": "^18.17.4",
20
- "babel-preset-solid": "^1.8.6",
20
+ "babel-preset-solid": "^1.8.9",
21
21
  "bumpp": "^9.2.1",
22
- "rollup": "^4.9.1",
22
+ "rollup": "^4.9.5",
23
23
  "rollup-plugin-dts": "^6.1.0",
24
- "solid-js": "^1.8.7",
24
+ "solid-js": "^1.8.11",
25
25
  "tslib": "^2.6.2",
26
26
  "typescript": "^5.3.3",
27
- "vite": "^5.0.10",
28
- "vike": "^0.4.152"
27
+ "vite": "^5.0.11",
28
+ "vike": "^0.4.156"
29
29
  },
30
30
  "exports": {
31
31
  ".": "./dist/+config.js",
32
32
  "./vite": "./dist/vite-plugin-vike-solid.js",
33
33
  "./usePageContext": "./dist/usePageContext.js",
34
+ "./useData": "./dist/useData.js",
34
35
  "./ClientOnly": "./dist/ClientOnly.js",
35
36
  "./renderer/onRenderHtml": "./dist/+onRenderHtml.js",
36
37
  "./renderer/onRenderClient": "./dist/+onRenderClient.js"
@@ -49,6 +50,9 @@
49
50
  "usePageContext": [
50
51
  "dist/components/usePageContext.d.ts"
51
52
  ],
53
+ "useData": [
54
+ "dist/components/useData.d.ts"
55
+ ],
52
56
  "ClientOnly": [
53
57
  "dist/components/ClientOnly.d.ts"
54
58
  ]