preactjs / preact-router

:earth_americas: URL router for Preact.
http://npm.im/preact-router
MIT License
1.01k stars 156 forks source link

Does not work on Vercel #450

Closed alfawal closed 7 months ago

alfawal commented 1 year ago

I've just tried this package and it seemed fine. Then I deployed my app on Vercel. Visiting /admin gives me a 404 error that is handled by Vercel itself (not my <Page404 />).

app.jsx:

export function App() {
  return (
    <Router>
      <Page404 default />
      <Home path="/" />
      <Admin path="/admin" />
    </Router>
  );
}

main.jsx:

render(
  <>
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <App />
    </ThemeProvider>
  </>,
  document.getElementById("app")
);

The 404 error on Vercel when visiting /admin. image

marvinhagemeister commented 1 year ago

By default preact-router runs on the client in the browser. To make vercel be aware that you're hosting an SPA they need a custom redirect to redirect all routes to index.html. Their docs have an example on how to do that: https://vercel.com/docs/concepts/projects/project-configuration#legacy-spa-fallback

Shuunen commented 7 months ago

I had the same issue on Netlify, here is the doc : https://docs.netlify.com/routing/redirects/rewrites-proxies/#history-pushstate-and-single-page-apps

Create a _redirects file in your public folder with this content : /* /index.html 200

marvinhagemeister commented 7 months ago

Marking as resolved.