Skip to content

Commit

Permalink
REFACTOR: Use more generic approach for ignoring specific user agents
Browse files Browse the repository at this point in the history
  • Loading branch information
mattclegg committed May 2, 2020
1 parent 8e4f457 commit 7d3b887
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
32 changes: 16 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"name": "axllent/silverstripe-trailing-slash",
"description": "Ensure that a single trailing slash is always added to the URL",
"type": "silverstripe-vendormodule",
"homepage": "https://github.com/axllent/silverstripe-trailing-slash",
"keywords": ["silverstripe", "urls", "seo"],
"license": "MIT",
"authors": [
{
"name": "Ralph Slooten",
"homepage": "http://www.axllent.org/"
}
],
"support": {
"issues": "https://github.com/axllent/silverstripe-trailing-slash/issues"
},
"description": "Ensure that a single trailing slash is always added to the URL",
"type": "silverstripe-vendormodule",
"homepage": "https://github.com/axllent/silverstripe-trailing-slash",
"keywords": ["silverstripe", "urls", "seo"],
"license": "MIT",
"authors": [
{
"name": "Ralph Slooten",
"homepage": "http://www.axllent.org/"
}
],
"support": {
"issues": "https://github.com/axllent/silverstripe-trailing-slash/issues"
},
"require": {
"silverstripe/framework": "^4.0"
},
"silverstripe/framework": "^4.0"
},
"autoload": {
"psr-4": {
"Axllent\\TrailingSlash\\": "src/"
Expand Down
15 changes: 15 additions & 0 deletions docs/en/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@
```
composer require axllent/silverstripe-trailing-slash
```


## Caveats

When using the silverstripe/silverstripe-staticpublishqueue module the REQUEST_URI is updated so what's used for the request will be different from what is defined in $_SERVER['REQUEST_URI']. As the REQUEST_URI can no longer be used to determine if a trailing slash is required then it makes more sense to ignore the user agent for staticpublishqueue.

The User agent can be ignored by this module by setting;

```yaml
Axllent\TrailingSlash\Middleware\TrailingSlashRedirector:
ignore_agents:
- 'silverstripe/staticpublishqueue'
```
ref; https://github.com/silverstripe/silverstripe-staticpublishqueue/blob/master/src/Publisher.php#L82-L101
14 changes: 13 additions & 1 deletion src/Middleware/TrailingSlashRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class TrailingSlashRedirector implements HTTPMiddleware
'dev/',
];

/**
* User-Agents to ignore
*
* @var array
*/
private static $ignore_agents = [];

/**
* Process request
*
Expand Down Expand Up @@ -67,8 +74,13 @@ public function process(HTTPRequest $request, callable $delegate)

$params = $request->getVars();

$ignore_agents = Config::inst()->get(TrailingSlashRedirector::class, 'ignore_agents');

if (!Director::is_ajax()
&& $request->getHeader('User-Agent') != 'silverstripe/staticpublishqueue'
&& !($ignore_agents && in_array(
$request->getHeader('User-Agent'),
$ignore_agents
))
&& !isset($urlPathInfo['extension'])
&& empty($params)
&& !preg_match(
Expand Down

0 comments on commit 7d3b887

Please sign in to comment.