From f65c12347449cab13598c7a9c20b39a22cc8becc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E7=BA=B8=E5=BF=98=E5=BF=A7?= Date: Wed, 17 Apr 2024 16:54:01 +0800 Subject: [PATCH] fix: support single page application routing https://github.com/elysiajs/elysia-static/issues/22 --- service/index.ts | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/service/index.ts b/service/index.ts index 0df7e8da..05f1f1f4 100644 --- a/service/index.ts +++ b/service/index.ts @@ -1,20 +1,15 @@ import { Elysia } from 'elysia' import { staticPlugin } from '@elysiajs/static' -new Elysia() - // .use( - // staticPlugin({ - // assets: 'dist/assets', - // prefix: '/assets', - // }), - // ) - // .get('/*', () => Bun.file('dist/index.html')) - .onError(ctx => { - if (ctx.code === 'NOT_FOUND') { - ctx.set.redirect = '/' - return '' - } - }) - .use(staticPlugin({ assets: 'dist', prefix: '/' })) - .get('/', () => Bun.file('dist/index.html')) - .listen(3000) +const app = new Elysia() + +app.use( + staticPlugin({ + assets: 'dist/assets', + prefix: '/assets', + }), +) + +app.get('/*', () => Bun.file('dist/index.html')) + +app.listen(3000)