Skip to content

Commit

Permalink
Merge pull request #9 from magicpages/issue/8-newsletter-links
Browse files Browse the repository at this point in the history
Issue/8 newsletter links
  • Loading branch information
betschki authored Jun 30, 2024
2 parents 80b75a1 + f19b8d1 commit 1484793
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ghost-bunnycdn-middleware",
"version": "1.1.3",
"version": "1.2.0",
"description": "A middleware between a Ghost CMS instance and BunnyCDN, supposed to run in a Docker stack, alongside a Ghost container.",
"main": "dist/index.js",
"author": "Jannis Fedoruk-Betschki <[email protected]>",
Expand Down
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const app = express();
app.use(bodyParser.json());

const PORT = process.env.PORT || 3000;
// remove trailing slash from GHOST_URL for consistency
const GHOST_URL = process.env.GHOST_URL?.replace(/\/$/, '');
const BUNNYCDN_API_KEY = process.env.BUNNYCDN_API_KEY;
const BUNNYCDN_PULL_ZONE_ID = process.env.BUNNYCDN_PULL_ZONE_ID;
Expand Down Expand Up @@ -155,18 +154,21 @@ const purgeCache = async (): Promise<void> => {
app.use((req, res, next) => {
const reqPath = req.path;

// Check if the path does not end with a slash, does not have a file extension, and does not include API endpoints
// Paths that should not have a trailing slash appended
const excludedPaths = ['/r/', '/api/'];

// Check if the path ends with a slash or has a file extension or is an excluded path
if (
reqPath !== '/' &&
!reqPath.endsWith('/') &&
!path.extname(reqPath) &&
!reqPath.includes('/api/')
reqPath.endsWith('/') ||
path.extname(reqPath) ||
excludedPaths.some((excludedPath) => reqPath.startsWith(excludedPath))
) {
const query = req.url.slice(reqPath.length);
res.redirect(301, `${reqPath}/${query}`);
return;
return next();
}
next();

// If the path is not in the excluded list and does not end with a slash, append one
const query = req.url.slice(reqPath.length);
res.redirect(301, `${reqPath}/${query}`);
});

app.use(async (req, res) => {
Expand Down

0 comments on commit 1484793

Please sign in to comment.