Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR to use more generic approach for ignoring specific user agents #12

Merged
merged 1 commit into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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