-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wp-now: add
/wp-admin
redirection with trailing slash (#60)
Add a middleware to redirect to `/wp-admin` to add the trailing slash `/wp-admin/`. Fixes core sidebar links.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Adds redirection adding a trailing slash, when a request matches a given path. | ||
* @param path - The path to add a trailing slash to. E.g. '/wp-admin' | ||
* @returns - Returns a middleware function that may redirect adding a trailing slash to the given path. E.g. '/wp-admin/' | ||
*/ | ||
export function addTrailingSlash(path) { | ||
return (req, res, next) => { | ||
const urlParts = req.url.split('?'); | ||
const url = urlParts[0]; | ||
const queryString = req.url.substr(url.length); | ||
if (url === path) { | ||
res.redirect(301, `${path}/${queryString}`); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { addTrailingSlash } from '../add-trailing-slash'; | ||
|
||
describe('add trailing slash middleware', () => { | ||
const middlewareTrailingSlash = addTrailingSlash('/wp-admin'); | ||
let res, next; | ||
|
||
beforeEach(() => { | ||
res = { | ||
redirect: vi.fn(), | ||
}; | ||
next = vi.fn(); | ||
}); | ||
|
||
test('adds a trailing slash to the given path', () => { | ||
const req = { url: '/wp-admin' }; | ||
middlewareTrailingSlash(req, res, next); | ||
expect(res.redirect).toHaveBeenCalledWith(301, '/wp-admin/'); | ||
expect(next).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('adds a trailing slash to the given path with parameters', () => { | ||
const req = { url: '/wp-admin?foo=bar' }; | ||
middlewareTrailingSlash(req, res, next); | ||
expect(res.redirect).toHaveBeenCalledWith(301, '/wp-admin/?foo=bar'); | ||
expect(next).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('does not add a trailing slash to the given path', () => { | ||
const req = { url: '/wp-admin/' }; | ||
middlewareTrailingSlash(req, res, next); | ||
expect(res.redirect).not.toHaveBeenCalled(); | ||
expect(next).toHaveBeenCalled(); | ||
}); | ||
}); |