Skip to content

Commit f539c8d

Browse files
committed
chore: speculation rules
1 parent 10e9e3e commit f539c8d

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/generators/web/constants.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@ export const JSX_IMPORTS = {
8080
source: '@node-core/ui-components/Providers/NotificationProvider',
8181
},
8282
};
83+
84+
/**
85+
* Specification rules for resource hints like prerendering and prefetching.
86+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API
87+
*/
88+
export const SPECULATION_RULES = {
89+
prefetch: [
90+
{ where: { selector_matches: '[rel~=prefetch]' }, eagerness: 'eager' },
91+
],
92+
prerender: [{ where: { href_matches: '/*.html' }, eagerness: 'moderate' }],
93+
};

src/generators/web/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<!-- Apply theme before paint to avoid Flash of Unstyled Content -->
1717
<script>document.documentElement.setAttribute("data-theme",localStorage.getItem("theme")||(matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"));</script>
1818
<script type="importmap">{{importMap}}</script>
19+
<script type="speculationrules">{{speculationRules}}</script>
1920
</head>
2021

2122
<body>

src/generators/web/ui/components/SideBar/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Select from '@node-core/ui-components/Common/Select/index.js';
1+
import Select from '@node-core/ui-components/Common/Select';
22
import SideBar from '@node-core/ui-components/Containers/Sidebar';
33

44
import styles from './index.module.css';
@@ -31,6 +31,7 @@ export default ({ versions, pathname, currentVersion, docPages }) => (
3131
},
3232
]}
3333
onSelect={redirect}
34+
as={props => <a {...props} rel="prefetch" />}
3435
>
3536
<div>
3637
<Select

src/generators/web/utils/processing.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { randomUUID } from 'node:crypto';
33
import HTMLMinifier from '@minify-html/node';
44
import { jsx, toJs } from 'estree-util-to-js';
55

6+
import { SPECULATION_RULES } from '../constants.mjs';
67
import bundleCode from './bundle.mjs';
78
import { createChunkedRequire } from './chunks.mjs';
89

@@ -83,6 +84,8 @@ export async function processJSXEntries(
8384

8485
const titleSuffix = `Node.js v${version} Documentation`;
8586

87+
const speculationRulesString = JSON.stringify(SPECULATION_RULES, null, 2);
88+
8689
// Process each entry to create final HTML
8790
const results = entries.map(({ data: { api, heading } }) => {
8891
const fileName = `${api}.js`;
@@ -92,7 +95,8 @@ export async function processJSXEntries(
9295
.replace('{{title}}', `${heading.data.name} | ${titleSuffix}`)
9396
.replace('{{dehydrated}}', serverBundle.get(fileName) ?? '')
9497
.replace('{{importMap}}', clientBundle.importMap ?? '')
95-
.replace('{{entrypoint}}', `./${fileName}?${randomUUID()}`);
98+
.replace('{{entrypoint}}', `./${fileName}?${randomUUID()}`)
99+
.replace('{{speculationRules}}', speculationRulesString);
96100

97101
// Minify HTML (input must be a Buffer)
98102
const finalHTMLBuffer = HTMLMinifier.minify(Buffer.from(renderedHtml), {});

0 commit comments

Comments
 (0)