Skip to content

Doesn't work for single page apps with client-side routingΒ #22

@Ricki-BumbleDev

Description

@Ricki-BumbleDev

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:

app.use(staticPlugin({ assets: './static', prefix: '/'}));
app.get('/*', () => Bun.file('./static/index.html'));

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:

app.get('/*', async ({ path }) => {
  const staticFile = Bun.file(`./static/${path}`);
  const fallBackFile = Bun.file('./static/index.html');
  return (await staticFile.exists()) ? staticFile : fallBackFile;
});

(I'm a little bit worried it might be vulnerable to path-traversal attacks, although from what I have tried it seems fine.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions