-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't work for single page apps with client-side routing #22
Comments
It works as intended when setting const app = new Elysia()
.use(staticPlugin({ assets: "./static", prefix: "/", alwaysStatic: true }))
.get("/*", () => Bun.file("./static/index.html")); The plugin resolves files differently depending on It's the "request time" file resolution that is interfering with the |
@aryzing Thank you for your response. I tried |
What version are you on? It works with,
|
Hi all 👋 you can do this instead of import { Elysia } from 'elysia';
import { staticPlugin } from '@elysiajs/static';
const app = new Elysia()
.onError((ctx) => {
if (ctx.code === 'NOT_FOUND') {
ctx.set.redirect = '/';
return '';
}
})
.use(staticPlugin({ assets: 'app/dist', prefix: '/' }))
.get('/', () => Bun.file('app/dist/index.html'))
.listen(8080);
console.log(app.server?.url.toString()); and make sure to exclude folders as described here: #17 (comment) |
@aryzing I am using the same versions, it's definitely not working like that. It responds with the |
@Ricki-BumbleDev It may be worth locally adding a |
I can confirm that the suggestion by @bogeychan is not working for SPA's with client side routing since it redirects everything to The workaround suggested by @Ricki-BumbleDev (#22 (comment)) works perfectly. |
@bogeychan's error handler opened opportunities for me. I also want to preserve the path for client side routing to pick where it left off. This is the solution I am using right now: .onError((ctx) => {
if (ctx.code === 'NOT_FOUND') {
if (ctx.path.startsWith('/app/')) {
// ctx.set.redirect = '/app/'; // looses path for the SPA
ctx.set.status = 'OK'
return Bun.file('../frontend/dist/index.html');
} else {
ctx.set.redirect = '/';
}
return '';
}
})
.use(staticPlugin({
assets: '../frontend/dist',
prefix: '/app',
})) Thanks for this repository and the discussion. |
For anyone that stumbles across this and looking for a solution, this is what worked for me: .use(
staticPlugin({
assets: FRONTEND_DIST_PATH,
prefix: "/",
alwaysStatic: true,
}),
)
.get("/*", () => Bun.file(`${FRONTEND_DIST_PATH}/index.html`)) Backend serving api routes and a SPA. Thanks @aryzing |
For single page apps with client-side routing the
index.html
file needs to be served for any path that cannot be resolved otherwise, so that it can be handled by the client-side JS.From the way it works in Express or Fastify I would expect this to work:
But with the Elysia static plugin it doesn't work. In this case I get the
index.html
returned also for my static assets.Would be nice if this could be addressed.
For now I cannot use the plugin and instead wrote this:
(I'm a little bit worried it might be vulnerable to path-traversal attacks, although from what I have tried it seems fine.)
The text was updated successfully, but these errors were encountered: