From 651378b0b2980e18463c4c0d80fdc30eb2f7726d Mon Sep 17 00:00:00 2001 From: Aurelien Lourot Date: Sat, 23 Sep 2023 15:24:04 +0200 Subject: [PATCH] qual: interface merging for Vike.PageContext Similar to https://github.com/vikejs/vike-react/pull/21 --- examples/basic/package.json | 2 +- .../pages/star-wars/@id/+onBeforeRender.ts | 6 ++-- examples/ssr-spa/package.json | 2 +- pnpm-lock.yaml | 16 ++++----- vike-solid/package.json | 4 +-- vike-solid/renderer/+config.ts | 3 +- vike-solid/renderer/+onRenderClient.tsx | 8 ++--- vike-solid/renderer/+onRenderHtml.tsx | 4 +-- vike-solid/renderer/PageContextProvider.tsx | 2 +- vike-solid/renderer/getPageElement.tsx | 2 +- vike-solid/renderer/types.ts | 33 +++++-------------- 11 files changed, 34 insertions(+), 48 deletions(-) diff --git a/examples/basic/package.json b/examples/basic/package.json index ae75329..9c8fec2 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -11,7 +11,7 @@ "node-fetch": "^3.3.2", "solid-js": "^1.7.11", "vike-solid": "workspace:*", - "vite-plugin-ssr": "^0.4.141" + "vite-plugin-ssr": "^0.4.142" }, "devDependencies": { "typescript": "^5.1.6" diff --git a/examples/basic/pages/star-wars/@id/+onBeforeRender.ts b/examples/basic/pages/star-wars/@id/+onBeforeRender.ts index dbfba67..e50f803 100644 --- a/examples/basic/pages/star-wars/@id/+onBeforeRender.ts +++ b/examples/basic/pages/star-wars/@id/+onBeforeRender.ts @@ -1,13 +1,13 @@ export default onBeforeRender; import fetch from "cross-fetch"; -import type { PageContextBuiltIn } from "vite-plugin-ssr/types"; +import type { PageContext } from "vite-plugin-ssr/types"; import { filterMovieData } from "../filterMovieData"; import type { MovieDetails } from "../types"; -async function onBeforeRender(pageContext: PageContextBuiltIn) { +async function onBeforeRender(pageContext: PageContext) { const response = await fetch( - `https://star-wars.brillout.com/api/films/${pageContext.routeParams.id}.json`, + `https://star-wars.brillout.com/api/films/${pageContext.routeParams?.id}.json` ); let movie = (await response.json()) as MovieDetails; diff --git a/examples/ssr-spa/package.json b/examples/ssr-spa/package.json index 07d515f..4a1964d 100644 --- a/examples/ssr-spa/package.json +++ b/examples/ssr-spa/package.json @@ -9,7 +9,7 @@ "dependencies": { "solid-js": "^1.7.11", "vike-solid": "workspace:*", - "vite-plugin-ssr": "^0.4.141" + "vite-plugin-ssr": "^0.4.142" }, "devDependencies": { "typescript": "^5.1.6" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d938f0..0c200ff 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,8 +23,8 @@ importers: specifier: workspace:* version: link:../../vike-solid vite-plugin-ssr: - specifier: ^0.4.141 - version: 0.4.141(vite@4.4.9) + specifier: ^0.4.142 + version: 0.4.142(vite@4.4.9) devDependencies: typescript: specifier: ^5.1.6 @@ -39,8 +39,8 @@ importers: specifier: workspace:* version: link:../../vike-solid vite-plugin-ssr: - specifier: ^0.4.141 - version: 0.4.141(vite@4.4.9) + specifier: ^0.4.142 + version: 0.4.142(vite@4.4.9) devDependencies: typescript: specifier: ^5.1.6 @@ -95,8 +95,8 @@ importers: specifier: ^4.4.9 version: 4.4.9(@types/node@18.17.4) vite-plugin-ssr: - specifier: ^0.4.141 - version: 0.4.141(vite@4.4.9) + specifier: ^0.4.142 + version: 0.4.142(vite@4.4.9) packages: @@ -2932,8 +2932,8 @@ packages: - supports-color dev: false - /vite-plugin-ssr@0.4.141(vite@4.4.9): - resolution: {integrity: sha512-mRYP7CWauioeCFWFhzNjYK/4Gv+mtfClM4oj5NZrHS3kmd2vQ/uHENOSccFZniqRTZoMzQoufqpGTd+1uT9vEg==} + /vite-plugin-ssr@0.4.142(vite@4.4.9): + resolution: {integrity: sha512-ZDgpocnTEUmhYxGgMah8ZNK1Sfy5CFVcdXu+Ll3zRtGIPyWT5vYvASs2BLYEneqantHpde2b31Khzb0hxmP62w==} engines: {node: '>=16.0.0'} hasBin: true peerDependencies: diff --git a/vike-solid/package.json b/vike-solid/package.json index 52479421..7117b0a 100644 --- a/vike-solid/package.json +++ b/vike-solid/package.json @@ -20,7 +20,7 @@ "peerDependencies": { "solid-js": "^1.7.12", "vite": "^4.4.9", - "vite-plugin-ssr": "^0.4.141" + "vite-plugin-ssr": "^0.4.142" }, "devDependencies": { "@babel/core": "^7.22.20", @@ -37,7 +37,7 @@ "tslib": "^2.6.2", "typescript": "^5.2.2", "vite": "^4.4.9", - "vite-plugin-ssr": "^0.4.141" + "vite-plugin-ssr": "^0.4.142" }, "typesVersions": { "*": { diff --git a/vike-solid/renderer/+config.ts b/vike-solid/renderer/+config.ts index 61d06b9..4c4a94e 100644 --- a/vike-solid/renderer/+config.ts +++ b/vike-solid/renderer/+config.ts @@ -58,7 +58,7 @@ export default { import type { Component } from "./types.js"; declare global { namespace VikePackages { - export interface ConfigVikeSolid { + interface ConfigVikeReact { /** Solid element renderer and appended into */ Head?: Component; Layout?: Component; @@ -80,6 +80,7 @@ declare global { * */ ssr?: boolean; + /** The page's root Solid component */ Page?: Component; } } diff --git a/vike-solid/renderer/+onRenderClient.tsx b/vike-solid/renderer/+onRenderClient.tsx index 514c43e..534e317 100644 --- a/vike-solid/renderer/+onRenderClient.tsx +++ b/vike-solid/renderer/+onRenderClient.tsx @@ -2,18 +2,18 @@ export default onRenderClient; import { hydrate, render } from "solid-js/web"; import { getTitle } from "./getTitle"; -import type { PageContextClient } from "./types"; +import type { PageContext } from "vite-plugin-ssr/types"; import { getPageElement } from "./getPageElement"; import { createStore, reconcile } from "solid-js/store"; -const [pageContextStore, setPageContext] = createStore( - {} as PageContextClient +const [pageContextStore, setPageContext] = createStore( + {} as PageContext ); let dispose: () => void; let rendered = false; -async function onRenderClient(pageContext: PageContextClient) { +async function onRenderClient(pageContext: PageContext) { if (!rendered) { // Dispose to prevent duplicate pages when navigating. if (dispose) dispose(); diff --git a/vike-solid/renderer/+onRenderHtml.tsx b/vike-solid/renderer/+onRenderHtml.tsx index 1ea7053..7a91cad 100644 --- a/vike-solid/renderer/+onRenderHtml.tsx +++ b/vike-solid/renderer/+onRenderHtml.tsx @@ -11,10 +11,10 @@ import { } from "vite-plugin-ssr/server"; import { getTitle } from "./getTitle"; import { getPageElement } from "./getPageElement"; -import type { PageContextServer } from "./types"; +import type { PageContext } from "vite-plugin-ssr/types"; import { PageContextProvider } from "./PageContextProvider"; -async function onRenderHtml(pageContext: PageContextServer) { +async function onRenderHtml(pageContext: PageContext) { const title = getTitle(pageContext); const titleTag = !title ? "" : escapeInject`${title}`; diff --git a/vike-solid/renderer/PageContextProvider.tsx b/vike-solid/renderer/PageContextProvider.tsx index 8556cd0..b0906bf 100644 --- a/vike-solid/renderer/PageContextProvider.tsx +++ b/vike-solid/renderer/PageContextProvider.tsx @@ -3,7 +3,7 @@ export { usePageContext }; import { useContext, createContext, type JSX } from "solid-js"; import { type Store } from "solid-js/store"; -import type { PageContext } from "./types"; +import type { PageContext } from "vite-plugin-ssr/types"; import { getGlobalObject } from "./utils/getGlobalObject"; const { Context } = getGlobalObject("PageContextProvider.ts", { diff --git a/vike-solid/renderer/getPageElement.tsx b/vike-solid/renderer/getPageElement.tsx index 244dbc6..3107f82 100644 --- a/vike-solid/renderer/getPageElement.tsx +++ b/vike-solid/renderer/getPageElement.tsx @@ -1,6 +1,6 @@ export { getPageElement }; -import type { PageContext } from "./types"; +import type { PageContext } from "vite-plugin-ssr/types"; import { PageContextProvider, usePageContext } from "./PageContextProvider"; import type { JSX } from "solid-js"; import { Dynamic } from "solid-js/web"; diff --git a/vike-solid/renderer/types.ts b/vike-solid/renderer/types.ts index b6efc2b..c66c093 100644 --- a/vike-solid/renderer/types.ts +++ b/vike-solid/renderer/types.ts @@ -1,30 +1,15 @@ -export type { PageContextServer }; -export type { PageContextClient }; -export type { PageContext }; -export type { PageProps }; -export type { Page }; +export type { Component } from "solid-js"; -import type { - PageContextBuiltInServer, - PageContextBuiltInClientWithClientRouting as PageContextBuiltInClient, -} from "vite-plugin-ssr/types"; import type { JSX } from "solid-js"; -export type { Component } from "solid-js"; - type Page = (pageProps: PageProps) => JSX.Element; type PageProps = Record; -type WrapperComponent = ({ children }: { children: any }) => JSX.Element; - -export type PageContextCommon = { - Page: Page; - pageProps?: PageProps; - config: { - Layout?: WrapperComponent; - Wrapper?: WrapperComponent; - }; -}; -type PageContextServer = PageContextBuiltInServer & PageContextCommon; -type PageContextClient = PageContextBuiltInClient & PageContextCommon; -type PageContext = PageContextClient | PageContextServer; +declare global { + namespace Vike { + interface PageContext { + Page: Page; + pageProps: Record; + } + } +}