From f999bfb741f9b20cc6c29608efc5cc1a8cb71cb9 Mon Sep 17 00:00:00 2001 From: Cosmin Budinschi Date: Wed, 5 Feb 2020 00:09:41 +0200 Subject: [PATCH 1/2] fix: trailing slash redirect --- packages/gatsby/src/commands/serve.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/gatsby/src/commands/serve.js b/packages/gatsby/src/commands/serve.js index 8c1129af6cd20..d529d846b8a2d 100644 --- a/packages/gatsby/src/commands/serve.js +++ b/packages/gatsby/src/commands/serve.js @@ -40,6 +40,24 @@ const readMatchPaths = async program => { return JSON.parse(rawJSON) } +/** + * Express middleware that adds url trailing slash in order to + * trick static middleware and thus preventing redirect + * This needs to be used before express static middleware + * @param {*} req + * @param {*} res + * @param {*} next + */ +const directoryRedirectPrevention = (req, res, next) => { + if (req.url == `/` || req.url.indexOf(`.`) || req.url.slice(-1) !== `/`) + return next() + + req.originalUrl += `/` // https://expressjs.com/en/api.html#req.originalUrl + req.url += `/` + + return next() +} + const matchPathRouter = (matchPaths, options) => (req, res, next) => { const { url } = req if (req.accepts(`html`)) { @@ -85,6 +103,7 @@ module.exports = async program => { app.use(telemetry.expressMiddleware(`SERVE`)) router.use(compression()) + router.use(directoryRedirectPrevention) router.use(express.static(`public`)) const matchPaths = await readMatchPaths(program) router.use(matchPathRouter(matchPaths, { root })) From 43802a15174fb9bac1ea09da163084763c03e1fd Mon Sep 17 00:00:00 2001 From: Cosmin Budinschi Date: Wed, 12 Feb 2020 19:44:11 +0200 Subject: [PATCH 2/2] fix(static-serve): add trailing slash to url before static middleware and prevent redirect --- packages/gatsby/src/commands/serve.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/src/commands/serve.js b/packages/gatsby/src/commands/serve.js index d529d846b8a2d..b1e75fdeb4b9e 100644 --- a/packages/gatsby/src/commands/serve.js +++ b/packages/gatsby/src/commands/serve.js @@ -14,6 +14,7 @@ const telemetry = require(`gatsby-telemetry`) const detectPortInUseAndPrompt = require(`../utils/detect-port-in-use-and-prompt`) const getConfigFile = require(`../bootstrap/get-config-file`) const preferDefault = require(`../bootstrap/prefer-default`) +const fileExtensionRegEx = /.+\..+$/g onExit(() => { telemetry.trackCli(`SERVE_STOP`) @@ -41,17 +42,22 @@ const readMatchPaths = async program => { } /** - * Express middleware that adds url trailing slash in order to - * trick static middleware and thus preventing redirect - * This needs to be used before express static middleware + * Adds trailing slash to request.url, in order to deceive static middleware + * into getting a directory request, thus preventing redirect. + * This is an express middleware to be used before static middleware * @param {*} req * @param {*} res * @param {*} next */ const directoryRedirectPrevention = (req, res, next) => { - if (req.url == `/` || req.url.indexOf(`.`) || req.url.slice(-1) !== `/`) - return next() - + // url is home page + if (req.url === `/`) return next() + // url has trailing slash, no need to intervene + if (req.url.slice(-1) === `/`) return next() + // url is a file request, no need for trailing slash + if (fileExtensionRegEx.test(req.url)) return next() + + // turning /slug into /slug/ req.originalUrl += `/` // https://expressjs.com/en/api.html#req.originalUrl req.url += `/`