Skip to content

Commit

Permalink
Prerender query params preserve (#1)
Browse files Browse the repository at this point in the history
Preserve query params in prerender proxy pass.
  • Loading branch information
smgladkovskiy authored May 21, 2020
1 parent 75b75da commit b32cbac
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**PERMANENTLY MOVED TO** https://github.com/spacetab-io/static-server-php


Static server
-------------

Expand Down
10 changes: 6 additions & 4 deletions configuration/defaults/___server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ defaults:
options:
pid: ~/.server/spa_nginx.pid
config: ~/.server/generated_nginx.conf
prerender:
prerender: # https://github.com/spacetab-io/prerender-go
enabled: false
cacheTtl: 24h # by default cache lives 24 hours
cdnUrl: null
cdnPath: / # CDN folder relative to root path (/)
cacheTtl: 1h # by default cache lives 1 hour
queryParams: [] # params that will be passed to CDN to find files. Params are sorted alphabetically.
cdnUrl: null # CDN url
cdnPath: / # CDN folder relative to root path with trailing slash (/)
url: null # Proxy pass URL for bots, with schema.
resolver: "8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1" # nginx "resolver" option to force DNS resolution and prevent caching of IPs
headers: [] # Authorization headers (or others), format: key - $headerName, value - $headerValue
log_info: "%%cSTAGE=%s SHA1=%s; %%cSecurity bugs: [email protected], Job/partnership: [email protected]"
logger:
enabled: true
level: info # debug, info, notice, warning, critical, emergency
Expand Down
15 changes: 15 additions & 0 deletions src/Handler/NginxHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function generateConfig(HeaderInterface $header): void
'serverHost' => $this->configuration->get('server.host'),
'prerenderEnabled' => $this->configuration->get('server.prerender.enabled'),
'prerenderCacheTTL' => $this->configuration->get('server.prerender.cacheTtl'),
'prerenderQueryParams' => $this->getSorted('server.prerender.queryParams'),
'CDNUrl' => $this->getParamWithoutTrailingSlash('server.prerender.cdnUrl'),
'CDNPath' => $this->getParamWithoutTrailingSlash('server.prerender.cdnPath'),
'prerenderHeaders' => $this->configuration->get('server.prerender.headers', []),
Expand Down Expand Up @@ -157,6 +158,20 @@ private function getParamWithoutTrailingSlash(string $key)
return rtrim($this->configuration->get($key), '/');
}

/**
* @param string $key
* @param array<mixed> $default
*
* @return bool|string
*/
private function getSorted(string $key, array $default = [])
{
$array = $this->configuration->get($key, $default);
sort($array);

return $array;
}

/**
* @codeCoverageIgnore
* @return string
Expand Down
22 changes: 15 additions & 7 deletions src/Handler/templates/nginx_default.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,28 +289,36 @@
set $prerender 0;
}

<?php if ($prerenderResolver):?>
# resolve using Google's DNS/Cloudflare server to force DNS resolution and prevent caching of IPs
resolver <?=$prerenderResolver?>;
<?php endif;?>
<?php if ($prerenderResolver):?>
# resolve using Google's DNS/Cloudflare server to force DNS resolution and prevent caching of IPs
resolver <?=$prerenderResolver?>;
<?php endif;?>

# need to redefine $request_uri if it will be "/" but $request_uri is blocked for changes
set $req_uri $uri;
if ($req_uri = /) {
set $req_uri "/index";
}

<?php if(count($prerenderQueryParams)):?>
set $q "";
<?php foreach($prerenderQueryParams as $key):?>
if ($args ~ "<?php echo $key?>=([^&]*)(?=&)*") {
set $q "$q-<?php echo $key?>=$1";
}
<?php endforeach;?>
<?php endif;?>
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
rewrite .* <?=$CDNPath?>$req_uri.html?$query_string break;
rewrite .* <?=$CDNPath?>$req_uri$q break;
proxy_pass "<?=$CDNUrl?>";
break;
}

if ($prerender = 0) {
<?php foreach($headers as $name => $value):?>
<?php foreach($headers as $name => $value):?>
add_header "<?=$name?>" "<?=addslashes($value)?>";
<?php endforeach;?>
<?php endforeach;?>
expires 30m;
add_header "Cache-Control" "public, max-age=1800";

Expand Down

0 comments on commit b32cbac

Please sign in to comment.