Skip to content

Commit

Permalink
qual: add hook types
Browse files Browse the repository at this point in the history
  • Loading branch information
lourot committed Oct 23, 2023
1 parent 0581094 commit 532ae32
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 65 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:*",
"vike": "^0.4.142"
"vike": "^0.4.144"
},
"devDependencies": {
"typescript": "^5.1.6"
Expand Down
11 changes: 7 additions & 4 deletions examples/basic/pages/star-wars/@id/+onBeforeRender.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export default onBeforeRender;
// https://vike.dev/onBeforeRender
export { onBeforeRender };

import fetch from "cross-fetch";
import type { PageContext } from "vike/types";
import type { OnBeforeRenderAsync } from "vike/types";
import { filterMovieData } from "../filterMovieData";
import type { MovieDetails } from "../types";

async function onBeforeRender(pageContext: PageContext) {
const onBeforeRender: OnBeforeRenderAsync = async (
pageContext
): ReturnType<OnBeforeRenderAsync> => {
const response = await fetch(
`https://star-wars.brillout.com/api/films/${pageContext.routeParams?.id}.json`
);
Expand All @@ -26,4 +29,4 @@ async function onBeforeRender(pageContext: PageContext) {
title,
},
};
}
};
8 changes: 6 additions & 2 deletions examples/basic/pages/star-wars/index/+onBeforeRender.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export default onBeforeRender;
// https://vike.dev/onBeforeRender
export { onBeforeRender };

import fetch from "node-fetch";
//import { filterMovieData } from '../filterMovieData'
import type { Movie, MovieDetails } from "../types";
import type { OnBeforeRenderAsync } from "vike/types";

// export { prerender }

async function onBeforeRender() {
const onBeforeRender: OnBeforeRenderAsync = async (
pageContext
): ReturnType<OnBeforeRenderAsync> => {
const movies = await getStarWarsMovies();
return {
pageContext: {
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:*",
"vike": "^0.4.142"
"vike": "^0.4.144"
},
"devDependencies": {
"typescript": "^5.1.6"
Expand Down
50 changes: 8 additions & 42 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.8.3",
"vite": "^4.5.0",
"vike": "^0.4.143"
"vike": "^0.4.144"
},
"devDependencies": {
"@babel/core": "^7.23.2",
Expand All @@ -37,7 +37,7 @@
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^4.5.0",
"vike": "^0.4.143"
"vike": "^0.4.144"
},
"typesVersions": {
"*": {
Expand Down
4 changes: 2 additions & 2 deletions vike-solid/renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const toggleSsrRelatedConfig: ConfigEffect = ({
};

export default {
onRenderHtml: "import:vike-solid/renderer/onRenderHtml",
onRenderClient: "import:vike-solid/renderer/onRenderClient",
onRenderHtml: "import:vike-solid/renderer/onRenderHtml:onRenderHtml",
onRenderClient: "import:vike-solid/renderer/onRenderClient:onRenderClient",
passToClient: ["pageProps", "title"],
clientRouting: true,
hydrationCanBeAborted: true,
Expand Down
9 changes: 6 additions & 3 deletions vike-solid/renderer/+onRenderClient.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export default onRenderClient;
// https://vike.dev/onRenderClient
export { onRenderClient };

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

Expand All @@ -13,7 +14,9 @@ const [pageContextStore, setPageContext] = createStore<PageContextClient>(
let dispose: () => void;
let rendered = false;

async function onRenderClient(pageContext: PageContextClient) {
const onRenderClient: OnRenderClientAsync = async (
pageContext
): ReturnType<OnRenderClientAsync> => {
if (!rendered) {
// Dispose to prevent duplicate pages when navigating.
if (dispose) dispose();
Expand Down
22 changes: 14 additions & 8 deletions vike-solid/renderer/+onRenderHtml.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
export default onRenderHtml;
import { generateHydrationScript, renderToStream, renderToString } from "solid-js/web";
// https://vike.dev/onRenderHtml
export { onRenderHtml };

import {
generateHydrationScript,
renderToStream,
renderToString,
} from "solid-js/web";
import { dangerouslySkipEscape, escapeInject, stampPipe } from "vike/server";
import { getTitle } from "./getTitle";
import { getPageElement } from "./getPageElement";
import type { PageContext } from "vike/types";
import type { OnRenderHtmlAsync } from "vike/types";
import { PageContextProvider } from "./PageContextProvider";

async function onRenderHtml(pageContext: PageContext) {
const onRenderHtml: OnRenderHtmlAsync = async (
pageContext
): ReturnType<OnRenderHtmlAsync> => {
const title = getTitle(pageContext);
const titleTag = !title ? "" : escapeInject`<title>${title}</title>`;

Expand Down Expand Up @@ -52,7 +60,5 @@ async function onRenderHtml(pageContext: PageContext) {
</body>
</html>`;

return {
documentHtml,
};
}
return documentHtml;
};

0 comments on commit 532ae32

Please sign in to comment.