twn39 / code

:memo: 代码笔记,通过 issue 的方式记录日常遇到的问题和学习笔记
13 stars 1 forks source link

Fastify SPA 路由配置 #386

Open twn39 opened 2 years ago

twn39 commented 2 years ago

配置:

const indexTemplate = readFileSync(assetsPath + '/index.html', {encoding: 'utf-8'});
let compiled = Handlebars.compile(indexTemplate);

const indexHandler = async (request: FastifyRequest, reply: FastifyReply) => {
  let indexHtml = await app.ioredis.get('index.html');
  if (indexHtml == null) {
    indexHtml = compiled({env: process.env['APP_ENV']});
    await app.ioredis.setex('index.html', 300, indexHtml);
  }
  return reply.type('text/html').send(indexHtml);
}

app.get('/', indexHandler);

app.setNotFoundHandler(indexHandler);