Skip to content

Commit 9226b26

Browse files
committed
ui: optimize service worker cache load
1 parent 2e9e93c commit 9226b26

8 files changed

Lines changed: 34 additions & 23 deletions

File tree

packages/hydrooj/src/handler/manage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class SystemConfigHandler extends SystemHandler {
177177
let value = this.ctx.setting.configSource;
178178

179179
const processNode = (node: any, schema: Schema<any, any>, parent?: any, accessKey?: string) => {
180+
if (!node) return;
180181
if (['union', 'intersect'].includes(schema.type)) {
181182
for (const item of schema.list) processNode(node, item, parent, accessKey);
182183
}
@@ -193,7 +194,9 @@ class SystemConfigHandler extends SystemHandler {
193194
const temp = yaml.load(this.ctx.setting.configSource);
194195
for (const schema of this.ctx.setting.settings) processNode(temp, schema);
195196
value = yaml.dump(temp);
196-
} catch (e) { }
197+
} catch (e) {
198+
logger.error('Failed to process config', e.message);
199+
}
197200
this.response.body = {
198201
schema: Schema.intersect(this.ctx.setting.settings).toJSON(),
199202
value,

packages/hydrooj/src/settings.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,10 @@ export class SettingService extends Service {
172172
});
173173
const that = this;
174174
const getAccess = (path: (string | symbol)[]) => {
175-
if (path.some((p) => SettingService.blacklist.includes(p.toString()))) throw new Error('Invalid path');
175+
if (path.some((p) => SettingService.blacklist.includes(p.toString()))) throw new Error(`Invalid path: ${path.join('.')}`);
176176
let currentValue = curValue;
177-
for (const p of path) {
178-
currentValue = currentValue[p];
179-
}
180-
if (typeof currentValue !== 'object' || !currentValue) return currentValue;
177+
for (const p of path) currentValue = currentValue[p];
178+
if (typeof currentValue !== 'object' || !currentValue || Array.isArray(currentValue)) return currentValue;
181179
return new Proxy(currentValue, {
182180
get(self, key: string) {
183181
return getAccess(path.concat(key));

packages/ui-default/backendlib/builder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ const federationPlugin: esbuild.Plugin = {
3030
setup(b) {
3131
const packages = {
3232
react: 'React',
33-
'react-dom': 'ReactDOM',
33+
'react/jsx-runtime': 'jsxRuntime',
34+
'react-dom/client': 'ReactDOM',
3435
jquery: '$',
3536
};
3637
b.onResolve({ filter: /^@hydrooj\/ui-default/ }, () => ({
3738
path: 'api',
3839
namespace: 'ui-default',
3940
}));
4041
for (const key in packages) {
41-
b.onResolve({ filter: new RegExp(`^${key}($|\\/)`) }, () => ({
42+
b.onResolve({ filter: new RegExp(`^${key}$`) }, () => ({
4243
path: packages[key],
4344
namespace: 'ui-default',
4445
}));

packages/ui-default/build/config/webpack.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { dirname } from 'path';
2+
import { dirname, join } from 'path';
33
import { sentryWebpackPlugin } from '@sentry/webpack-plugin';
44
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
55
import CopyWebpackPlugin from 'copy-webpack-plugin';
@@ -136,6 +136,9 @@ export default async function (env: { watch?: boolean, production?: boolean, mea
136136
vj: root(),
137137
'react/jsx-runtime': require.resolve('react/jsx-runtime'),
138138
react: require.resolve('react'),
139+
'react-dom/client': require.resolve('react-dom/client'),
140+
'react-dom/server': join(dirname(require.resolve('react-dom/package.json')), 'server.browser.js'),
141+
'react-dom': require.resolve('react-dom'),
139142
},
140143
},
141144
module: {
@@ -282,7 +285,8 @@ export default async function (env: { watch?: boolean, production?: boolean, mea
282285
monaco: 'monaco-editor/esm/vs/editor/editor.api',
283286
}),
284287
new ExtractCssPlugin({
285-
filename: '[name].css?[fullhash:6]',
288+
filename: `[name]-${version}.css?[fullhash:6]`,
289+
chunkFilename: '[name]-[chunkhash:6].chunk.css',
286290
}),
287291
new WebpackManifestPlugin({}),
288292
new webpack.IgnorePlugin({ resourceRegExp: /(^\.\/locale$)/ }),

packages/ui-default/build/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ async function main() {
117117
});
118118
} else {
119119
await runWebpack(argv.options as any);
120-
if (fs.existsSync('public/theme.css')) {
121-
fs.copyFileSync('public/theme.css', `public/theme-${pkg.version}.css`);
122-
}
123120
await Promise.all(globbySync('public/**/*.map').map((i) => fs.remove(i)));
124121
}
125122
process.chdir(dir);

packages/ui-default/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ const codeFontRange = {
162162

163163
const defaultAbout = (yaml.load(readFileSync(join(__dirname, 'setting.yaml'), 'utf-8')) as any).about.value;
164164

165-
export function apply(ctx: Context) {
165+
export const name = 'ui-default';
166+
export const Config = Schema.object({
167+
serviceWorker: Schema.object({
168+
preload: Schema.string().default(''),
169+
assets: Schema.array(Schema.string()).default([]),
170+
domains: Schema.array(Schema.string()).default([]),
171+
}).description('Service worker optimization settings').experimental(),
172+
});
173+
174+
export function apply(ctx: Context, config: ReturnType<typeof Config>) {
166175
ctx.inject(['setting'], (c) => {
167176
c.setting.PreferenceSetting(
168177
SettingModel.Setting('setting_display', 'rounded', false, 'boolean', 'Rounded Corners'),
@@ -179,7 +188,6 @@ export function apply(ctx: Context) {
179188
'ui-default': Schema.object({
180189
footer_extra_html: Schema.string().role('textarea').default(''),
181190
nav_logo_dark: Schema.string().default('/components/navigation/nav-logo-small_dark.png'),
182-
preload: Schema.string().default(''),
183191
domainNavigation: Schema.boolean().default(true).description('Show Domain Navigation'),
184192
about: Schema.string().role('markdown').default(defaultAbout),
185193
enableScratchpad: Schema.boolean().default(true).description('Enable Scratchpad Mode'),
@@ -202,15 +210,15 @@ export function apply(ctx: Context) {
202210
});
203211
ctx.on('handler/after', async (that) => {
204212
that.UiContext.SWConfig = {
205-
preload: SystemModel.get('ui-default.preload'),
213+
preload: config.serviceWorker.preload,
206214
hosts: [
207215
`http://${that.request.host}`,
208216
`https://${that.request.host}`,
209217
SystemModel.get('server.url'),
210218
SystemModel.get('server.cdn'),
211219
],
212-
assets: ((SystemModel.get('ui-default.assets') || '').split(',')).filter((i) => i) || [],
213-
domains: SystemModel.get('ui-default.domains') || [],
220+
assets: config.serviceWorker.assets,
221+
domains: config.serviceWorker.domains,
214222
};
215223
});
216224
ctx.plugin(TemplateService);

packages/ui-default/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"@fontsource/source-code-pro": "^5.2.7",
2222
"@fontsource/ubuntu-mono": "^5.2.8",
2323
"@hydrooj/utils": "workspace:^",
24-
"@sentry/browser": "^10.20.0",
25-
"@sentry/webpack-plugin": "^4.4.0",
24+
"@sentry/browser": "^10.22.0",
25+
"@sentry/webpack-plugin": "^4.5.0",
2626
"@simplewebauthn/browser": "13.2.2",
2727
"@svgr/webpack": "^8.1.0",
2828
"@types/html-to-text": "^9.0.4",
@@ -33,7 +33,7 @@
3333
"@types/markdown-it": "^14.1.2",
3434
"@types/nunjucks": "^3.2.6",
3535
"@types/pickadate": "^3.5.35",
36-
"@types/qrcode": "^1.5.5",
36+
"@types/qrcode": "^1.5.6",
3737
"@types/react": "^18.3.26",
3838
"@types/react-dom": "^18.3.7",
3939
"@types/redux-logger": "^3.0.13",
@@ -99,7 +99,7 @@
9999
"redux-thunk": "^3.1.0",
100100
"rupture": "^0.7.1",
101101
"sass": "^1.93.2",
102-
"sass-loader": "^16.0.5",
102+
"sass-loader": "^16.0.6",
103103
"schemastery": "^3.17.1",
104104
"shorty.js": "^1.0.1",
105105
"slideout": "^1.0.1",

packages/ui-default/service-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ self.addEventListener('notificationclick', (event) => {
8787
}));
8888
});
8989

90-
const PRECACHE = `precache-${process.env.VERSION}`;
90+
const PRECACHE = 'ui-resources-cache';
9191
const DO_NOT_PRECACHE = ['.worker.js', 'fonts'];
9292

9393
function shouldCachePath(path: string) {

0 commit comments

Comments
 (0)