Skip to content

Commit 7377f78

Browse files
authored
chore: preload rule data (#277)
1 parent 12d9fa5 commit 7377f78

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

website/docs/en/rules/rule-manifest.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2+
import { usePageData } from '@rspress/core/runtime';
23

34
type FailingCase = {
45
name: string;
@@ -108,15 +109,21 @@ export default function RuleManifestTable() {
108109
const [error, setError] = useState<string | null>(null);
109110
const [statusFilter, setStatusFilter] = useState<string>('all');
110111
const [groupFilter, setGroupFilter] = useState<string>('all');
111-
112+
const pageData = usePageData();
112113
useEffect(() => {
114+
if (pageData.page.ruleManifest) {
115+
let data: any = pageData.page.ruleManifest;
116+
setRules(data.rules ?? []);
117+
setLoading(false);
118+
return;
119+
}
113120
fetch(RULE_MANIFEST_URL)
114121
.then(res => {
115122
if (!res.ok) throw new Error('Failed to fetch rule-manifest.json');
116123
return res.json();
117124
})
118125
.then(data => {
119-
setRules(Array.isArray(data) ? data : data.rules || []);
126+
setRules(data.rules ?? []);
120127
setLoading(false);
121128
})
122129
.catch(err => {

website/preload-plugin.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { RspressPlugin } from '@rspress/core';
2+
const RULE_MANIFEST_URL =
3+
'https://raw.githubusercontent.com/web-infra-dev/rslint/main/packages/rslint-test-tools/rule-manifest.json';
4+
export function pluginPreloadRule(): RspressPlugin {
5+
return {
6+
name: 'preload-rules-data',
7+
async extendPageData(pageData) {
8+
try {
9+
const result = await fetch(RULE_MANIFEST_URL);
10+
(pageData as any).ruleManifest = await result.json();
11+
} catch (error) {
12+
// don't stop compile if fetch failed
13+
console.error('Failed to fetch rule manifest:', error);
14+
}
15+
},
16+
};
17+
}

website/rspress.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';
66
import { pluginOpenGraph } from 'rsbuild-plugin-open-graph';
77
import { pluginFontOpenSans } from 'rspress-plugin-font-open-sans';
88
import pluginSitemap from 'rspress-plugin-sitemap';
9+
import { pluginPreloadRule } from './preload-plugin';
910

1011
const siteUrl = 'https://rslint.rs';
1112
const description = 'The Rspack-based testing framework';
@@ -55,6 +56,7 @@ export default defineConfig({
5556
],
5657
},
5758
plugins: [
59+
pluginPreloadRule(),
5860
pluginFontOpenSans(),
5961
pluginSitemap({
6062
domain: siteUrl,

0 commit comments

Comments
 (0)