diff --git a/forge/routes/ui/index.js b/forge/routes/ui/index.js index fc434ba71..4789c9f26 100644 --- a/forge/routes/ui/index.js +++ b/forge/routes/ui/index.js @@ -36,12 +36,6 @@ module.exports = async function (app) { let injection = '' // check which tools we are using - if (telemetry.frontend.plausible?.domain) { - const domain = telemetry.frontend.plausible.domain - const extension = telemetry.frontend.plausible.extension - injection += `` - } - if (telemetry.frontend.posthog?.apikey) { // add to frontend const apihost = telemetry.frontend.posthog.apiurl || 'https://app.posthog.com' @@ -83,6 +77,52 @@ module.exports = async function (app) { return cachedIndex } + async function getIndexFile (request, reply, app) { + if (app.config.telemetry?.frontend) { + let injectedContent = await injectAnalytics(app.config) + injectedContent = updateSEOTags(request, injectedContent) + + return reply.type('text/html').send(injectedContent) + } else { + const filepath = path.join(frontendAssetsDir, 'index.html') + let data = await fsp.readFile(filepath, 'utf8') + data = updateSEOTags(request, data) + + return reply.type('text/html').send(data) + } + } + + function updateSEOTags (request, data) { + switch (true) { + case !request.sid && request.raw.url === '/': + // We can safely assume that unauthenticated users reaching the root endpoint are on the login page + // so we can safely replace tags used by crawlers to serve them statically for ease of indexing + data = data.replace( + 'FlowFuse', + 'Log in - FlowFuse' + ) + data = data.replace( + '', + '' + ) + return data + case !request.sid && request.raw.url.includes('/account/create'): + // We can safely assume that unauthenticated users reaching the root endpoint are on the account creation page + // so we can safely replace tags used by crawlers to serve them statically for ease of indexing + data = data.replace( + 'FlowFuse', + 'Sign up - FlowFuse' + ) + data = data.replace( + '', + '' + ) + return data + default: + return data + } + } + let cachedIndex = null // Check the frontend has been built @@ -92,50 +132,27 @@ module.exports = async function (app) { app.register(Avatar, { prefix: '/avatar' }) + // Setup static file serving for the UI assets. + app.register(require('@fastify/static'), { + index: false, + root: frontendAssetsDir + }) + app.get('/', async (request, reply) => { if (!app.settings.get('setup:initialised')) { reply.redirect('/setup') return } - if (app.config.telemetry?.frontend?.plausible) { - app.log.warn('Configuration found for Plausible. Please note that support for Plausible will be deprecated after FlowFuse 0.9') - } - // check if we need to inject plausible - if (app.config.telemetry?.frontend) { - const injectedContent = await injectAnalytics(app.config) - reply.type('text/html').send(injectedContent) - } else { - reply.sendFile('index.html') - } - return reply - }) - // Setup static file serving for the UI assets. - app.register(require('@fastify/static'), { - index: false, - root: frontendAssetsDir + return await getIndexFile(request, reply, app) }) // Any requests not handled by this time get served `index.html`. // This allows the frontend vue router to change the browser URL and we cope // if the user then hits reload app.setNotFoundHandler(async (request, reply) => { - // // check if we need to inject plausible - // if (app.config.telemetry.frontend?.plausible?.domain) { - // const injectedContent = await injectPlausible(app.config.telemetry.frontend.plausible.domain, app.config.telemetry.frontend.plausible.extension) - // reply.type('text/html').send(injectedContent) - // } else { - // reply.sendFile('index.html') - // } - // check if we need to inject plausible if (request.method === 'GET' && !request.url.startsWith('/api')) { - if (app.config.telemetry?.frontend) { - const injectedContent = await injectAnalytics(app.config) - reply.type('text/html').send(injectedContent) - } else { - reply.sendFile('index.html') - } - return reply + return await getIndexFile(request, reply, app) } else { return reply.status(404).send() } diff --git a/frontend/src/index.html b/frontend/src/index.html index 6b10c9d2d..d6cc6e871 100644 --- a/frontend/src/index.html +++ b/frontend/src/index.html @@ -7,6 +7,7 @@ + <%= htmlWebpackPlugin.options.title %>