
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
@overra/koa-history-api-fallback
Advanced tools
Provides a fallback for non-existing directories so that the HTML 5 history API can be used for Koa.
Koa v2 implementation of connect-history-api-fallback
This is a fork of koa-history-api-fallback which is a simple rewrite of connect-history-api-fallback
The middleware is available through NPM and can easily be added.
npm install --save @overra/koa-history-api-fallback
Import the library
const historyApiFallback = require('koa-history-api-fallback');
const app = new Koa();
app.use(historyApiFallback());
To use this with koa-webpack-middleware you'll need to do the following.
const {devMiddleware, hotMiddleware} = require('koa-webpack-middleware')
const historyApiFallback = require('@overra/koa-history-api-fallback')
// ...
const app = new Koa()
const webpackMiddleware = devMiddleware(compiler, options)
app.use(webpackMiddleware) // serve up webpack content
app.use(historyApiFallback()) // catch any other requests and redirect to /index.html
app.use(webpackMiddleware) // serve up webpack content
You can optionally pass options to the library when obtaining the middleware
const middleware = historyApiFallback({});
Override the index (default /index.html
)
historyApiFallback({
index: '/default.html'
});
Override the index when the request url matches a regex pattern. You can either rewrite to a static string or use a function to transform the incoming request.
The following will rewrite a request that matches the /\/soccer/
pattern to /soccer.html
.
historyApiFallback({
rewrites: [
{ from: /\/soccer/, to: '/soccer.html'}
]
});
Alternatively functions can be used to have more control over the rewrite process. For instance, the following listing shows how requests to /libs/jquery/jquery.1.12.0.min.js
and the like can be routed to ./bower_components/libs/jquery/jquery.1.12.0.min.js
. You can also make use of this if you have an API version in the URL path.
historyApiFallback({
rewrites: [
{
from: /^\/libs\/.*$/,
to: function(context) {
return '/bower_components' + context.parsedUrl.pathname;
}
}
]
});
The function will always be called with a context object that has the following properties:
url.parse
.String.match(...)
.This middleware does not log any information by default. If you wish to activate logging, then you can do so via the verbose
option or by specifying a logger function.
historyApiFallback({
verbose: true
});
Alternatively use your own logger
historyApiFallback({
logger: console.log.bind(console)
});
FAQs
Provides a fallback for non-existing directories so that the HTML 5 history API can be used for Koa.
We found that @overra/koa-history-api-fallback demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.