Skip to content

Commit

Permalink
fix: fixed trailing slash issue
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Oct 10, 2023
1 parent b1901d2 commit a8b2893
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const methodOverride = require('koa-methodoverride');
const ms = require('ms');
const ratelimit = require('@ladjs/koa-simple-ratelimit');
const redisStore = require('koa-redis');
const removeTrailingSlashes = require('koa-no-trailing-slash');
const requestId = require('express-request-id');
const requestReceived = require('request-received');
const responseTime = require('response-time');
Expand All @@ -47,6 +46,19 @@ const sharedConfig = require('@ladjs/shared-config');
const views = require('@ladjs/koa-views');
const { boolean } = require('boolean');

// https://gist.github.com/titanism/241fc0c5f1c1a0b7cae3d97580e435fb
function removeTrailingSlashes(ctx, next) {
const { path, search } = ctx.request;
if (path !== '/' && !path.startsWith('//') && path.slice(-1) === '/') {
const redirectUrl = path.slice(0, -1) + search;
ctx.response.status = 301;
ctx.redirect(redirectUrl);
return;
}

return next();
}

const defaultSrc = isSANB(process.env.WEB_HOST)
? [
"'self'",
Expand Down Expand Up @@ -241,7 +253,7 @@ class Web {
if (this.config.auth) app.use(auth(this.config.auth));

// remove trailing slashes
app.use(removeTrailingSlashes());
app.use(removeTrailingSlashes);

// security
// (needs to come before i18n so HSTS header gets added)
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"koa-json": "^2.0.2",
"koa-meta": "^4.0.1",
"koa-methodoverride": "^2.0.0",
"koa-no-trailing-slash": "^2.1.0",
"koa-redirect-loop": "^3.0.2",
"koa-redis": "^4.0.1",
"lodash": "^4.17.21",
Expand Down

0 comments on commit a8b2893

Please sign in to comment.