Skip to content

Commit 188ef82

Browse files
committed
feat: update getServerSideProps to use event context and remove unused code
1 parent 0a90718 commit 188ef82

File tree

6 files changed

+22
-35
lines changed

6 files changed

+22
-35
lines changed

examples/basic/src/pages/products/[id]/[name]/page.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ export default function ProductIdPage(props: { id: string; name: string }) {
1717
export const getServerSideProps: GetServerSidePropsFunction<{
1818
id: string;
1919
name: string;
20-
}> = async () => {
21-
return {
22-
id: Math.random().toFixed(10),
23-
name: "Pedro",
24-
};
20+
}> = async ({ event }) => {
21+
return event.context.params as { id: string; name: string };
2522
};

examples/basic/src/pages/products/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "./products.css";
55

66
export default function ProductsPage(props: { cuantity: number }) {
77
const { props_status } = useAppContext();
8-
console.log(props_status);
98

109
return (
1110
<div>

packages/pranx/src/client/app-context.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export const AppContextProvider = (props: PropsWithChildren) => {
5252
}
5353

5454
try {
55-
const response = await fetch(next_route_data.server_data_api_url);
55+
const targetUrl = new URL(window.location.href);
56+
targetUrl.searchParams.set("props", "only");
57+
58+
const response = await fetch(targetUrl);
5659
const json_data = (await response.json()) as { props: Record<string, any> };
5760
set_page_props(json_data.props);
5861
onRouteChangeUpdateHead(next_route_data);

packages/pranx/src/cmd/build.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { logger } from "@/utils/logger.js";
33
import { measureTime } from "@/utils/time-perf.js";
44
import fse from "fs-extra";
55
import kleur from "kleur";
6-
import crypto from "node:crypto";
76
import { join } from "pathe";
87
import { Fragment, h } from "preact";
98
import { renderToStringAsync } from "preact-render-to-string";
@@ -148,7 +147,6 @@ path: ${final_path}`);
148147
css: [css_output.entry, css_output[final_path] || ""].filter(Boolean),
149148
static_generated_routes: [],
150149
absolute_module_path: module_path,
151-
server_data_api_key: "",
152150
});
153151

154152
for (const static_path of static_paths_result.paths) {
@@ -199,7 +197,6 @@ params returned by getStaticPaths: ${JSON.stringify(static_path.params)}`);
199197
module: pages_relative_path,
200198
props: statics_fn_result.props,
201199
rendering_kind: isStatic ? "static" : "server-side",
202-
server_data_api_key: crypto.randomUUID(),
203200
revalidate: statics_fn_result.revalidate || -1,
204201
static_generated_routes: [],
205202
is_dynamic: isUrlDynamic,
@@ -219,7 +216,6 @@ params returned by getStaticPaths: ${JSON.stringify(static_path.params)}`);
219216
css: r.css,
220217
is_dynamic: r.is_dynamic,
221218
path_parsed_for_routing: filePathToRoutingPath(r.path),
222-
server_data_api_url: `/_internal_/${r.server_data_api_key}`,
223219
static_generated_routes: r.static_generated_routes.map((r) => {
224220
return {
225221
path: r.path,

packages/pranx/src/cmd/start.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const createServeStatic = (event: H3Event<EventHandlerRequest>) =>
7979

8080
return undefined;
8181
},
82+
8283
headers: {
8384
"Cache-Control": "public, max-age=2592000, immutable", // agresive caching
8485
"Expires": new Date(Date.now() + 2592000000).toUTCString(), // one month
@@ -109,7 +110,17 @@ const attach_server_side_pages_to_app = async (server_manifest: SERVER_MANIFEST,
109110
let props_to_return = {};
110111

111112
if (getServerSideProps) {
112-
props_to_return = await getServerSideProps();
113+
props_to_return = await getServerSideProps({
114+
event,
115+
});
116+
}
117+
118+
event.res.headers.set("Cache-Control", "private, no-cache, no-store, must-revalidate");
119+
120+
if (event.url.searchParams.get("props") === "only") {
121+
return {
122+
props: props_to_return,
123+
};
113124
}
114125

115126
const target_route_index = hydrate_data.routes.findIndex((r) => r.path === route.path);
@@ -133,28 +144,9 @@ const attach_server_side_pages_to_app = async (server_manifest: SERVER_MANIFEST,
133144
css: route.css,
134145
});
135146

136-
event.res.headers.set("Cache-Control", "private, no-cache, no-store, must-revalidate");
137147
return html(event, html_string);
138148
})
139149
);
140-
141-
app.on(
142-
"GET",
143-
`/_internal_/${route.server_data_api_key}`,
144-
defineHandler(async (event) => {
145-
let props_to_return = {};
146-
147-
if (getServerSideProps) {
148-
props_to_return = await getServerSideProps();
149-
}
150-
151-
event.res.headers.set("Cache-Control", "no-store");
152-
153-
return {
154-
props: props_to_return,
155-
};
156-
})
157-
);
158150
}
159151
}
160152
};

packages/pranx/types/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventHandlerRequest, H3Event } from "h3";
12
import { VNode } from "preact";
23
import { PropsWithChildren } from "preact/compat";
34
import { useHead } from "unhead";
@@ -29,8 +30,9 @@ export type GetStaticPropsFunction<
2930
> = (context: { params: Params }) => Promise<GetStaticPropsResult<Props>>;
3031

3132
// Server Side
32-
export type GetServerSidePropsFunction<Props extends Record<string, any> = {}> =
33-
() => Promise<Props>;
33+
export type GetServerSidePropsFunction<Props extends Record<string, any> = {}> = (context: {
34+
event: H3Event<EventHandlerRequest>;
35+
}) => Promise<Props>;
3436

3537
// Meta
3638
export type MetaFunction = typeof useHead;
@@ -69,7 +71,6 @@ export type ServerManifestRoute = {
6971
is_dynamic: boolean;
7072
dynamic_params: Array<string>;
7173
css: string[];
72-
server_data_api_key: string;
7374
};
7475

7576
export type SERVER_MANIFEST = {
@@ -86,7 +87,6 @@ export type HydrateDataRoute = {
8687
css: string[];
8788
path_parsed_for_routing: string;
8889
static_generated_routes: Array<{ path: string; props: Record<string, any> }>;
89-
server_data_api_url: string;
9090
};
9191

9292
export type HYDRATE_DATA = {

0 commit comments

Comments
 (0)