Skip to content

Commit c551de1

Browse files
committed
Refactors defer_pagefind to work with site.afterRender()
The previous defer_pagefind.ts exported a default function (export default function deferPagefind() { ... }) that, when called, returned another function that processes an array of pages ((pages: Page[]) => { ... }). This structure is correct for Lume's site.process() method. However, for site.afterRender(), Lume expects a function that directly takes a single Page object as its argument. It iterates over the pages itself and passes each one to the provided function. By changing defer_pagefind.ts to export export function deferPagefindForPage(page: Page), we are: * Changing it to a named export: allows import as specifically as deferPagefindForPage in your _config.ts. * Modifying its signature: directly accepts a single page: Page object, aligning with what site.afterRender() expects. This change is fundamental to ensure the script runs at the correct stage of the Lume build process where the page.content is reliably a string (the rendered HTML), allowing the regex replacements to work as intended.
1 parent 424850e commit c551de1

File tree

2 files changed

+47
-50
lines changed

2 files changed

+47
-50
lines changed

mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// One place for all exports
22
export { default as cssBanner } from "./plugins/css_banner.ts";
33
export { default as shuffle } from "./plugins/shuffle.ts";
4-
export { default as deferPagefind } from "./processors/defer_pagefind.ts";
4+
// Lume site.afterRender() expects a function that directly takes a single Page object as its argument, so this is a named export:
5+
export { deferPagefindForPage } from "./processors/defer_pagefind.ts";
56
export { default as externalLinksIcon } from "./processors/external_links_icon.ts";

processors/defer_pagefind.ts

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,54 @@
1-
// defer_pagefind.ts searches html pages for Pagefind CSS and JS links,
2-
// and modifies them to load with `defer` and `media="print"` attributes, so they don't block rendering.
1+
// processors/defer_pagefind.ts
32

43
import type { Page } from "../types/lume.ts";
54

6-
export default function deferPagefind() {
7-
return (pages: Page[]) => {
8-
console.log("Starting deferPagefind script...");
9-
for (const page of pages) {
10-
const content = page.content;
11-
if (typeof content !== "string") {
12-
console.log(`Skipping non-string content for: ${page.src?.path || "unknown page"}`);
13-
continue;
14-
}
15-
16-
console.log(`Processing page: ${page.src?.path || "unknown page"}`);
17-
console.log(`Original content snippet (first 200 chars): ${content.substring(0, 200)}`);
18-
19-
let updated = content;
20-
let modified = false;
21-
22-
// Check for Pagefind CSS link BEFORE replacement
23-
const cssMatch = content.match(/<link\s+[^>]*href=["']\/pagefind\/pagefind-ui\.css["'][^>]*>/gi);
24-
if (cssMatch) {
25-
console.log(`Found Pagefind CSS link on ${page.src?.path}: ${cssMatch[0]}`);
26-
} else {
27-
console.log(`❌ Pagefind CSS link NOT found on ${page.src?.path}`);
28-
}
29-
5+
/**
6+
* Modifies the content of a single Lume Page to defer Pagefind CSS and JS loading.
7+
* Intended to be used with site.afterRender().
8+
*/
9+
export function deferPagefindForPage(page: Page) {
10+
const content = page.content;
11+
12+
// Crucially, this check should now usually pass for HTML pages in afterRender
13+
if (typeof content !== "string") {
14+
// This console.log should ideally be seen less or not at all for .html pages
15+
console.log(`Skipping non-string content for: ${page.src?.path || "unknown page"} (at afterRender stage)`);
16+
return; // Don't proceed if content isn't a string
17+
}
18+
19+
let updated = content;
20+
let modified = false; // Flag to track if any changes were made
21+
22+
// Regex to ensure we capture the attributes correctly for replacement
23+
const cssRegex = /(<link\s+[^>]*href=["']\/pagefind\/pagefind-ui\.css["'])(\s*[^>]*>)/gi;
24+
if (cssRegex.test(updated)) {
25+
// Replaces the existing tag by inserting the new attributes before the closing bracket
3026
updated = updated.replace(
31-
/<link\s+[^>]*href=["']\/pagefind\/pagefind-ui\.css["'][^>]*>/gi,
32-
`<link rel="stylesheet" href="/pagefind/pagefind-ui.css" media="print" onload="this.media='all'">`
27+
cssRegex,
28+
`$1 media="print" onload="this.media='all'"$2`
3329
);
30+
modified = true;
31+
}
3432

35-
// Check for Pagefind JS script BEFORE replacement
36-
const jsMatch = content.match(/<script\s+[^>]*src=["']\/pagefind\/pagefind-ui\.js["'][^>]*><\/script>/gi);
37-
if (jsMatch) {
38-
console.log(`Found Pagefind JS script on ${page.src?.path}: ${jsMatch[0]}`);
39-
} else {
40-
console.log(`❌ Pagefind JS script NOT found on ${page.src?.path}`);
41-
}
4233

34+
const jsRegex = /(<script\s+[^>]*src=["']\/pagefind\/pagefind-ui\.js["'])(\s*[^>]*><\/script>)/gi;
35+
if (jsRegex.test(updated)) {
36+
// Replaces the existing tag by inserting the 'defer' attribute
4337
updated = updated.replace(
44-
/<script\s+[^>]*src=["']\/pagefind\/pagefind-ui\.js["'][^>]*><\/script>/gi,
45-
`<script type="text/javascript" src="/pagefind/pagefind-ui.js" defer></script>`
38+
jsRegex,
39+
`$1 defer$2`
4640
);
47-
48-
if (updated !== content) {
49-
page.content = updated;
50-
console.log(`✅ Modified: ${page.src?.path || "unknown page"}`);
51-
console.log(`Updated content snippet (first 200 chars): ${page.content.substring(0, 200)}`);
52-
} else {
53-
console.log(`No changes made to: ${page.src?.path || "unknown page"}`);
54-
}
55-
}
56-
console.log("Finished deferPagefind script.");
57-
};
58-
}
41+
modified = true;
42+
}
43+
44+
// Only update page.content and log if actual modifications occurred
45+
if (modified) {
46+
page.content = updated;
47+
console.log(`✅ Modified Pagefind attributes for: ${page.src?.path || "unknown page"}`);
48+
// You can uncomment this for very detailed debugging, but it can be noisy
49+
// console.log(`Updated content snippet (first 500 chars): ${page.content.substring(0, 500)}`);
50+
} else {
51+
// This log can be useful if you expect modifications but don't see them
52+
// console.log(`No Pagefind modifications needed for: ${page.src?.path || "unknown page"}`);
53+
}
54+
}

0 commit comments

Comments
 (0)