Skip to content

Commit

Permalink
fix: app offline problems (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored May 12, 2022
1 parent 10f88bb commit e2548ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions build/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ server {
try_files $uri /assets/$1 =404;
}

# Service Worker requests should not be changed
location ~* ^/ngsw {
try_files $uri =404;
}

location /de {
index index.html index.htm;
try_files $uri $uri/ /de/index.html;
Expand All @@ -29,9 +34,9 @@ server {
try_files $uri $uri/ /en-US/index.html;
}

# Set the default language when nothing is entered in the url
# Append the default language when no other path matches (e.g. requesting root index.html)
location / {
try_files $uri /${DEFAULT_LANGUAGE}/index.html;
rewrite ^/ /${DEFAULT_LANGUAGE}/$uri;
}

#error_page 404 /404.html;
Expand Down
6 changes: 5 additions & 1 deletion build/merge-service-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ locales.forEach((locale) => {
});

combined.index = "/index.html";
// add root index.html to correctly cache direct app entry (e.g. without en-US)
combined["assetGroups"][0]["urls"].push("/index.html");

fs.writeFileSync(`${distFolder}/ngsw.json`, JSON.stringify(combined));
fs.unlinkSync(`${distFolder}/${firstLocale}/ngsw.json`);
Expand All @@ -51,7 +53,9 @@ const swFile = fs
.toString();
const patchedSw = swFile.replace(
"return this.handleFetch(this.adapter.newRequest(this.indexUrl), context);",
"return this.handleFetch(this.adapter.newRequest('/' + this.adapter.normalizeUrl(req.url).split('/')[1] + '/index.html'), context);"
`const locale = this.adapter.normalizeUrl(req.url).split("/")[1];
const url = locale ? "/" + locale + "/index.html": "/index.html";
return this.handleFetch(this.adapter.newRequest(url), context);`
);
fs.writeFileSync(`${distFolder}/ngsw-worker.js`, patchedSw);
fs.unlinkSync(`${distFolder}/${firstLocale}/ngsw-worker.js`);
Expand Down

0 comments on commit e2548ed

Please sign in to comment.