@eserstack/laroux-server 0.0.0-bootstrap.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +411 -0
- package/dist/adapters/react/error-chunk.d.ts +11 -0
- package/dist/adapters/react/error-chunk.js +12 -0
- package/dist/adapters/react/html-shell.d.ts +15 -0
- package/dist/adapters/react/html-shell.js +109 -0
- package/dist/adapters/react/inline-rsc-emitter.d.ts +62 -0
- package/dist/adapters/react/inline-rsc-emitter.js +120 -0
- package/dist/adapters/react/mod.d.ts +30 -0
- package/dist/adapters/react/mod.js +36 -0
- package/dist/adapters/react/render-app.d.ts +46 -0
- package/dist/adapters/react/render-app.js +44 -0
- package/dist/adapters/react/renderer.d.ts +15 -0
- package/dist/adapters/react/renderer.js +86 -0
- package/dist/adapters/react/rsc-flight-renderer.d.ts +64 -0
- package/dist/adapters/react/rsc-flight-renderer.js +623 -0
- package/dist/adapters/react/rsc-handler.d.ts +47 -0
- package/dist/adapters/react/rsc-handler.js +114 -0
- package/dist/adapters/react/script-json.d.ts +12 -0
- package/dist/adapters/react/script-json.js +18 -0
- package/dist/adapters/react/ssr-renderer.d.ts +63 -0
- package/dist/adapters/react/ssr-renderer.js +625 -0
- package/dist/bin-main.d.ts +1 -0
- package/dist/bin-main.js +18 -0
- package/dist/cli.d.ts +26 -0
- package/dist/cli.js +48 -0
- package/dist/commands/build.d.ts +78 -0
- package/dist/commands/build.js +221 -0
- package/dist/commands/dev.d.ts +50 -0
- package/dist/commands/dev.js +133 -0
- package/dist/commands/init.d.ts +29 -0
- package/dist/commands/init.js +228 -0
- package/dist/commands/serve.d.ts +47 -0
- package/dist/commands/serve.js +124 -0
- package/dist/config/load-config.d.ts +14 -0
- package/dist/config/load-config.js +67 -0
- package/dist/domain/action-registry.d.ts +83 -0
- package/dist/domain/action-registry.js +124 -0
- package/dist/domain/build-module.d.ts +30 -0
- package/dist/domain/build-module.js +50 -0
- package/dist/domain/hmr-manager.d.ts +51 -0
- package/dist/domain/hmr-manager.js +90 -0
- package/dist/domain/html-shell.d.ts +56 -0
- package/dist/domain/html-shell.js +15 -0
- package/dist/domain/middleware-dispatcher.d.ts +42 -0
- package/dist/domain/middleware-dispatcher.js +150 -0
- package/dist/domain/rebuild-handler.d.ts +52 -0
- package/dist/domain/rebuild-handler.js +52 -0
- package/dist/domain/renderer.d.ts +62 -0
- package/dist/domain/renderer.js +15 -0
- package/dist/domain/route-dispatcher.d.ts +40 -0
- package/dist/domain/route-dispatcher.js +134 -0
- package/dist/domain/server-modules.d.ts +65 -0
- package/dist/domain/server-modules.js +208 -0
- package/dist/main.d.ts +40 -0
- package/dist/main.js +256 -0
- package/dist/module.d.ts +24 -0
- package/dist/module.js +140 -0
- package/dist/options.d.ts +53 -0
- package/dist/options.js +14 -0
- package/dist/package.json +98 -0
- package/dist/proxy/types.d.ts +19 -0
- package/dist/proxy/types.js +3 -0
- package/dist/runtime/action-guard.d.ts +49 -0
- package/dist/runtime/action-guard.js +115 -0
- package/dist/runtime/html-shell.d.ts +66 -0
- package/dist/runtime/html-shell.js +246 -0
- package/dist/runtime/media-types.d.ts +13 -0
- package/dist/runtime/media-types.js +83 -0
- package/dist/runtime/path-containment.d.ts +11 -0
- package/dist/runtime/path-containment.js +27 -0
- package/dist/runtime/seo/generate-meta.d.ts +16 -0
- package/dist/runtime/seo/generate-meta.js +121 -0
- package/dist/runtime/seo/index.d.ts +5 -0
- package/dist/runtime/seo/index.js +5 -0
- package/dist/runtime/seo/types.d.ts +25 -0
- package/dist/runtime/seo/types.js +5 -0
- package/dist/runtime/server.d.ts +58 -0
- package/dist/runtime/server.js +699 -0
- package/package.json +84 -1
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Checks for POST /_rsc, the server action endpoint: which exports a request
|
|
4
|
+
* may call, how large its body may be, and which origins may send it.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
9
|
+
/**
|
|
10
|
+
* The largest server action request body, in bytes: 1 MiB. The body is held
|
|
11
|
+
* in memory to decode the arguments, so the limit bounds the memory one
|
|
12
|
+
* request can take. A larger body is refused with 413 before it is parsed.
|
|
13
|
+
*/
|
|
14
|
+
export const ACTION_MAX_BODY_BYTES = 1_048_576;
|
|
15
|
+
/** The marker React and the laroux build put on every server action. */
|
|
16
|
+
const SERVER_REFERENCE = Symbol.for("react.server.reference");
|
|
17
|
+
/**
|
|
18
|
+
* Reads `<serverDir>/actions-manifest.json`, the list of "use server" modules
|
|
19
|
+
* the build wrote. A build without the file has no actions.
|
|
20
|
+
*/
|
|
21
|
+
export const readActionManifest = async (serverDir) => {
|
|
22
|
+
const manifestPath = runtime.path.join(serverDir, "actions-manifest.json");
|
|
23
|
+
if (!(await runtime.fs.exists(manifestPath))) {
|
|
24
|
+
return { actions: new Set() };
|
|
25
|
+
}
|
|
26
|
+
const parsed = JSON.parse(await runtime.fs.readTextFile(manifestPath));
|
|
27
|
+
const actions = typeof parsed === "object" &&
|
|
28
|
+
parsed !== null &&
|
|
29
|
+
Array.isArray(parsed.actions)
|
|
30
|
+
? parsed.actions.filter((entry) => typeof entry === "string")
|
|
31
|
+
: [];
|
|
32
|
+
return { actions: new Set(actions) };
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Whether the module of an action id (`path/to/file#export`) is one the build
|
|
36
|
+
* lists. Checked before the module is imported, so a request can never load a
|
|
37
|
+
* server module that is not an action module.
|
|
38
|
+
*/
|
|
39
|
+
export const isListedActionModule = (manifest, modulePath) => manifest.actions.has(`${modulePath}.js`);
|
|
40
|
+
/**
|
|
41
|
+
* Whether `value` is the server action `actionId` names: a function that
|
|
42
|
+
* carries React's server reference marker with the same id. Plain exports of
|
|
43
|
+
* an action module, such as helpers, fail this check and cannot be called.
|
|
44
|
+
*/
|
|
45
|
+
export const isServerAction = (value, actionId) => {
|
|
46
|
+
if (typeof value !== "function") {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const marked = value;
|
|
50
|
+
return marked.$$typeof === SERVER_REFERENCE && marked.$$id === actionId;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Whether a browser sent the request from a page of the same origin.
|
|
54
|
+
*
|
|
55
|
+
* With Fetch Metadata, `Sec-Fetch-Site` must be `same-origin` or `none`.
|
|
56
|
+
* Without it, an `Origin` header must equal the request URL's origin. A
|
|
57
|
+
* request with neither header passes: browsers send at least one of them on
|
|
58
|
+
* every POST, so only non-browser clients, which cross-site request forgery
|
|
59
|
+
* cannot drive, omit both.
|
|
60
|
+
*/
|
|
61
|
+
export const isSameOriginRequest = (req) => {
|
|
62
|
+
const fetchSite = req.headers.get("sec-fetch-site");
|
|
63
|
+
if (fetchSite !== null) {
|
|
64
|
+
return fetchSite === "same-origin" || fetchSite === "none";
|
|
65
|
+
}
|
|
66
|
+
const origin = req.headers.get("origin");
|
|
67
|
+
if (origin === null) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return origin === new URL(req.url).origin;
|
|
71
|
+
};
|
|
72
|
+
/** The request body is over {@link ACTION_MAX_BODY_BYTES}. */
|
|
73
|
+
export class ActionBodyTooLargeError extends Error {
|
|
74
|
+
limitBytes;
|
|
75
|
+
constructor(limitBytes) {
|
|
76
|
+
super(`server action request body is over the ${limitBytes}-byte limit`);
|
|
77
|
+
this.limitBytes = limitBytes;
|
|
78
|
+
this.name = "ActionBodyTooLargeError";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Reads the request body into memory, failing with
|
|
83
|
+
* {@link ActionBodyTooLargeError} as soon as it is known to pass
|
|
84
|
+
* `limitBytes`: from Content-Length before any byte is read, otherwise from
|
|
85
|
+
* the bytes counted while reading.
|
|
86
|
+
*/
|
|
87
|
+
export const readBoundedBody = async (req, limitBytes = ACTION_MAX_BODY_BYTES) => {
|
|
88
|
+
const declared = req.headers.get("content-length");
|
|
89
|
+
if (declared !== null &&
|
|
90
|
+
/^\d+$/.test(declared.trim()) &&
|
|
91
|
+
Number(declared.trim()) > limitBytes) {
|
|
92
|
+
await req.body?.cancel();
|
|
93
|
+
throw new ActionBodyTooLargeError(limitBytes);
|
|
94
|
+
}
|
|
95
|
+
if (req.body === null) {
|
|
96
|
+
return new Uint8Array(0);
|
|
97
|
+
}
|
|
98
|
+
const chunks = [];
|
|
99
|
+
let readBytes = 0;
|
|
100
|
+
for await (const chunk of req.body) {
|
|
101
|
+
readBytes += chunk.byteLength;
|
|
102
|
+
if (readBytes > limitBytes) {
|
|
103
|
+
// Leaving the loop early cancels the stream.
|
|
104
|
+
throw new ActionBodyTooLargeError(limitBytes);
|
|
105
|
+
}
|
|
106
|
+
chunks.push(chunk);
|
|
107
|
+
}
|
|
108
|
+
const bytes = new Uint8Array(readBytes);
|
|
109
|
+
let offset = 0;
|
|
110
|
+
for (const chunk of chunks) {
|
|
111
|
+
bytes.set(chunk, offset);
|
|
112
|
+
offset += chunk.byteLength;
|
|
113
|
+
}
|
|
114
|
+
return bytes;
|
|
115
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML Shell Template
|
|
3
|
+
* Provides the initial HTML structure for the React app with embedded chunk manifest
|
|
4
|
+
*/
|
|
5
|
+
import type { ChunkManifest } from "@eserstack/laroux-bundler";
|
|
6
|
+
import type { SeoConfig } from "./seo/index.ts";
|
|
7
|
+
/**
|
|
8
|
+
* Options for generating the HTML shell
|
|
9
|
+
*/
|
|
10
|
+
export type HtmlShellOptions = {
|
|
11
|
+
/** Chunk manifest with component mappings */
|
|
12
|
+
chunkManifest: ChunkManifest;
|
|
13
|
+
/** Entry point script path */
|
|
14
|
+
entrypoint: string;
|
|
15
|
+
/** Font preload hints (optional) */
|
|
16
|
+
fontPreloads?: string[];
|
|
17
|
+
/** Font face CSS (optional) */
|
|
18
|
+
fontCSS?: string;
|
|
19
|
+
/** SEO configuration override (optional) */
|
|
20
|
+
seoConfig?: Partial<SeoConfig>;
|
|
21
|
+
/** Current locale (optional) */
|
|
22
|
+
locale?: string;
|
|
23
|
+
/** Critical chunks to preload (optional) */
|
|
24
|
+
criticalChunks?: string[];
|
|
25
|
+
/** Critical page-specific CSS (optional, extracted layout utilities for the page) */
|
|
26
|
+
criticalPageCSS?: string;
|
|
27
|
+
/** Critical universal CSS (optional, base styles extracted from theme at build time) */
|
|
28
|
+
criticalUniversalCSS?: string;
|
|
29
|
+
/** Deferred CSS path to load async (optional) */
|
|
30
|
+
deferredCSSPath?: string;
|
|
31
|
+
/** Server-rendered HTML content (SSR) */
|
|
32
|
+
ssrContent?: string;
|
|
33
|
+
/** RSC payload for hydration (JSON-serialized chunks) */
|
|
34
|
+
rscPayload?: string;
|
|
35
|
+
/** Client component modules to preload */
|
|
36
|
+
clientModules?: string[];
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Options for streaming HTML shell (streaming-optimal mode)
|
|
40
|
+
*/
|
|
41
|
+
export type StreamingHtmlShellOptions = HtmlShellOptions & {
|
|
42
|
+
/** Bootstrap script for inline RSC chunks */
|
|
43
|
+
inlineBootstrapScript?: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Create the START portion of streaming HTML shell
|
|
47
|
+
* Returns everything up to and including <div id="root">
|
|
48
|
+
*
|
|
49
|
+
* This is sent immediately when streaming begins, allowing the browser
|
|
50
|
+
* to start parsing and rendering before async content resolves.
|
|
51
|
+
*/
|
|
52
|
+
export declare function createStreamingHtmlShellStart(options: StreamingHtmlShellOptions): string;
|
|
53
|
+
/**
|
|
54
|
+
* Create the END portion of streaming HTML shell
|
|
55
|
+
* Just closes the HTML structure - manifest and client script are sent earlier
|
|
56
|
+
* (before RSC chunks) to enable progressive chunk processing.
|
|
57
|
+
*
|
|
58
|
+
* This is sent after all streamed content (HTML + inline RSC chunks) is complete.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createStreamingHtmlShellEnd(_options: StreamingHtmlShellOptions): string;
|
|
61
|
+
/**
|
|
62
|
+
* Create HTML shell with embedded chunk manifest and RSC client bootstrap
|
|
63
|
+
* @param options - Configuration options for HTML generation
|
|
64
|
+
* @returns Complete HTML document as string
|
|
65
|
+
*/
|
|
66
|
+
export declare function createHtmlShell(options: HtmlShellOptions): string;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* HTML Shell Template
|
|
4
|
+
* Provides the initial HTML structure for the React app with embedded chunk manifest
|
|
5
|
+
*/
|
|
6
|
+
import * as scriptJson from "../adapters/react/script-json.js";
|
|
7
|
+
import * as seo from "./seo/index.js";
|
|
8
|
+
// Default SEO configuration for laroux.now
|
|
9
|
+
const DEFAULT_SEO_CONFIG = {
|
|
10
|
+
title: "laroux.js 3.0",
|
|
11
|
+
description: "Zero-configuration React framework built on Server Components. Modern, simple, and built with cutting-edge technology.",
|
|
12
|
+
siteName: "laroux.js",
|
|
13
|
+
siteUrl: "https://laroux.now",
|
|
14
|
+
locale: "en_US",
|
|
15
|
+
themeColor: "#7c3aed",
|
|
16
|
+
favicon: "/favicon.svg",
|
|
17
|
+
twitterHandle: "@eser",
|
|
18
|
+
ogImage: "https://laroux.now/og-image.png",
|
|
19
|
+
ogType: "website",
|
|
20
|
+
keywords: [
|
|
21
|
+
"laroux",
|
|
22
|
+
"open source",
|
|
23
|
+
"community",
|
|
24
|
+
"development",
|
|
25
|
+
"react",
|
|
26
|
+
"server components",
|
|
27
|
+
"server actions",
|
|
28
|
+
"island architecture",
|
|
29
|
+
],
|
|
30
|
+
author: "Eser Ozvataf",
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Create the START portion of streaming HTML shell
|
|
34
|
+
* Returns everything up to and including <div id="root">
|
|
35
|
+
*
|
|
36
|
+
* This is sent immediately when streaming begins, allowing the browser
|
|
37
|
+
* to start parsing and rendering before async content resolves.
|
|
38
|
+
*/
|
|
39
|
+
export function createStreamingHtmlShellStart(options) {
|
|
40
|
+
const entrypoint = options.entrypoint ?? "/client.js";
|
|
41
|
+
// Use buildId for cache busting
|
|
42
|
+
const cacheBuster = options.chunkManifest?.buildId
|
|
43
|
+
? `?v=${options.chunkManifest.buildId}`
|
|
44
|
+
: `?v=${Date.now()}`;
|
|
45
|
+
const cssCacheBuster = options.chunkManifest?.hmrEnabled ? "" : cacheBuster;
|
|
46
|
+
// Generate font preload hints
|
|
47
|
+
const fontPreloadHints = options.fontPreloads?.join("\n ") ?? "";
|
|
48
|
+
// Generate inline font CSS
|
|
49
|
+
const inlineFontCSS = options.fontCSS
|
|
50
|
+
? `<style>${options.fontCSS}</style>`
|
|
51
|
+
: "";
|
|
52
|
+
// Generate critical chunk preload hints
|
|
53
|
+
const criticalChunkPreloads = options.criticalChunks
|
|
54
|
+
?.map((chunk) => `<link rel="modulepreload" href="${chunk}${cacheBuster}">`)
|
|
55
|
+
.join("\n ") ?? "";
|
|
56
|
+
// Generate client module preloads
|
|
57
|
+
const clientModulePreloads = options.clientModules
|
|
58
|
+
?.map((moduleId) => {
|
|
59
|
+
const chunkInfo = options.chunkManifest?.chunks?.[moduleId];
|
|
60
|
+
if (chunkInfo && "main" in chunkInfo) {
|
|
61
|
+
return `<link rel="modulepreload" href="/chunk-${chunkInfo.main}.js${cacheBuster}">`;
|
|
62
|
+
}
|
|
63
|
+
return "";
|
|
64
|
+
})
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.join("\n ") ?? "";
|
|
67
|
+
// Critical Universal CSS - dynamically extracted from compiled CSS at build time
|
|
68
|
+
// Fallback is minimal (scrollbar-gutter + box-sizing) - real styles come from compiled CSS
|
|
69
|
+
// NOTE: No hardcoded body/heading/a/button styles - these are extracted dynamically
|
|
70
|
+
// to respect the app's global.css and prevent conflicts with @layer utilities
|
|
71
|
+
const defaultUniversalCSS = `html{scrollbar-gutter:stable}*,*::before,*::after{box-sizing:border-box}`;
|
|
72
|
+
// An empty extraction result falls back to the default too
|
|
73
|
+
const criticalUniversalCSS = options.criticalUniversalCSS !== undefined &&
|
|
74
|
+
options.criticalUniversalCSS !== ""
|
|
75
|
+
? options.criticalUniversalCSS
|
|
76
|
+
: defaultUniversalCSS;
|
|
77
|
+
// Combine universal CSS (base/theme) with page CSS (layout utilities)
|
|
78
|
+
const combinedCSS = `${criticalUniversalCSS}${options.criticalPageCSS ?? ""}`;
|
|
79
|
+
const inlineCriticalCSS = `<style id="critical-css">${combinedCSS}</style>`;
|
|
80
|
+
// Deferred CSS loader - use media="print" pattern for proper async loading
|
|
81
|
+
// The preload hints the browser to fetch early, then the stylesheet link with media="print"
|
|
82
|
+
// doesn't block rendering. When loaded, we switch to media="all" to apply the styles.
|
|
83
|
+
const deferredCSSLoader = options.deferredCSSPath
|
|
84
|
+
? `<link rel="stylesheet" href="${options.deferredCSSPath}${cssCacheBuster}" media="print" onload="this.media='all'">
|
|
85
|
+
<noscript><link rel="stylesheet" href="${options.deferredCSSPath}${cssCacheBuster}"></noscript>`
|
|
86
|
+
: "";
|
|
87
|
+
// SEO config
|
|
88
|
+
const seoConfig = {
|
|
89
|
+
...DEFAULT_SEO_CONFIG,
|
|
90
|
+
...options.seoConfig,
|
|
91
|
+
};
|
|
92
|
+
const metaTags = seo.generateMetaTags(seoConfig);
|
|
93
|
+
const jsonLd = seo.generateJsonLd(seoConfig);
|
|
94
|
+
const lang = options.locale?.split("_")[0] ?? seoConfig.locale?.split("_")[0] ?? "en";
|
|
95
|
+
const cssPreload = options.deferredCSSPath
|
|
96
|
+
? `<link rel="preload" href="${options.deferredCSSPath}${cssCacheBuster}" as="style">`
|
|
97
|
+
: `<link rel="preload" href="/styles.css${cssCacheBuster}" as="style">`;
|
|
98
|
+
return `<!DOCTYPE html>
|
|
99
|
+
<html lang="${lang}">
|
|
100
|
+
<head>
|
|
101
|
+
<meta charset="UTF-8">
|
|
102
|
+
${cssPreload}
|
|
103
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
104
|
+
${metaTags}
|
|
105
|
+
<title>${seo.escapeHtml(seoConfig.title)}</title>
|
|
106
|
+
${fontPreloadHints}
|
|
107
|
+
${inlineFontCSS}
|
|
108
|
+
${inlineCriticalCSS}
|
|
109
|
+
${options.deferredCSSPath
|
|
110
|
+
? deferredCSSLoader
|
|
111
|
+
: `<link rel="stylesheet" href="/styles.css${cssCacheBuster}" media="print" onload="this.media='all'">
|
|
112
|
+
<noscript><link rel="stylesheet" href="/styles.css${cssCacheBuster}"></noscript>`}
|
|
113
|
+
<link rel="modulepreload" href="${entrypoint}${cacheBuster}">
|
|
114
|
+
${criticalChunkPreloads}
|
|
115
|
+
${clientModulePreloads}
|
|
116
|
+
${jsonLd}
|
|
117
|
+
</head>
|
|
118
|
+
<body>
|
|
119
|
+
<div id="root">`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create the END portion of streaming HTML shell
|
|
123
|
+
* Just closes the HTML structure - manifest and client script are sent earlier
|
|
124
|
+
* (before RSC chunks) to enable progressive chunk processing.
|
|
125
|
+
*
|
|
126
|
+
* This is sent after all streamed content (HTML + inline RSC chunks) is complete.
|
|
127
|
+
*/
|
|
128
|
+
export function createStreamingHtmlShellEnd(_options) {
|
|
129
|
+
// Manifest and client script are now sent RIGHT AFTER the bootstrap script
|
|
130
|
+
// (before RSC chunks stream) in server.ts createStreamingOptimalResponse.
|
|
131
|
+
// This allows the client to load in parallel and process chunks progressively.
|
|
132
|
+
return `</div>
|
|
133
|
+
</body>
|
|
134
|
+
</html>`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Create HTML shell with embedded chunk manifest and RSC client bootstrap
|
|
138
|
+
* @param options - Configuration options for HTML generation
|
|
139
|
+
* @returns Complete HTML document as string
|
|
140
|
+
*/
|
|
141
|
+
export function createHtmlShell(options) {
|
|
142
|
+
const entrypoint = options.entrypoint ?? "/client.js";
|
|
143
|
+
const manifestScript = options.chunkManifest
|
|
144
|
+
? `<script>globalThis.__CHUNK_MANIFEST__ = ${JSON.stringify(options.chunkManifest)};</script>`
|
|
145
|
+
: "";
|
|
146
|
+
// Use buildId for cache busting (more stable than timestamp)
|
|
147
|
+
const cacheBuster = options.chunkManifest?.buildId
|
|
148
|
+
? `?v=${options.chunkManifest.buildId}`
|
|
149
|
+
: `?v=${Date.now()}`;
|
|
150
|
+
// Skip CSS cache-busting in HMR mode (HMR client handles CSS reloading)
|
|
151
|
+
const cssCacheBuster = options.chunkManifest?.hmrEnabled ? "" : cacheBuster;
|
|
152
|
+
// Generate font preload hints
|
|
153
|
+
const fontPreloadHints = options.fontPreloads?.join("\n ") ?? "";
|
|
154
|
+
// Generate inline font CSS
|
|
155
|
+
const inlineFontCSS = options.fontCSS
|
|
156
|
+
? `<style>${options.fontCSS}</style>`
|
|
157
|
+
: "";
|
|
158
|
+
// Generate critical chunk preload hints for faster loading
|
|
159
|
+
const criticalChunkPreloads = options.criticalChunks
|
|
160
|
+
?.map((chunk) => `<link rel="modulepreload" href="${chunk}${cacheBuster}">`)
|
|
161
|
+
.join("\n ") ?? "";
|
|
162
|
+
// Generate client module preloads for SSR hydration
|
|
163
|
+
const clientModulePreloads = options.clientModules
|
|
164
|
+
?.map((moduleId) => {
|
|
165
|
+
// Look up the module in chunk manifest to get actual chunk file
|
|
166
|
+
const chunkInfo = options.chunkManifest?.chunks?.[moduleId];
|
|
167
|
+
if (chunkInfo && "main" in chunkInfo) {
|
|
168
|
+
return `<link rel="modulepreload" href="/chunk-${chunkInfo.main}.js${cacheBuster}">`;
|
|
169
|
+
}
|
|
170
|
+
return "";
|
|
171
|
+
})
|
|
172
|
+
.filter(Boolean)
|
|
173
|
+
.join("\n ") ?? "";
|
|
174
|
+
// Critical Universal CSS - dynamically extracted from compiled CSS at build time
|
|
175
|
+
// Fallback is minimal (scrollbar-gutter + box-sizing) - real styles come from compiled CSS
|
|
176
|
+
// NOTE: No hardcoded body/heading/a/button styles - these are extracted dynamically
|
|
177
|
+
// to respect the app's global.css and prevent conflicts with @layer utilities
|
|
178
|
+
const defaultUniversalCSS = `html{scrollbar-gutter:stable}*,*::before,*::after{box-sizing:border-box}`;
|
|
179
|
+
// An empty extraction result falls back to the default too
|
|
180
|
+
const criticalUniversalCSS = options.criticalUniversalCSS !== undefined &&
|
|
181
|
+
options.criticalUniversalCSS !== ""
|
|
182
|
+
? options.criticalUniversalCSS
|
|
183
|
+
: defaultUniversalCSS;
|
|
184
|
+
// Combine universal CSS (base/theme) with page CSS (layout utilities)
|
|
185
|
+
const combinedCSS = `${criticalUniversalCSS}${options.criticalPageCSS ?? ""}`;
|
|
186
|
+
const inlineCriticalCSS = `<style id="critical-css">${combinedCSS}</style>`;
|
|
187
|
+
// Deferred CSS loader - use media="print" pattern for proper async loading
|
|
188
|
+
const deferredCSSLoader = options.deferredCSSPath
|
|
189
|
+
? `<link rel="stylesheet" href="${options.deferredCSSPath}${cssCacheBuster}" media="print" onload="this.media='all'">
|
|
190
|
+
<noscript><link rel="stylesheet" href="${options.deferredCSSPath}${cssCacheBuster}"></noscript>`
|
|
191
|
+
: "";
|
|
192
|
+
// Merge SEO config with defaults
|
|
193
|
+
const seoConfig = {
|
|
194
|
+
...DEFAULT_SEO_CONFIG,
|
|
195
|
+
...options.seoConfig,
|
|
196
|
+
};
|
|
197
|
+
// Generate SEO meta tags and JSON-LD
|
|
198
|
+
const metaTags = seo.generateMetaTags(seoConfig);
|
|
199
|
+
const jsonLd = seo.generateJsonLd(seoConfig);
|
|
200
|
+
// Determine language from locale
|
|
201
|
+
const lang = options.locale?.split("_")[0] ?? seoConfig.locale?.split("_")[0] ?? "en";
|
|
202
|
+
// Generate CSS preload (early discovery for faster loading)
|
|
203
|
+
const cssPreload = options.deferredCSSPath
|
|
204
|
+
? `<link rel="preload" href="${options.deferredCSSPath}${cssCacheBuster}" as="style">`
|
|
205
|
+
: `<link rel="preload" href="/styles.css${cssCacheBuster}" as="style">`;
|
|
206
|
+
return `<!DOCTYPE html>
|
|
207
|
+
<html lang="${lang}">
|
|
208
|
+
<head>
|
|
209
|
+
<meta charset="UTF-8">
|
|
210
|
+
<!-- Early CSS preload for faster discovery -->
|
|
211
|
+
${cssPreload}
|
|
212
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
213
|
+
${metaTags}
|
|
214
|
+
<title>${seo.escapeHtml(seoConfig.title)}</title>
|
|
215
|
+
${fontPreloadHints}
|
|
216
|
+
${inlineFontCSS}
|
|
217
|
+
<!-- Critical CSS (inlined for fast first paint) -->
|
|
218
|
+
${inlineCriticalCSS}
|
|
219
|
+
${options.deferredCSSPath
|
|
220
|
+
? deferredCSSLoader
|
|
221
|
+
: `<!-- Async full CSS loading -->
|
|
222
|
+
<link rel="stylesheet" href="/styles.css${cssCacheBuster}" media="print" onload="this.media='all'">
|
|
223
|
+
<noscript><link rel="stylesheet" href="/styles.css${cssCacheBuster}"></noscript>`}
|
|
224
|
+
<!-- Modulepreload for faster script loading -->
|
|
225
|
+
<link rel="modulepreload" href="${entrypoint}${cacheBuster}">
|
|
226
|
+
${criticalChunkPreloads}
|
|
227
|
+
${clientModulePreloads}
|
|
228
|
+
${jsonLd}
|
|
229
|
+
</head>
|
|
230
|
+
<body>
|
|
231
|
+
<div id="root">${options.ssrContent
|
|
232
|
+
? options.ssrContent
|
|
233
|
+
: '<div class="initial-loading">Loading...</div>'}</div>
|
|
234
|
+
|
|
235
|
+
<!-- Chunk manifest for lazy loading -->
|
|
236
|
+
${manifestScript}
|
|
237
|
+
${options.rscPayload
|
|
238
|
+
? `<!-- RSC payload for hydration -->
|
|
239
|
+
<script id="__RSC_PAYLOAD__" type="application/json">${scriptJson.escapeJsonForScript(options.rscPayload)}</script>`
|
|
240
|
+
: ""}
|
|
241
|
+
|
|
242
|
+
<!-- Client entry point -->
|
|
243
|
+
<script type="module" src="${entrypoint}${cacheBuster}"></script>
|
|
244
|
+
</body>
|
|
245
|
+
</html>`;
|
|
246
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Content-Type for a file extension, with or without its leading dot, in
|
|
3
|
+
* any case; null when the extension is not in the table.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import * as mediaTypes from "./media-types.ts";
|
|
8
|
+
*
|
|
9
|
+
* mediaTypes.contentType(".js"); // "text/javascript; charset=UTF-8"
|
|
10
|
+
* mediaTypes.contentType(".unknown"); // null
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare const contentType: (extension: string) => string | null;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* The Content-Type of the static files a laroux server sends: build output
|
|
4
|
+
* and the files under `public/`. The values match what the server sent with
|
|
5
|
+
* `@std/media-types`, a charset on the text types included. An extension the
|
|
6
|
+
* table does not hold gets no type, and the server falls back to
|
|
7
|
+
* `application/octet-stream`.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
const UTF8 = "; charset=UTF-8";
|
|
12
|
+
/** Content-Type by lower-case file extension, without the dot. */
|
|
13
|
+
const CONTENT_TYPES = {
|
|
14
|
+
// Documents, scripts and styles
|
|
15
|
+
html: `text/html${UTF8}`,
|
|
16
|
+
htm: `text/html${UTF8}`,
|
|
17
|
+
css: `text/css${UTF8}`,
|
|
18
|
+
js: `text/javascript${UTF8}`,
|
|
19
|
+
mjs: `text/javascript${UTF8}`,
|
|
20
|
+
json: `application/json${UTF8}`,
|
|
21
|
+
map: `application/json${UTF8}`,
|
|
22
|
+
webmanifest: `application/manifest+json${UTF8}`,
|
|
23
|
+
jsonld: "application/ld+json",
|
|
24
|
+
txt: `text/plain${UTF8}`,
|
|
25
|
+
md: `text/markdown${UTF8}`,
|
|
26
|
+
csv: `text/csv${UTF8}`,
|
|
27
|
+
xml: "application/xml",
|
|
28
|
+
rss: "application/rss+xml",
|
|
29
|
+
atom: "application/atom+xml",
|
|
30
|
+
vtt: `text/vtt${UTF8}`,
|
|
31
|
+
wasm: "application/wasm",
|
|
32
|
+
pdf: "application/pdf",
|
|
33
|
+
zip: "application/zip",
|
|
34
|
+
gz: "application/gzip",
|
|
35
|
+
// Images
|
|
36
|
+
svg: "image/svg+xml",
|
|
37
|
+
png: "image/png",
|
|
38
|
+
apng: "image/apng",
|
|
39
|
+
jpg: "image/jpeg",
|
|
40
|
+
jpeg: "image/jpeg",
|
|
41
|
+
gif: "image/gif",
|
|
42
|
+
webp: "image/webp",
|
|
43
|
+
avif: "image/avif",
|
|
44
|
+
ico: "image/vnd.microsoft.icon",
|
|
45
|
+
bmp: "image/bmp",
|
|
46
|
+
// Fonts
|
|
47
|
+
woff: "font/woff",
|
|
48
|
+
woff2: "font/woff2",
|
|
49
|
+
ttf: "font/ttf",
|
|
50
|
+
otf: "font/otf",
|
|
51
|
+
eot: "application/vnd.ms-fontobject",
|
|
52
|
+
// Audio and video
|
|
53
|
+
mp3: "audio/mpeg",
|
|
54
|
+
wav: "audio/wave",
|
|
55
|
+
ogg: "audio/ogg",
|
|
56
|
+
oga: "audio/ogg",
|
|
57
|
+
opus: "audio/ogg",
|
|
58
|
+
m4a: "audio/mp4",
|
|
59
|
+
aac: "audio/aac",
|
|
60
|
+
weba: "audio/webm",
|
|
61
|
+
mp4: "video/mp4",
|
|
62
|
+
webm: "video/webm",
|
|
63
|
+
ogv: "video/ogg",
|
|
64
|
+
mov: "video/quicktime",
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* The Content-Type for a file extension, with or without its leading dot, in
|
|
68
|
+
* any case; null when the extension is not in the table.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import * as mediaTypes from "./media-types.ts";
|
|
73
|
+
*
|
|
74
|
+
* mediaTypes.contentType(".js"); // "text/javascript; charset=UTF-8"
|
|
75
|
+
* mediaTypes.contentType(".unknown"); // null
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export const contentType = (extension) => {
|
|
79
|
+
const key = extension.replace(/^\./, "").toLowerCase();
|
|
80
|
+
return Object.hasOwn(CONTENT_TYPES, key)
|
|
81
|
+
? (CONTENT_TYPES[key] ?? null)
|
|
82
|
+
: null;
|
|
83
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a request-derived relative path against a base directory and
|
|
3
|
+
* returns it only when the result stays strictly inside that directory.
|
|
4
|
+
*
|
|
5
|
+
* Returns null for absolute inputs (a `//`-prefixed request target turns the
|
|
6
|
+
* remainder into an absolute path, which `resolve` would adopt wholesale), for
|
|
7
|
+
* `..` escapes, and for the base directory itself. The check compares path
|
|
8
|
+
* segments via `relative`, so a sibling such as `dist-old` never passes as a
|
|
9
|
+
* child of `dist`.
|
|
10
|
+
*/
|
|
11
|
+
export declare const resolveWithin: (baseDir: string, relativePath: string) => string | null;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a request-derived relative path against a base directory and
|
|
5
|
+
* returns it only when the result stays strictly inside that directory.
|
|
6
|
+
*
|
|
7
|
+
* Returns null for absolute inputs (a `//`-prefixed request target turns the
|
|
8
|
+
* remainder into an absolute path, which `resolve` would adopt wholesale), for
|
|
9
|
+
* `..` escapes, and for the base directory itself. The check compares path
|
|
10
|
+
* segments via `relative`, so a sibling such as `dist-old` never passes as a
|
|
11
|
+
* child of `dist`.
|
|
12
|
+
*/
|
|
13
|
+
export const resolveWithin = (baseDir, relativePath) => {
|
|
14
|
+
if (relativePath.includes("\0") || runtime.path.isAbsolute(relativePath)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const base = runtime.path.resolve(baseDir);
|
|
18
|
+
const resolved = runtime.path.resolve(base, relativePath);
|
|
19
|
+
const rel = runtime.path.relative(base, resolved);
|
|
20
|
+
if (rel === "" ||
|
|
21
|
+
rel === ".." ||
|
|
22
|
+
rel.startsWith(`..${runtime.path.sep}`) ||
|
|
23
|
+
runtime.path.isAbsolute(rel)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return resolved;
|
|
27
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SEO metadata generator for Laroux.js
|
|
3
|
+
*/
|
|
4
|
+
import type { PageMetadata, SeoConfig } from "./types.ts";
|
|
5
|
+
/**
|
|
6
|
+
* Generate HTML meta tags from SEO config
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateMetaTags(config: SeoConfig, pageMetadata?: PageMetadata): string;
|
|
9
|
+
/**
|
|
10
|
+
* Generate JSON-LD structured data
|
|
11
|
+
*/
|
|
12
|
+
export declare function generateJsonLd(config: SeoConfig, pageMetadata?: PageMetadata): string;
|
|
13
|
+
/**
|
|
14
|
+
* Escape HTML special characters
|
|
15
|
+
*/
|
|
16
|
+
export declare function escapeHtml(text: string): string;
|