Skip to content

Commit 43bdb8a

Browse files
committed
fix: fixed trailing slash issue
1 parent 4db896d commit 43bdb8a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,25 @@ const koa404Handler = require('koa-404-handler');
2121
const koaConnect = require('koa-connect');
2222
const multimatch = require('multimatch');
2323
const ratelimit = require('@ladjs/koa-simple-ratelimit');
24-
const removeTrailingSlashes = require('koa-no-trailing-slash');
2524
const requestId = require('express-request-id');
2625
const requestReceived = require('request-received');
2726
const responseTime = require('response-time');
2827
const sharedConfig = require('@ladjs/shared-config');
2928
const { boolean } = require('boolean');
3029

30+
// https://gist.github.com/titanism/241fc0c5f1c1a0b7cae3d97580e435fb
31+
function removeTrailingSlashes(ctx, next) {
32+
const { path, search } = ctx.request;
33+
if (path !== '/' && !path.startsWith('//') && path.slice(-1) === '/') {
34+
const redirectUrl = path.slice(0, -1) + search;
35+
ctx.response.status = 301;
36+
ctx.redirect(redirectUrl);
37+
return;
38+
}
39+
40+
return next();
41+
}
42+
3143
class API {
3244
// eslint-disable-next-line complexity
3345
constructor(config, Users) {
@@ -108,7 +120,7 @@ class API {
108120
if (this.config.auth) app.use(auth(this.config.auth));
109121

110122
// Remove trailing slashes
111-
app.use(removeTrailingSlashes());
123+
app.use(removeTrailingSlashes);
112124

113125
// I18n
114126
if (this.config.i18n) {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"koa-connect": "^2.1.0",
3333
"koa-etag": "^4.0.0",
3434
"koa-json": "^2.0.2",
35-
"koa-no-trailing-slash": "^2.1.0",
3635
"lodash": "^4.17.21",
3736
"multimatch": "5",
3837
"request-received": "^0.0.3",

0 commit comments

Comments
 (0)