vike-react 0.1.5 → 0.2.0

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/README.md ADDED
@@ -0,0 +1,12 @@
1
+ <!-- WARNING: keep links absolute in this file so they work on NPM too -->
2
+
3
+ [<img src="https://avatars.githubusercontent.com/u/86403530?s=200&v=4" align="right" width="64" height="64">](https://vite-plugin-ssr.com)
4
+ [![npm version](https://img.shields.io/npm/v/vike-react)](https://www.npmjs.com/package/vike-react)
5
+
6
+ # `vike-react`
7
+
8
+ React integration for [Vike](https://github.com/brillout/vite-plugin-ssr/issues/736).
9
+
10
+ > For integrations with Vue and Solid, see the other [`vike-*` packages](https://vite-plugin-ssr.com/vike-packages).
11
+
12
+ See [examples/](https://github.com/vikejs/vike-react/tree/main/examples).
@@ -1,25 +1,4 @@
1
- import type { Config } from 'vite-plugin-ssr/types';
2
- import type { Component } from './types';
3
- export type ConfigEnhanced = Config & Partial<VikeReactConfig & {
4
- Page: Component;
5
- }>;
6
- export type VikeReactConfig = {
7
- /** React element renderer and appended into &lt;head>&lt;/head> */
8
- Head: Component;
9
- Layout: Component;
10
- /** &lt;title>${title}&lt;/title> */
11
- title: string;
12
- /** &lt;meta name="description" content="${description}" /> */
13
- description: string;
14
- /** &lt;link rel="icon" href="${favicon}" /> */
15
- favicon: string;
16
- /** &lt;html lang="${lang}">
17
- *
18
- * @default 'en'
19
- *
20
- */
21
- lang: string;
22
- };
1
+ import type { ConfigEffect } from 'vite-plugin-ssr/types';
23
2
  declare const _default: {
24
3
  onRenderHtml: "import:vike-react/renderer/onRenderHtml";
25
4
  onRenderClient: "import:vike-react/renderer/onRenderClient";
@@ -45,6 +24,45 @@ declare const _default: {
45
24
  lang: {
46
25
  env: "server-only";
47
26
  };
27
+ ssr: {
28
+ env: "config-only";
29
+ effect: ConfigEffect;
30
+ };
48
31
  };
49
32
  };
50
33
  export default _default;
34
+ import type { Component } from './types';
35
+ declare global {
36
+ namespace VikePackages {
37
+ interface ConfigVikeReact {
38
+ /** React element rendered and appended into &lt;head>&lt;/head> */
39
+ Head?: Component;
40
+ /** A component, usually common to several pages, that wraps the root component `Page` */
41
+ Layout?: Component;
42
+ /** &lt;title>${title}&lt;/title> */
43
+ title?: string;
44
+ /** &lt;meta name="description" content="${description}" /> */
45
+ description?: string;
46
+ /** &lt;link rel="icon" href="${favicon}" /> */
47
+ favicon?: string;
48
+ /** &lt;html lang="${lang}">
49
+ *
50
+ * @default 'en'
51
+ *
52
+ */
53
+ lang?: string;
54
+ /**
55
+ * If true, render mode is SSR or pre-rendering (aka SSG). In other words, the
56
+ * page's HTML will be rendered at build-time or request-time.
57
+ * If false, render mode is SPA. In other words, the page will only be
58
+ * rendered in the browser.
59
+ *
60
+ * See https://vite-plugin-ssr.com/render-modes
61
+ *
62
+ * @default true
63
+ *
64
+ */
65
+ ssr?: boolean;
66
+ }
67
+ }
68
+ }
@@ -1,3 +1,23 @@
1
+ // Depending on the value of `config.meta.ssr`, set other config options' `env`
2
+ // accordingly.
3
+ // See https://vite-plugin-ssr.com/meta#modify-existing-configurations
4
+ const toggleSsrRelatedConfig = ({ configDefinedAt, configValue }) => {
5
+ if (typeof configValue !== 'boolean') {
6
+ throw new Error(`${configDefinedAt} should be a boolean`);
7
+ }
8
+ return {
9
+ meta: {
10
+ // When the SSR flag is false, we want to render the page only in the
11
+ // browser. We achieve this by then making the `Page` implementation
12
+ // accessible only in the client's renderer.
13
+ Page: {
14
+ env: configValue
15
+ ? 'server-and-client' // default
16
+ : 'client-only'
17
+ }
18
+ }
19
+ };
20
+ };
1
21
  export default {
2
22
  onRenderHtml: 'import:vike-react/renderer/onRenderHtml',
3
23
  onRenderClient: 'import:vike-react/renderer/onRenderClient',
@@ -22,6 +42,10 @@ export default {
22
42
  },
23
43
  lang: {
24
44
  env: 'server-only'
45
+ },
46
+ ssr: {
47
+ env: 'config-only',
48
+ effect: toggleSsrRelatedConfig
25
49
  }
26
50
  }
27
51
  };
@@ -2,9 +2,13 @@ export { getPageElement };
2
2
  import React from 'react';
3
3
  import { PageContextProvider } from './PageContextProvider.js';
4
4
  function getPageElement(pageContext) {
5
- var _a, _b;
5
+ var _a;
6
6
  const Layout = (_a = pageContext.config.Layout) !== null && _a !== void 0 ? _a : PassThrough;
7
- const Wrapper = (_b = pageContext.config.Wrapper) !== null && _b !== void 0 ? _b : PassThrough;
7
+ const Wrapper =
8
+ /* Should we implement this? Enabling users to defined a wrapper that is used across all layouts.
9
+ pageContext.config.Wrapper ??
10
+ */
11
+ PassThrough;
8
12
  const { Page, pageProps } = pageContext;
9
13
  const page = (React.createElement(React.StrictMode, null,
10
14
  React.createElement(PageContextProvider, { pageContext: pageContext },
@@ -2,6 +2,5 @@ export { getTitle };
2
2
  import type { ConfigEntries } from 'vite-plugin-ssr/types';
3
3
  declare function getTitle(pageContext: {
4
4
  title?: unknown;
5
- config: Record<string, unknown>;
6
5
  configEntries: ConfigEntries;
7
6
  }): null | string;
@@ -6,7 +6,7 @@ let root;
6
6
  async function onRenderClient(pageContext) {
7
7
  const page = getPageElement(pageContext);
8
8
  const container = document.getElementById('page-view');
9
- if (pageContext.isHydration) {
9
+ if (container.innerHTML !== '' && pageContext.isHydration) {
10
10
  root = ReactDOM.hydrateRoot(container, page);
11
11
  }
12
12
  else {
@@ -1,5 +1,5 @@
1
1
  export default onRenderHtml;
2
2
  import type { PageContextServer } from './types';
3
3
  declare function onRenderHtml(pageContext: PageContextServer): Promise<{
4
- documentHtml: import("vite-plugin-ssr/dist/types/node/runtime/html/renderHtml.js").TemplateWrapped;
4
+ documentHtml: import("vite-plugin-ssr/dist/esm/node/runtime/html/renderHtml.js").TemplateWrapped;
5
5
  }>;
@@ -6,8 +6,11 @@ import { getPageElement } from './getPageElement.js';
6
6
  import { PageContextProvider } from './PageContextProvider.js';
7
7
  import React from 'react';
8
8
  async function onRenderHtml(pageContext) {
9
- const page = getPageElement(pageContext);
10
- const pageHtml = renderToString(page);
9
+ let pageHtml = '';
10
+ if (!!pageContext.Page) {
11
+ const page = getPageElement(pageContext);
12
+ pageHtml = renderToString(page);
13
+ }
11
14
  const title = getTitle(pageContext);
12
15
  const titleTag = !title ? '' : escapeInject `<title>${title}</title>`;
13
16
  const { description } = pageContext.config;
@@ -4,25 +4,15 @@ export type { PageContext };
4
4
  export type { PageProps };
5
5
  export type { Page };
6
6
  export type { Component };
7
- import type { PageContextBuiltIn, PageContextBuiltInClientWithClientRouting as PageContextBuiltInClient } from 'vite-plugin-ssr/types';
8
- import type { ConfigEnhanced } from './+config';
7
+ import type { PageContextBuiltInServer, PageContextBuiltInClientWithClientRouting as PageContextBuiltInClient } from 'vite-plugin-ssr/types';
9
8
  import type { ReactElement } from 'react';
10
9
  type Component = (props: any) => ReactElement;
11
10
  type Page = (pageProps: PageProps) => ReactElement;
12
11
  type PageProps = Record<string, unknown>;
13
- type WrapperComponent = ({ children }: {
14
- children: any;
15
- }) => ReactElement;
16
12
  export type PageContextCommon = {
17
13
  Page: Page;
18
14
  pageProps?: PageProps;
19
- config: {
20
- Layout?: WrapperComponent;
21
- Wrapper?: WrapperComponent;
22
- };
23
- };
24
- type PageContextServer = PageContextBuiltIn<Page> & PageContextCommon & {
25
- config: Partial<ConfigEnhanced>;
26
15
  };
16
+ type PageContextServer = PageContextBuiltInServer<Page> & PageContextCommon;
27
17
  type PageContextClient = PageContextBuiltInClient<Page> & PageContextCommon;
28
18
  type PageContext = PageContextClient | PageContextServer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-react",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "./dist/renderer/+config.js",
6
6
  "types": "./dist/renderer/+config.d.ts",
@@ -20,7 +20,7 @@
20
20
  "react": "18.x.x",
21
21
  "react-dom": "18.x.x",
22
22
  "vite": "^4.3.8",
23
- "vite-plugin-ssr": "^0.4.130"
23
+ "vite-plugin-ssr": "^0.4.141"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@brillout/release-me": "^0.1.3",
@@ -51,6 +51,6 @@
51
51
  "dist/",
52
52
  "types.d.ts"
53
53
  ],
54
- "repository": "github:brillout/vike-react",
54
+ "repository": "github:vikejs/vike-react",
55
55
  "license": "MIT"
56
56
  }
package/types.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export type {
2
- PageContextBuiltIn,
3
- PageContextBuiltInClientWithClientRouting as PageContextBuiltInClient
4
- } from 'vite-plugin-ssr/types'
5
- export type { ConfigEnhanced as Config } from './renderer/+config'