feat(server): Add basePath config to host from a directory (#2092)

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
Reed von Redwitz
2023-11-29 16:44:25 +00:00
committed by GitHub
co-authored by Marvin Hagemeister
parent fe792455ab
commit 9ab8651018
40 changed files with 810 additions and 28 deletions
+42 -1
View File
@@ -11,7 +11,7 @@ import {
} from "preact";
import { assetHashingHook } from "../../runtime/utils.ts";
import { Partial, PartialProps } from "../../runtime/Partial.tsx";
import { renderToString } from "../deps.ts";
import { join, renderToString, SEP } from "../deps.ts";
import { RenderState } from "./state.ts";
import { Island } from "../types.ts";
import {
@@ -21,6 +21,7 @@ import {
PartialMode,
} from "../../constants.ts";
import { setActiveUrl } from "../../runtime/active_url.ts";
import { withBase } from "../router.ts";
// See: https://github.com/preactjs/preact/blob/7748dcb83cedd02e37b3713634e35b97b26028fd/src/internal.d.ts#L3C1-L16
enum HookType {
@@ -207,6 +208,46 @@ options.vnode = (vnode) => {
if (typeof props[CLIENT_NAV_ATTR] === "boolean") {
props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : "false";
}
if (typeof props.href === "string") {
props.href = withBase(props.href, current?.basePath);
}
if (typeof props.src === "string") {
props.src = withBase(props.src, current?.basePath);
}
srcsetRewrite:
if (typeof props.srcset === "string") {
// Bail out on complex syntax that's too complicated for now
if (props.srcset.includes("(")) break srcsetRewrite;
const parts = props.srcset.split(",");
const out: string[] = [];
for (const part of parts) {
const trimmed = part.trimStart();
if (trimmed === "") break srcsetRewrite;
let urlEnd = trimmed.indexOf(" ");
if (urlEnd === -1) urlEnd = trimmed.length;
const leadingWhitespace = part.length - trimmed.length;
const leading = part.substring(0, leadingWhitespace);
const url = trimmed.substring(0, urlEnd);
const trailing = trimmed.substring(urlEnd);
if (url.startsWith("/") && current?.basePath) {
const joinedPath = join("/", current.basePath, url).replaceAll(
SEP,
"/",
);
out.push(leading + joinedPath + trailing);
} else {
out.push(part);
}
}
props.srcset = out.join(",");
}
} else if (
current && typeof vnode.type === "function" && vnode.type !== Fragment &&
ownerStack.length > 0
+3
View File
@@ -12,6 +12,7 @@ export interface RenderStateRouteOptions {
state?: any;
error?: unknown;
params: Record<string, string | string[]>;
basePath: string;
}
export class RenderState {
@@ -44,6 +45,7 @@ export class RenderState {
partialDepth = 0;
islandDepth = 0;
url: URL;
basePath: string;
constructor(
routeOptions: RenderStateRouteOptions,
@@ -57,6 +59,7 @@ export class RenderState {
this.componentStack = componentStack;
this.url = routeOptions.url;
this.isPartial = routeOptions.url.searchParams.has(PARTIAL_SEARCH_PARAM);
this.basePath = routeOptions.basePath;
if (error) this.routeOptions.error = error;
}
+7 -2
View File
@@ -11,6 +11,7 @@ import {
} from "preact";
import { HEAD_CONTEXT } from "../../runtime/head.ts";
import { CSP_CONTEXT } from "../../runtime/csp.ts";
import { withBase } from "$fresh/src/server/router.ts";
export function renderHtml(state: RenderState) {
setRenderState(state);
@@ -121,10 +122,14 @@ export function renderOuterDocument(
docTitle,
docHeadNodes.map((node) => h(node.type, node.props)),
opts.preloads.map((src) =>
h("link", { rel: "modulepreload", href: src })
h("link", { rel: "modulepreload", href: withBase(src, state.basePath) })
),
opts.moduleScripts.map(([src, nonce]) =>
h("script", { src: src, nonce, type: "module" })
h("script", {
src: withBase(src, state.basePath),
nonce,
type: "module",
})
),
filteredHeadNodes,
),