From ac4623ee6d36320147484f380c4486facfd34a7f Mon Sep 17 00:00:00 2001 From: Opliko Date: Wed, 9 Aug 2023 01:41:04 +0200 Subject: [PATCH] fix: copy FA fonts to build directory instead of serving them directly (#11891) resolves issues when proxies don't fall back to NodeBB for assets --- public/scss/fontawesome/loader.scss | 2 +- src/meta/css.js | 18 +++++++++++++++++- src/routes/index.js | 2 -- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/public/scss/fontawesome/loader.scss b/public/scss/fontawesome/loader.scss index ceeca56340..81700f154d 100644 --- a/public/scss/fontawesome/loader.scss +++ b/public/scss/fontawesome/loader.scss @@ -1,4 +1,4 @@ -$fa-font-path: "./vendor/fontawesome/webfonts"; +$fa-font-path: "./fontawesome/webfonts"; @import "fontawesome"; @import "v4-shims"; @import "nodebb-shims"; \ No newline at end of file diff --git a/src/meta/css.js b/src/meta/css.js index 490ac8c215..5b665f6d43 100644 --- a/src/meta/css.js +++ b/src/meta/css.js @@ -5,6 +5,7 @@ const winston = require('winston'); const nconf = require('nconf'); const fs = require('fs'); const path = require('path'); +const { mkdirp } = require('mkdirp'); const plugins = require('../plugins'); const db = require('../database'); @@ -139,6 +140,20 @@ function getFontawesomeStyle() { return styles.map(style => `@import "fontawesome/style-${style}";`).join('\n'); } +async function copyFontAwesomeFiles() { + await mkdirp(path.join(__dirname, '../../build/public/fontawesome/webfonts')); + const fonts = await fs.promises.opendir(path.join(utils.getFontawesomePath(), '/webfonts')); + const copyOperations = []; + for await (const file of fonts) { + if (file.isFile() && file.name.match(/\.(woff2|ttf|eot)?$/)) { // there shouldn't be any legacy eot files, but just in case we'll allow it + copyOperations.push( + fs.promises.copyFile(path.join(fonts.path, file.name), path.join(__dirname, '../../build/public/fontawesome/webfonts/', file.name)) + ); + } + } + await Promise.all(copyOperations); +} + async function filterMissingFiles(filepaths) { const exists = await Promise.all( filepaths.map(async (filepath) => { @@ -186,7 +201,7 @@ async function getBundleMetadata(target) { const paths = [ path.join(__dirname, '../../node_modules'), path.join(__dirname, '../../public/scss'), - path.join(__dirname, '../../public/vendor/fontawesome/scss'), + path.join(__dirname, '../../public/fontawesome/scss'), path.join(utils.getFontawesomePath(), 'scss'), ]; @@ -324,6 +339,7 @@ CSS.buildBundle = async function (target, fork) { await Promise.all([ fs.promises.writeFile(path.join(__dirname, '../../build/public', `${target}.css`), ltr.code), fs.promises.writeFile(path.join(__dirname, '../../build/public', `${target}-rtl.css`), rtl.code), + copyFontAwesomeFiles(), ]); return [ltr.code, rtl.code]; }; diff --git a/src/routes/index.js b/src/routes/index.js index 97fa857a1a..4008f1565a 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -9,7 +9,6 @@ const meta = require('../meta'); const controllers = require('../controllers'); const controllerHelpers = require('../controllers/helpers'); const plugins = require('../plugins'); -const utils = require('../utils'); const authRoutes = require('./authentication'); const writeRoutes = require('./write'); @@ -173,7 +172,6 @@ function addCoreRoutes(app, router, middleware, mounts) { const statics = [ { route: '/assets', path: path.join(__dirname, '../../build/public') }, { route: '/assets', path: path.join(__dirname, '../../public') }, - { route: '/assets/vendor/fontawesome/webfonts', path: path.join(utils.getFontawesomePath(), 'webfonts') }, ]; const staticOptions = { maxAge: app.enabled('cache') ? 5184000000 : 0,