vite icon indicating copy to clipboard operation
vite copied to clipboard

multi-page apps and client-side routing with child paths

Open akvadrako opened this issue 5 years ago • 4 comments

Clear and concise description of the problem

I'm using vite with a multi-page app like so:

  • /index.html
  • /subdir/index.html

When I navigate to /subdir/ I see the 2nd index.html as expected. But when I navigate to anything under that directory, like /subdir/xyz, the top-level index.html is rendered. This makes it impossible to have a multi-page app with client-side routing based on child paths.

Suggested solution

I suggest that vite dev and preview commands should serve the nearest index.html file in the hierarchy.

So navigating to /subdir/xyz and /subdir/xyz/blah should serve /subdir/index.html, if it exists.

Alternative

Clarify the documentation that the root index.html is always used unless the path exactly matches another page.

Additional context

  • behavior tested with vite v2.3.8, using vanilla scaffolding

Validations

akvadrako avatar Jun 30 '21 08:06 akvadrako

I found a workaround using a middleware :

import { defineConfig } from "vite";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    {
      name: "middleware",
      apply: "serve",
      configureServer(viteDevServer) {
        return () => {
          viteDevServer.middlewares.use(async (req, res, next) => {
            if (req.originalUrl.startsWith("/subdir")) {
              req.url = "/subdir/index.html";
            }

            next();
          });
        };
      }
    }
  ],
  ...
});

Don't really known if it is the correct way

asecher avatar Jul 09 '21 08:07 asecher

I ran into a closely related issue (i think?) by using config.base and trying to navigate to a subdirectory. For example in vite.config.ts:

export default defineConfig({
  base: "foo"
});

and a file structure like:

.
├── bar
│   ├── index.html
└── index.html

I would expect to be able to load http://localhost:5000/foo/bar. However this URL will return the root index page, but http://localhost:5000/foo/bar/index.html seems to work. Of course this isn't ideal for client side routing, etc.

@asecher thanks for the snippet. That worked for me as well.

ianschmitz avatar Aug 11 '21 03:08 ianschmitz

@asecher Thanks for that snippet, that really helped! I had to adjust it a bit because I think the plugin API has changed.

If you have several pages and have already configured it in rollupOptions.input per the docs then you might also like to iterate over those keys:

const multiPageAppIndexRouting = () => ({
  name: "configure-server",
  configureServer(server) {
    return () => {
      server.middlewares.use(async (req, res, next) => {
        for (const appName in server.config.build.rollupOptions.input) {
          if (req.originalUrl.startsWith(`/${appName}`)) {
            req.url = `/${appName}/index.html`;
            break;
          }
        }
        next();
      });
    };
  }
});

matt-snider avatar Mar 07 '22 18:03 matt-snider

Duplicate of https://github.com/vitejs/vite/issues/2958

bluwy avatar Mar 12 '22 14:03 bluwy