Skip to content

Commit c9735e9

Browse files
committed
Make preview SSR only
1 parent a3758ba commit c9735e9

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Diff for: runtime/fresh/plugin.tsx

+33-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import type { AppManifest, SiteInfo } from "@deco/deco";
44
import { Deco, type PageData, type PageParams } from "@deco/deco";
55
import { framework as htmxFramework } from "@deco/deco/htmx";
6-
import type { ComponentType } from "preact";
6+
import type { ComponentChildren, ComponentType } from "preact";
77
import framework from "./Bindings.tsx";
88
import type { Plugin } from "$fresh/server.ts";
9+
import { renderToString } from "preact-render-to-string";
10+
import { HEAD_CONTEXT } from "$fresh/src/runtime/head.ts";
911
export type { Plugin } from "$fresh/server.ts";
1012

1113
export interface PluginRoute {
@@ -65,12 +67,41 @@ export const component = ({ data }: PageParams<PageData>) => {
6567
return <data.page.Component {...data.page.props} />;
6668
};
6769

70+
const adaptRenderOptions = (
71+
render: (data: unknown) => Promise<Response> | Response,
72+
) => {
73+
return (data: PageData) => {
74+
if (data.page.props.options?.serverSideOnly) {
75+
const headContextValue: ComponentChildren[] = [];
76+
// Fill the headContextValue with <Head> from the page.
77+
let res = renderToString(
78+
<HEAD_CONTEXT.Provider value={headContextValue}>
79+
{data.page.Component(data.page.props)}
80+
</HEAD_CONTEXT.Provider>,
81+
);
82+
// Render <Head> to string and add it to the head of the page.
83+
const headContent = headContextValue.map((child) => {
84+
try {
85+
return renderToString(child);
86+
} catch (error) {
87+
console.error("Error rendering to string:", error);
88+
return null;
89+
}
90+
}).filter((content) => content !== null).join("");
91+
res =
92+
`<!DOCTYPE html><html><head><meta charset="utf-8">${headContent}</head>${res}</html>`;
93+
return new Response(res);
94+
}
95+
return render(data);
96+
};
97+
};
98+
6899
export function createFreshHandler<M extends AppManifest = AppManifest>(
69100
deco: Deco<M>,
70101
) {
71102
const h: PluginRoute["handler"] = (req: Request, ctx) => {
72103
return deco.handler(req, {
73-
RENDER_FN: ctx.render.bind(ctx),
104+
RENDER_FN: adaptRenderOptions(ctx.render.bind(ctx)),
74105
GLOBALS: ctx.state.global,
75106
});
76107
};

Diff for: runtime/routes/blockPreview.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ export const render = async (
9999
return await ctx.render({
100100
page: {
101101
Component: Preview,
102-
props: { url: ctx.var.url, params: ctx.req.param(), data: page },
102+
props: {
103+
url: ctx.var.url,
104+
params: ctx.req.param(),
105+
data: page,
106+
options: { serverSideOnly: true },
107+
},
103108
},
104109
});
105110
} finally {

Diff for: runtime/routes/previews.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const handler = createHandler((ctx) => {
4747
return ctx.render({
4848
page: {
4949
Component: Preview,
50-
props: {},
50+
props: { options: { serverSideOnly: true } },
5151
},
5252
});
5353
});

0 commit comments

Comments
 (0)