Skip to content

Commit

Permalink
PWA bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SindlaXYZ committed Jan 5, 2021
1 parent 6249921 commit 5412e58
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
26 changes: 9 additions & 17 deletions src/Utils/PWA/PWA.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public function browserConfig(Request $Request)
public function mainJS(Request $Request)
{
$rendered = $this->twig->render('@Aurora/pwa-main.js.twig', [
'build' => $this->_build($Request),
'pwaVersion' => $this->version(),
'automatically_prompt' => ($this->container->hasParameter('aurora.pwa.automatically_prompt') ? boolval($this->container->getParameter('aurora.pwa.automatically_prompt')) : true)
]);
Expand All @@ -164,11 +163,11 @@ public function mainJS(Request $Request)
public function serviceWorkerJS(Request $Request)
{
$rendered = $this->twig->render('@Aurora/pwa-sw.js.twig', [
'pwaVersion' => $this->version(),
'precache' => "'" . implode("', '", array_unique(array_merge([$this->container->getParameter('aurora.pwa.start_url'), $this->container->getParameter('aurora.pwa.offline')], $this->container->getParameter('aurora.pwa.precache')))) . "'",
'prevent_cache' => "'" . implode("', '", $this->container->getParameter('aurora.pwa.prevent_cache')) . "'",
'external_cache' => "/" . implode("/, /", $this->container->getParameter('aurora.pwa.external_cache')) . "/",
'offline' => $this->container->getParameter('aurora.pwa.offline'),
'build' => $this->_build($Request)
'offline' => $this->container->getParameter('aurora.pwa.offline')
]);

// Minify if not DEV
Expand All @@ -184,16 +183,21 @@ public function serviceWorkerJS(Request $Request)
return $response;
}

public function version()
public function version(Request $Request)
{
$serviceGit = $this->container->get('aurora.git');
$version = $serviceGit->getHash();
$versionAppend = $this->container->getParameter('aurora.pwa.version_append');


if ($Request->cookies->get('PHPSESSID')) {
$version .= '_' . $this->session->get('PHPSESSID');
}

if (0 === strpos($versionAppend, '!php/eval')) {
preg_match('/`(.*)`/', $versionAppend, $match);
if (isset($match[1]) && !empty($match[1])) {
$version .= '-' . substr(sha1(eval("return " . trim($match[1], ';') . ";")), 0, 15);
$version .= '_' . substr(sha1(eval("return " . trim($match[1], ';') . ";")), 0, 15);
}
}

Expand Down Expand Up @@ -248,16 +252,4 @@ private function _icon(string $iconPath)
$Response->headers->set('X-Backend-Hit', true);
return $Response;
}

/**
* PWA build version based on last git commit hash and PHPSESSID
*
* @param Request $Request
* @return string
*/
private function _build(Request $Request)
{
$serviceGit = $this->container->get('aurora.git');
return sha1(($serviceGit->getHash() . ($Request->cookies->get('PHPSESSID') ?? $this->session->get('PHPSESSID'))));
}
}
4 changes: 2 additions & 2 deletions src/templates/pwa-main.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

if ('serviceWorker' in navigator) {
var serviceWorkerPath = document.currentScript.getAttribute('service-worker');
var serviceWorkerVersion = '{{ pwaVersion }}'; {# document.currentScript.getAttribute('service-worker-version')#};
var serviceWorkerVersion = '{{ pwaVersion }}'; {# document.currentScript.getAttribute('service-worker-version')#}
var serviceWorkerDebug = document.currentScript.getAttribute('service-worker-debug');
let deferredPrompt;

{% if _debug %}
console.log('Load SW '+ serviceWorkerPath + ', version (build) ' + serviceWorkerVersion);
console.log('Load SW '+ serviceWorkerPath + ', version ' + serviceWorkerVersion);
{% endif %}

{# if false, then do not automatically show the "Install" prompt #}
Expand Down

0 comments on commit 5412e58

Please sign in to comment.