connect-pushstate is a Connect middleware designed to facilitate client-side routing in single-page applications (SPAs) by rewriting non-asset requests to the application's root. Released as version 1.1.0, this package appears to be abandoned, with its last update occurring approximately nine years ago. It operates by identifying requests that lack a file extension (presumed to be application routes) and internally redirects them to the root path, while preserving the original URL for the client-side router to process. Requests with file extensions (e.g., for images, stylesheets, JavaScript files) are left untouched. The middleware offers optional `root`, `allow`, and `disallow` regular expression configurations to provide finer control over which paths are affected, making it suitable for scenarios where an API or specific server-rendered pages coexist with an SPA. Its key differentiator lies in its simple, focused approach within the `connect` ecosystem, providing a lightweight solution for pushstate routing without the overhead of full-fledged server-side frameworks.
npm install connect-pushstateVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to integrate `connect-pushstate` with `connect` and `serve-static` to serve a single-page application, routing all non-asset requests to the root index.html for client-side routing.
Migrate to a currently maintained alternative. For Express-based applications, a simple wildcard route to serve `index.html` is common. For other frameworks, look for dedicated SPA routing middleware.
Consider using more modern routing solutions available in frameworks like Express (e.g., `res.sendFile('index.html')` with a wildcard route) or integrated client-side routers.If in a pure ESM project, you may need to use dynamic `import()` or wrapper functions, but it's generally recommended to stick to `require()` for this package. A more modern alternative might be preferred.
Leverage the `allow` and `disallow` options with precise regular expressions to explicitly control which paths are affected, especially for API endpoints or specific server-side routes.
Use `const pushState = require('connect-pushstate');` as `connect-pushstate` is a CommonJS module.Ensure `app` is an instance created by `connect()` or `express()`. For example, `const app = connect();`.
Ensure your static file serving middleware (e.g., `serve-static`) is correctly configured to serve your application's `index.html` for the root path, and that `index.html` exists in the specified static directory and is reachable.
Add `const pushState = require('connect-pushstate');` at the top of your file to properly import the middleware.