Skip to content

Commit

Permalink
qual: interface merging for Vike.PageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
lourot committed Sep 23, 2023
1 parent 32f1ba6 commit 651378b
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions examples/basic/pages/star-wars/@id/+onBeforeRender.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion examples/ssr-spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vike-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
"*": {
Expand Down
3 changes: 2 additions & 1 deletion vike-solid/renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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></head> */
Head?: Component;
Layout?: Component;
Expand All @@ -80,6 +80,7 @@ declare global {
*
*/
ssr?: boolean;
/** The page's root Solid component */
Page?: Component;
}
}
Expand Down
8 changes: 4 additions & 4 deletions vike-solid/renderer/+onRenderClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageContextClient>(
{} as PageContextClient
const [pageContextStore, setPageContext] = createStore<PageContext>(
{} 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();
Expand Down
4 changes: 2 additions & 2 deletions vike-solid/renderer/+onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>${title}</title>`;

Expand Down
2 changes: 1 addition & 1 deletion vike-solid/renderer/PageContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion vike-solid/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
33 changes: 9 additions & 24 deletions vike-solid/renderer/types.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;
type WrapperComponent = ({ children }: { children: any }) => JSX.Element;

export type PageContextCommon = {
Page: Page;
pageProps?: PageProps;
config: {
Layout?: WrapperComponent;
Wrapper?: WrapperComponent;
};
};

type PageContextServer = PageContextBuiltInServer<Page> & PageContextCommon;
type PageContextClient = PageContextBuiltInClient<Page> & PageContextCommon;
type PageContext = PageContextClient | PageContextServer;
declare global {
namespace Vike {
interface PageContext {
Page: Page;
pageProps: Record<string, unknown>;
}
}
}

0 comments on commit 651378b

Please sign in to comment.