meno-core 1.0.11 → 1.0.13
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/build-static.ts +33 -3
- package/lib/client/core/ComponentBuilder.ts +1 -1
- package/lib/server/ssr/attributeBuilder.ts +1 -1
- package/lib/server/ssr/htmlGenerator.ts +8 -3
- package/lib/server/ssr/imageMetadata.ts +2 -2
- package/lib/server/ssr/ssrRenderer.ts +10 -1
- package/lib/server/ssrRenderer.test.ts +60 -0
- package/package.json +1 -1
package/build-static.ts
CHANGED
|
@@ -32,6 +32,34 @@ function hashContent(content: string): string {
|
|
|
32
32
|
return createHash('sha256').update(content).digest('hex').slice(0, 8);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Minify JavaScript code using Bun's built-in bundler
|
|
37
|
+
*/
|
|
38
|
+
async function minifyJS(code: string): Promise<string> {
|
|
39
|
+
const tempFile = join('/tmp', `meno-minify-${Date.now()}.js`);
|
|
40
|
+
try {
|
|
41
|
+
await writeFile(tempFile, code, 'utf-8');
|
|
42
|
+
|
|
43
|
+
const result = await Bun.build({
|
|
44
|
+
entrypoints: [tempFile],
|
|
45
|
+
minify: true,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (result.success && result.outputs.length > 0) {
|
|
49
|
+
return await result.outputs[0].text();
|
|
50
|
+
}
|
|
51
|
+
// Fallback to original if minification fails
|
|
52
|
+
return code;
|
|
53
|
+
} finally {
|
|
54
|
+
// Clean up temp file
|
|
55
|
+
try {
|
|
56
|
+
rmSync(tempFile, { force: true });
|
|
57
|
+
} catch {
|
|
58
|
+
// Ignore cleanup errors
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
35
63
|
/**
|
|
36
64
|
* Track JavaScript files to avoid duplicates
|
|
37
65
|
* Maps content hash -> script path
|
|
@@ -43,7 +71,9 @@ const jsFileCache = new Map<string, string>();
|
|
|
43
71
|
* Returns the path to reference in HTML
|
|
44
72
|
*/
|
|
45
73
|
async function getScriptPath(jsContent: string, distDir: string): Promise<string> {
|
|
46
|
-
|
|
74
|
+
// Minify JavaScript for production
|
|
75
|
+
const minified = await minifyJS(jsContent);
|
|
76
|
+
const hash = hashContent(minified);
|
|
47
77
|
|
|
48
78
|
// Check if we already wrote this content
|
|
49
79
|
if (jsFileCache.has(hash)) {
|
|
@@ -56,10 +86,10 @@ async function getScriptPath(jsContent: string, distDir: string): Promise<string
|
|
|
56
86
|
mkdirSync(scriptsDir, { recursive: true });
|
|
57
87
|
}
|
|
58
88
|
|
|
59
|
-
// Write script file
|
|
89
|
+
// Write minified script file
|
|
60
90
|
const scriptPath = `/_scripts/${hash}.js`;
|
|
61
91
|
const fullPath = join(distDir, '_scripts', `${hash}.js`);
|
|
62
|
-
await writeFile(fullPath,
|
|
92
|
+
await writeFile(fullPath, minified, 'utf-8');
|
|
63
93
|
|
|
64
94
|
// Cache for reuse
|
|
65
95
|
jsFileCache.set(hash, scriptPath);
|
|
@@ -387,7 +387,7 @@ export class ComponentBuilder {
|
|
|
387
387
|
* Filter internal props from node props
|
|
388
388
|
*/
|
|
389
389
|
private filterInternalProps(nodeProps: Record<string, unknown>, tag: string | undefined): Record<string, unknown> {
|
|
390
|
-
const imageOnlyProps = ['src', 'alt', 'loading', 'width', 'height', 'sizes', 'srcset'];
|
|
390
|
+
const imageOnlyProps = ['src', 'alt', 'loading', 'width', 'height', 'sizes', 'srcset', 'fetchpriority', 'priority'];
|
|
391
391
|
const internalProps = ['type', 'tag', 'component', 'props', 'children', 'html', 'style', ...imageOnlyProps];
|
|
392
392
|
const props: Record<string, unknown> = {};
|
|
393
393
|
|
|
@@ -21,7 +21,7 @@ export function escapeHtml(unsafe: string): string {
|
|
|
21
21
|
export function buildAttributes(props: Record<string, unknown>, exclude: string[] = []): string {
|
|
22
22
|
const attrs: string[] = [];
|
|
23
23
|
// Internal props that should never be rendered as HTML attributes
|
|
24
|
-
const internalProps = ['tag', 'component', 'props', 'children', 'src', 'alt', 'loading', 'width', 'height', 'sizes', 'srcset'];
|
|
24
|
+
const internalProps = ['tag', 'component', 'props', 'children', 'src', 'alt', 'loading', 'width', 'height', 'sizes', 'srcset', 'fetchpriority', 'priority'];
|
|
25
25
|
const defaultExclude = [...internalProps, ...exclude];
|
|
26
26
|
|
|
27
27
|
// Regex to detect unresolved template strings like {{link.target}}
|
|
@@ -164,8 +164,8 @@ export async function generateSSRHTML(
|
|
|
164
164
|
|
|
165
165
|
if (allJavaScript) {
|
|
166
166
|
if (extScriptPath) {
|
|
167
|
-
// CSP-compliant: reference external script file
|
|
168
|
-
componentScript = `\n <script src="${extScriptPath}"></script>`;
|
|
167
|
+
// CSP-compliant: reference external script file (defer to avoid render-blocking)
|
|
168
|
+
componentScript = `\n <script src="${extScriptPath}" defer></script>`;
|
|
169
169
|
externalJavaScript = allJavaScript;
|
|
170
170
|
} else if (returnSeparateJS) {
|
|
171
171
|
// Return JS separately (for build-static to write to file)
|
|
@@ -235,12 +235,17 @@ export async function generateSSRHTML(
|
|
|
235
235
|
: '';
|
|
236
236
|
const iconTags = [faviconTag, appleTouchIconTag].filter(Boolean).join('\n ');
|
|
237
237
|
|
|
238
|
+
// Script preload tag - eliminates critical request chain by discovering script early
|
|
239
|
+
const scriptPreloadTag = extScriptPath
|
|
240
|
+
? `<link rel="preload" href="${extScriptPath}" as="script">`
|
|
241
|
+
: '';
|
|
242
|
+
|
|
238
243
|
const htmlDocument = `<!DOCTYPE html>
|
|
239
244
|
<html lang="${rendered.locale}" theme="${themeConfig.default}">
|
|
240
245
|
<head>
|
|
241
246
|
<meta charset="UTF-8">
|
|
242
247
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
243
|
-
${iconTags ? iconTags + '\n ' : ''}${fontPreloadTags ? fontPreloadTags + '\n ' : ''}${rendered.meta}
|
|
248
|
+
${iconTags ? iconTags + '\n ' : ''}${scriptPreloadTag ? scriptPreloadTag + '\n ' : ''}${fontPreloadTags ? fontPreloadTags + '\n ' : ''}${rendered.meta}
|
|
244
249
|
${configInlineScript}${cmsInlineScript}<style>
|
|
245
250
|
${fontCSS ? fontCSS + '\n ' : ''}${themeColorVariablesCSS ? themeColorVariablesCSS + '\n ' : ''}* {
|
|
246
251
|
margin: 0;
|
|
@@ -28,9 +28,9 @@ export interface ImageMetadata {
|
|
|
28
28
|
export type ImageMetadataMap = Map<string, ImageMetadata>;
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Responsive image widths for variant detection
|
|
31
|
+
* Responsive image widths for variant detection (optimized for mobile 2x/3x DPR and desktop)
|
|
32
32
|
*/
|
|
33
|
-
export const RESPONSIVE_WIDTHS = [500,
|
|
33
|
+
export const RESPONSIVE_WIDTHS = [500, 800, 1080, 1600, 2400] as const;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Default sizes attribute for responsive images
|
|
@@ -891,6 +891,8 @@ function renderImageElement(
|
|
|
891
891
|
const alt = imgProps.alt as string | undefined;
|
|
892
892
|
const loading = imgProps.loading as string | undefined;
|
|
893
893
|
const sizes = imgProps.sizes as string | undefined;
|
|
894
|
+
const fetchpriority = imgProps.fetchpriority as string | undefined;
|
|
895
|
+
const priority = imgProps.priority as boolean | undefined;
|
|
894
896
|
let width = imgProps.width as string | number | undefined;
|
|
895
897
|
let height = imgProps.height as string | number | undefined;
|
|
896
898
|
|
|
@@ -910,7 +912,14 @@ function renderImageElement(
|
|
|
910
912
|
let imgAttrs = '';
|
|
911
913
|
if (src) imgAttrs += ` src="${escapeHtml(String(src))}"`;
|
|
912
914
|
if (alt !== undefined) imgAttrs += ` alt="${escapeHtml(String(alt))}"`;
|
|
913
|
-
if
|
|
915
|
+
// Handle priority: if true, set fetchpriority="high" and loading="eager" for LCP optimization
|
|
916
|
+
if (priority) {
|
|
917
|
+
imgAttrs += ' fetchpriority="high"';
|
|
918
|
+
imgAttrs += ' loading="eager"';
|
|
919
|
+
} else {
|
|
920
|
+
if (fetchpriority) imgAttrs += ` fetchpriority="${escapeHtml(String(fetchpriority))}"`;
|
|
921
|
+
if (loading) imgAttrs += ` loading="${escapeHtml(String(loading))}"`;
|
|
922
|
+
}
|
|
914
923
|
if (width !== undefined) imgAttrs += ` width="${escapeHtml(String(width))}"`;
|
|
915
924
|
if (height !== undefined) imgAttrs += ` height="${escapeHtml(String(height))}"`;
|
|
916
925
|
|
|
@@ -883,6 +883,66 @@ describe("SSR Renderer - Security (XSS Prevention)", () => {
|
|
|
883
883
|
});
|
|
884
884
|
});
|
|
885
885
|
|
|
886
|
+
describe("Image LCP optimization", () => {
|
|
887
|
+
test("should render fetchpriority attribute", async () => {
|
|
888
|
+
const pageData: JSONPage = {
|
|
889
|
+
root: {
|
|
890
|
+
type: "node",
|
|
891
|
+
tag: "img",
|
|
892
|
+
attributes: {
|
|
893
|
+
src: "/test.jpg",
|
|
894
|
+
alt: "test",
|
|
895
|
+
fetchpriority: "high"
|
|
896
|
+
}
|
|
897
|
+
} as any
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
const result = await renderPageSSR(pageData);
|
|
901
|
+
|
|
902
|
+
expect(result.html).toContain('fetchpriority="high"');
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
test("should render priority prop as fetchpriority=high and loading=eager", async () => {
|
|
906
|
+
const pageData: JSONPage = {
|
|
907
|
+
root: {
|
|
908
|
+
type: "node",
|
|
909
|
+
tag: "img",
|
|
910
|
+
attributes: {
|
|
911
|
+
src: "/test.jpg",
|
|
912
|
+
alt: "test",
|
|
913
|
+
priority: true
|
|
914
|
+
}
|
|
915
|
+
} as any
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
const result = await renderPageSSR(pageData);
|
|
919
|
+
|
|
920
|
+
expect(result.html).toContain('fetchpriority="high"');
|
|
921
|
+
expect(result.html).toContain('loading="eager"');
|
|
922
|
+
});
|
|
923
|
+
|
|
924
|
+
test("priority should override explicit loading=lazy", async () => {
|
|
925
|
+
const pageData: JSONPage = {
|
|
926
|
+
root: {
|
|
927
|
+
type: "node",
|
|
928
|
+
tag: "img",
|
|
929
|
+
attributes: {
|
|
930
|
+
src: "/test.jpg",
|
|
931
|
+
alt: "test",
|
|
932
|
+
priority: true,
|
|
933
|
+
loading: "lazy"
|
|
934
|
+
}
|
|
935
|
+
} as any
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
const result = await renderPageSSR(pageData);
|
|
939
|
+
|
|
940
|
+
// priority=true should set loading="eager", ignoring the lazy value
|
|
941
|
+
expect(result.html).toContain('loading="eager"');
|
|
942
|
+
expect(result.html).not.toContain('loading="lazy"');
|
|
943
|
+
});
|
|
944
|
+
});
|
|
945
|
+
|
|
886
946
|
describe("Embed content sanitization", () => {
|
|
887
947
|
test("should sanitize embed HTML with DOMPurify", async () => {
|
|
888
948
|
const pageData: JSONPage = {
|