Set up server-side rendering of Choo app on public route

This commit is contained in:
Robbie Antenesse 2020-09-20 17:22:38 -06:00
parent 0e956f6c67
commit 8f9fe6e97d
1 changed files with 21 additions and 7 deletions

View File

@ -1,16 +1,30 @@
async function routes(fastify, options) {
// This route is not totally necessary because fastify-static serves public/ wholesale, but it's good to be verbose!
fastify.get('/', async (request, reply) => {
return reply.sendFile('index.html');
});
const fs = require('fs');
const path = require('path');
const htmlContainer = fs.readFileSync(path.resolve('public/index.html'));
const chooApp = require('../../app')();
const chooI18n = require('../../app/i18n').I18n;
async function routes(fastify, options) {
// This is overridden by any explicitly named routes, so the API will be fine.
fastify.get('/:chooRoute', async (request, reply) => {
if (/\.\w+$/.test(request.params.chooRoute)) { // If the :chooRoute is a filename, serve the file instead
return reply.sendFile(request.params.chooRoute);
}
// Otherwise, send index.html and allow Choo to route it.
return reply.sendFile('index.html');
// Otherwise, send allow Choo to route it.
// const state = Object.assign({}, chooApp.state);
chooApp.state.language = request.language;
chooApp.state.isLoggedIn = request.isLoggedInUser;
chooApp.state.i18n = new chooI18n(chooApp.state);
chooApp.state.i18n.availableLanguages = fastify.i18n.available.slice();
chooApp.state.i18n.default = Object.assign({}, fastify.i18n.default);
const locale = typeof fastify.i18n[chooApp.state.language] !== 'undefined' ? chooApp.state.language : 'default';
chooApp.state.i18n.language = Object.assign({}, fastify.i18n[locale]);
chooApp.state.i18n.pages = Object.assign({}, fastify.i18n.pages[locale]);
const html = htmlContainer.toString().replace(/\<body\>.+?\<\/body\>/, chooApp.toString('/' + request.params.chooRoute, chooApp.state));
return reply.type('text/html').send(html);
});
}