diff --git a/packages/template-mrt-reference-app/app/ssr.js b/packages/template-mrt-reference-app/app/ssr.js index fcfa0a1a8a..5987f08e5d 100644 --- a/packages/template-mrt-reference-app/app/ssr.js +++ b/packages/template-mrt-reference-app/app/ssr.js @@ -356,12 +356,12 @@ const loggingMiddleware = (req, res, next) => { const envBasePathMiddleware = (req, res, next) => { const basePath = process.env.MRT_ENV_BASE_PATH - console.log(`Base path: ${basePath}`) - console.log(`Request path: ${req.url}`) - if (basePath && req.url.startsWith(basePath)) { - req.url = req.path.slice(basePath.length) || '/' - console.log( - `Base path: Rewrote ${basePath} -> Request path: ${req.originalUrl} -> New path: ${req.url}` + console.debug(`Base path: Base path: ${basePath}`) + console.debug(`Request path: Request path: ${req.url}`) + if (basePath && (req.path.startsWith(`${basePath}/`) || req.path === basePath)) { + req.url = req.url.slice(basePath.length) || '/' + console.debug( + `Base path: Rewrote ${basePath} -> Original url: ${req.originalUrl} -> New url: ${req.url}` ) } return next() diff --git a/packages/template-mrt-reference-app/app/ssr.test.js b/packages/template-mrt-reference-app/app/ssr.test.js index 3f48754b00..c56a8883ff 100644 --- a/packages/template-mrt-reference-app/app/ssr.test.js +++ b/packages/template-mrt-reference-app/app/ssr.test.js @@ -46,6 +46,20 @@ describe('server', () => { ]) } + test('Path /echo should work with base path', async () => { + const basePath = '/test-base-path' + process.env.MRT_ENV_BASE_PATH = basePath + const response = await request(app).get(`${basePath}/echo?x=foo&y=bar`) + expect(response.status).toBe(200) + // preserves query parameters + expect(response.body.query.x).toBe('foo') + expect(response.body.query.y).toBe('bar') + // path is the path after the base path + expect(response.body.path).toBe('/echo') + // base path env var present in response body + expect(response.body.env.MRT_ENV_BASE_PATH).toBe(basePath) + }) + beforeEach(() => { originalEnv = process.env process.env = Object.assign({}, process.env, {