Files
wekan/server/lib/customHeadRender.js
T
Lauri Ojansivu 631a7fdfe2 LibreJS: a JS Web Labels page for the browser bundle.
LibreJS runs a page's JavaScript only when it can tell the code is free.
A Meteor bundle is generated, minified and named by hash, so it cannot
carry per-file @license comments; LibreJS's other way is a JS Web Labels
table, linked with rel="jslicense". WeKan now serves /jslicense.html,
built from the client bundle it is actually serving (web.browser and
web.browser.legacy, under ROOT_URL's path), labelling each script with
the free licenses the bundle combines - Expat (MIT), Apache-2.0, Modified
BSD, FreeBSD and ISC, under LibreJS's own names and addresses - and with
WeKan's source at the running version. Every page links it from <head>,
whether or not the custom head is on.

models/lib/jsWebLabels.js builds the table and the link;
tests/jsWebLabels.test.cjs checks the scripts chosen from a manifest, the
table LibreJS reads, escaping and the wiring. releases/risk-baseline.json
allows the license addresses, and ntfy.sh, which the ntfy channel
(d873af70e8) names in a placeholder and comments.

Thanks to alexgleason and xet7 !

Fixes #1040.
2026-10-14 14:39:40 +03:00

92 lines
3.6 KiB
JavaScript

import { WebApp } from 'meteor/webapp';
import { WebAppInternals } from 'meteor/webapp';
import Settings from '/models/settings';
const { safeMetaTags } = require('/models/lib/customHeadMetaTags');
// Cache the setting since the boilerplate callback is synchronous
let cachedSetting = null;
Meteor.startup(async () => {
// Load initial setting
cachedSetting = await Settings.findOneAsync();
// Keep cache updated reactively
Settings.find().observeChanges({
added() { Settings.findOneAsync().then(s => { cachedSetting = s; }); },
changed() { Settings.findOneAsync().then(s => { cachedSetting = s; }); },
removed() { Settings.findOneAsync().then(s => { cachedSetting = s; }); },
});
// Use Meteor's official API to modify the HTML boilerplate
WebAppInternals.registerBoilerplateDataCallback('wekan-custom-head', (request, data) => {
try {
const setting = cachedSetting;
// Initialize head array if it doesn't exist
if (!data.head) {
data.head = '';
}
// Always set title tag based on productName
const productName = (setting && setting.productName) ? setting.productName : 'Wekan';
data.head += `\n <title>${productName}</title>\n`;
// #6419: ensure a responsive viewport meta tag. Without it, mobile browsers
// lay the page out at their default ~980px virtual width and scale it down,
// so the whole UI looks tiny ("icons too small to tap"), window.innerWidth
// reports 980 on a 390px phone, and the `@media (max-width: 800px)` rules
// never match. Declaring width=device-width makes WeKan responsive on phones.
// User zoom is left enabled (no user-scalable=no) for accessibility.
if (!/name=["']?viewport/i.test(data.head)) {
data.head +=
' <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">\n';
}
// #1040: where LibreJS finds the license of every script on the page
// (server/jsWebLabels.js).
if (!/rel=["']?jslicense/i.test(data.head)) {
const { jsLicenseLink } = require('/models/lib/jsWebLabels');
data.head += ` ${jsLicenseLink((typeof __meteor_runtime_config__ !== 'undefined' && __meteor_runtime_config__.ROOT_URL_PATH_PREFIX) || '')}\n`;
}
// Only add custom head tags if enabled
if (!setting || !setting.customHeadEnabled) {
return data;
}
let injection = '';
// #4042: the custom meta tags - <meta> elements only, see
// models/lib/customHeadMetaTags.js.
for (const tag of safeMetaTags(setting.customHeadMetaTags)) {
injection += ` ${tag}\n`;
}
// Add custom link tags (except manifest if custom manifest is enabled)
if (setting.customHeadLinkTags && setting.customHeadLinkTags.trim()) {
let linkTags = setting.customHeadLinkTags;
if (setting.customManifestEnabled) {
// Remove any manifest links from custom link tags to avoid duplicates
linkTags = linkTags.replace(/<link[^>]*rel=["\']?manifest["\']?[^>]*>/gi, '');
}
if (linkTags.trim()) {
injection += linkTags + '\n';
}
}
// Add manifest link if custom manifest is enabled
if (setting.customManifestEnabled) {
injection += ' <link rel="manifest" href="/site.webmanifest" crossorigin="use-credentials">\n';
}
if (injection.trim()) {
// Append custom head content to the existing head
data.head += injection;
}
return data;
} catch (e) {
console.error('[Custom Head] Error in boilerplate callback:', e.message, e.stack);
return data;
}
});
});