Skip to content

Commit

Permalink
Merge pull request #16 from AurelienLourot/quality/pagecontext-interf…
Browse files Browse the repository at this point in the history
…ace-merging

qual: interface merging for Vike.PageContext
  • Loading branch information
magne4000 authored Sep 25, 2023
2 parents 32f1ba6 + 29ffdbe commit bd03f44
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 73 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.

6 changes: 3 additions & 3 deletions vike-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"dev": "rollup -c rollup.config.js --watch",
"build": "rollup -c rollup.config.js",
"build": "tsc --noEmit && rollup -c rollup.config.js",
"release": "pnpm run build && bumpp --commit --push --tag && pnpm publish"
},
"dependencies": {
Expand All @@ -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
11 changes: 6 additions & 5 deletions vike-solid/renderer/+config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Config, ConfigEffect } from "vite-plugin-ssr/types";
import type { Config, ConfigEffect, PageContext } from "vite-plugin-ssr/types";
// We purposely define the ConfigVikeSolid interface in this file: that way we ensure it's always applied whenever the user `import vikeSolid from 'vike-solid'`
import type { Component } from "./types.js";

// Depending on the value of `config.meta.ssr`, set other config options' `env`
// accordingly.
Expand Down Expand Up @@ -54,15 +56,13 @@ export default {
},
} satisfies Config;

// We purposely define the ConfigVikeSolid interface in this file: that way we ensure it's always applied whenever the user `import vikeSolid from 'vike-solid'`
import type { Component } from "./types.js";
declare global {
namespace VikePackages {
export interface ConfigVikeSolid {
interface ConfigVikeSolid {
/** Solid element renderer and appended into <head></head> */
Head?: Component;
Layout?: Component;
title?: string;
title?: string | ((pageContext: PageContext) => string);
description?: string;
/**
* @default 'en'
Expand All @@ -80,6 +80,7 @@ declare global {
*
*/
ssr?: boolean;
/** The page's root Solid component */
Page?: Component;
}
}
Expand Down
2 changes: 1 addition & 1 deletion vike-solid/renderer/+onRenderClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default onRenderClient;

import { hydrate, render } from "solid-js/web";
import { getTitle } from "./getTitle";
import type { PageContextClient } from "./types";
import type { PageContextClient } from "vite-plugin-ssr/types";
import { getPageElement } from "./getPageElement";
import { createStore, reconcile } from "solid-js/store";

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
23 changes: 5 additions & 18 deletions vike-solid/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
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";
import type { Store } from "solid-js/store";

function getPageElement(pageContext: Store<PageContext>): JSX.Element {
export function getPageElement(pageContext: Store<PageContext>): JSX.Element {
const page = (
<PageContextProvider pageContext={pageContext}>
<Wrapper>
<Layout>
<Page />
</Layout>
</Wrapper>
<Layout>
<Page />
</Layout>
</PageContextProvider>
);
return page;
}

function Wrapper(props: { children: JSX.Element }) {
const pageContext = usePageContext();
return (
<Dynamic component={pageContext.config.Wrapper ?? Passthrough}>
{props.children}
</Dynamic>
);
}

function Layout(props: { children: JSX.Element }) {
const pageContext = usePageContext();
return (
Expand Down
8 changes: 2 additions & 6 deletions vike-solid/renderer/getTitle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
export { getTitle };

import type { ConfigEntries } from "vite-plugin-ssr/types";
import type { PageContext } from "vite-plugin-ssr/types";

function getTitle(pageContext: {
title?: unknown;
config: Record<string, unknown>;
configEntries: ConfigEntries;
}): null | string {
function getTitle(pageContext: PageContext): null | string {
if (typeof pageContext.title === "string") {
return pageContext.title;
}
Expand Down
34 changes: 10 additions & 24 deletions vike-solid/renderer/types.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
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>;
title?: string;
}
}
}

0 comments on commit bd03f44

Please sign in to comment.