Skip to content

Commit 67ead43

Browse files
authored
Merge branch 'main' into rename-FreshContext-req-to-request
2 parents 2ca696d + b0e75b7 commit 67ead43

21 files changed

Lines changed: 198 additions & 205 deletions

deno.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/fresh/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fresh/core",
3-
"version": "2.0.0-alpha.62",
3+
"version": "2.0.0-alpha.63",
44
"license": "MIT",
55
"exports": {
66
".": "./src/mod.ts",

packages/fresh/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class App<State> {
179179

180180
constructor(config: FreshConfig = {}) {
181181
this.config = {
182-
root: Deno.cwd(),
182+
root: ".",
183183
basePath: config.basePath ?? "",
184184
mode: config.mode ?? "production",
185185
};

packages/fresh/src/context.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import { SpanStatusCode } from "@opentelemetry/api";
1212
import type { ResolvedFreshConfig } from "./config.ts";
1313
import type { BuildCache } from "./build_cache.ts";
1414
import type { LayoutConfig } from "./types.ts";
15-
import { RenderState, setRenderState } from "./runtime/server/preact_hooks.tsx";
16-
import { PARTIAL_SEARCH_PARAM } from "./constants.ts";
15+
import {
16+
FreshScripts,
17+
RenderState,
18+
setRenderState,
19+
} from "./runtime/server/preact_hooks.tsx";
20+
import { DEV_ERROR_OVERLAY_URL, PARTIAL_SEARCH_PARAM } from "./constants.ts";
1721
import { tracer } from "./otel.ts";
1822
import {
1923
type ComponentDef,
2024
isAsyncAnyComponent,
2125
type PageProps,
22-
preactRender,
2326
renderAsyncAnyComponent,
2427
renderRouteComponent,
2528
} from "./render.ts";
29+
import { renderToString } from "preact-render-to-string";
2630

2731
export interface Island {
2832
file: string;
@@ -281,22 +285,47 @@ export class Context<State> {
281285
try {
282286
setRenderState(state);
283287

284-
const inner = preactRender(
288+
let html = renderToString(
285289
vnode ?? h(Fragment, null),
286-
this,
287-
state,
288-
!hasApp,
289290
);
290291

291-
if (!hasApp) {
292-
return inner;
292+
if (hasApp) {
293+
appChild = jsxTemplate([html]);
294+
html = renderToString(appVNode);
293295
}
294296

295-
appChild = jsxTemplate([inner]);
296-
297-
const outer = preactRender(appVNode, this, state, true);
297+
if (
298+
!state.renderedHtmlBody || !state.renderedHtmlHead ||
299+
!state.renderedHtmlTag
300+
) {
301+
let fallback: VNode = jsxTemplate([html]);
302+
if (!state.renderedHtmlBody) {
303+
let scripts: VNode | null = null;
304+
305+
if (
306+
this.url.pathname !== this.config.basePath + DEV_ERROR_OVERLAY_URL
307+
) {
308+
scripts = h(FreshScripts, null) as VNode;
309+
}
310+
311+
fallback = h("body", null, fallback, scripts);
312+
}
313+
if (!state.renderedHtmlHead) {
314+
fallback = h(
315+
Fragment,
316+
null,
317+
h("head", null, h("meta", { charset: "utf-8" })),
318+
fallback,
319+
);
320+
}
321+
if (!state.renderedHtmlTag) {
322+
fallback = h("html", null, fallback);
323+
}
324+
325+
html = renderToString(fallback);
326+
}
298327

299-
return outer;
328+
return `<!DOCTYPE html>${html}`;
300329
} catch (err) {
301330
if (err instanceof Error) {
302331
span.recordException(err);

packages/fresh/src/render.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ import {
55
type RenderableProps,
66
type VNode,
77
} from "preact";
8-
import {
9-
FreshScripts,
10-
type RenderState,
11-
} from "./runtime/server/preact_hooks.tsx";
128
import type { Context } from "./context.ts";
139
import { recordSpanError, tracer } from "./otel.ts";
14-
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
15-
import { renderToString } from "preact-render-to-string";
16-
import { escape as escapeHtml } from "@std/html";
1710

1811
export type AsyncAnyComponent<P> = {
1912
(
@@ -56,46 +49,6 @@ export async function renderAsyncAnyComponent<Props>(
5649
);
5750
}
5851

59-
export function preactRender<State, Data>(
60-
vnode: VNode,
61-
ctx: PageProps<Data, State>,
62-
state: RenderState,
63-
renderFallback: boolean,
64-
) {
65-
let res = renderToString(vnode);
66-
67-
if (!renderFallback) return res;
68-
69-
// We require a the full outer DOM structure so that browser put
70-
// comment markers in the right place in the DOM.
71-
if (!state.renderedHtmlBody) {
72-
let scripts = "";
73-
if (ctx.url.pathname !== ctx.config.basePath + DEV_ERROR_OVERLAY_URL) {
74-
scripts = renderToString(h(FreshScripts, null));
75-
}
76-
res = `<body>${res}${scripts}</body>`;
77-
}
78-
if (!state.renderedHtmlHead) {
79-
let head = `<head><meta charset="utf-8">`;
80-
81-
const entryAssets = state.buildCache.getEntryAssets();
82-
for (let i = 0; i < entryAssets.length; i++) {
83-
const asset = entryAssets[i];
84-
85-
if (asset.endsWith(".css")) {
86-
head += `<link rel="stylesheet" href="${escapeHtml(asset)}">`;
87-
}
88-
}
89-
90-
res = `${head}</head>${res}`;
91-
}
92-
if (!state.renderedHtmlTag) {
93-
res = `<html>${res}</html>`;
94-
}
95-
96-
return `<!DOCTYPE html>${res}`;
97-
}
98-
9952
export type PageProps<Data = unknown, T = unknown> =
10053
& Pick<
10154
Context<T>,

packages/init/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fresh/init",
3-
"version": "2.0.0-alpha.62",
3+
"version": "2.0.0-alpha.63",
44
"license": "MIT",
55
"exports": "./src/mod.ts",
66
"exclude": ["**/tmp/*"],

packages/init/src/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as colors from "@std/fmt/colors";
33
import * as path from "@std/path";
44

55
// Keep these as is, as we replace these version in our release script
6-
const FRESH_VERSION = "2.0.0-alpha.62";
6+
const FRESH_VERSION = "2.0.0-alpha.63";
77
const FRESH_TAILWIND_VERSION = "0.0.1-alpha.9";
88
const PREACT_VERSION = "10.27.1";
99
const PREACT_SIGNALS_VERSION = "2.3.1";

packages/plugin-vite/deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fresh/plugin-vite",
3-
"version": "0.9.3",
3+
"version": "0.9.5",
44
"license": "MIT",
55
"exports": {
66
".": "./src/mod.ts"
@@ -23,6 +23,7 @@
2323
"@babel/preset-react": "npm:@babel/preset-react@^7.27.1",
2424
"@deno/loader": "jsr:@deno/loader@^0.3.2",
2525
"@marvinh-test/import-json": "jsr:@marvinh-test/import-json@^0.0.1",
26+
"@mjackson/node-fetch-server": "npm:@mjackson/node-fetch-server@^0.7.0",
2627
"@prefresh/vite": "npm:@prefresh/vite@^2.4.8",
2728
"@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12",
2829
"@types/babel__core": "npm:@types/babel__core@^7.20.5",

packages/plugin-vite/src/plugins/client_entry.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
66
const modNameUser = "fresh:client-entry-user";
77

88
let clientEntry = "";
9+
let isDev = false;
910

1011
return {
1112
name: "fresh:client-entry",
13+
config(_, env) {
14+
isDev = env.command === "serve";
15+
},
1216
applyToEnvironment(env) {
1317
return env.name === "client";
1418
},
@@ -33,7 +37,9 @@ export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
3337
exists = false;
3438
}
3539

36-
return `export * from "fresh/runtime-client";
40+
return `
41+
${isDev ? 'import "preact/debug"' : ""}
42+
export * from "fresh/runtime-client";
3743
${exists ? `import "fresh:client-entry-user";` : ""}
3844
3945
if (import.meta.hot) {

packages/plugin-vite/src/plugins/deno.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as path from "@std/path";
1010
import * as babel from "@babel/core";
1111
import babelReact from "@babel/preset-react";
1212
import { httpAbsolute } from "./patches/http_absolute.ts";
13+
import { JS_REG } from "../utils.ts";
1314

1415
interface DenoState {
1516
type: RequestedModuleType;
@@ -180,7 +181,7 @@ export function deno(): Plugin {
180181
// Skip for non-js files like `.css`
181182
if (
182183
meta.type === RequestedModuleType.Default &&
183-
!/\.([tj]sx?|[mc]?[tj]s)$/.test(id)
184+
!JS_REG.test(id)
184185
) {
185186
return;
186187
}
@@ -359,6 +360,7 @@ function babelTransform(
359360
sourceMaps: "inline",
360361
presets: presets,
361362
plugins: [httpAbsolute(url)],
363+
compact: true,
362364
});
363365

364366
if (result !== null && result.code) {

0 commit comments

Comments
 (0)