Skip to content

Commit

Permalink
fix: repair service worker (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored Mar 7, 2022
1 parent 26ebdf2 commit 2a7a47d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ RUN if [ ! -z "${activeThemes}" ]; then npm config set intershop-pwa:active-them
RUN npm run build:multi client -- --deploy-url=DEPLOY_URL_PLACEHOLDER
COPY tsconfig.server.json server.ts /workspace/
RUN npm run build:multi server
# remove cache check for resources (especially index.html)
# https://github.com/angular/angular/issues/23613#issuecomment-415886919
RUN test "${serviceWorker}" = "true" && sed -i 's/canonicalHash !== cacheBustedHash/false/g' /workspace/dist/browser/ngsw-worker.js || true
RUN node scripts/compile-docker-scripts
COPY dist/* /workspace/dist/

Expand Down
2 changes: 1 addition & 1 deletion nginx/templates/multi-channel.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ server {
}

# respect cache entries of static assets
location ~* ^/(ngx_pagespeed_beacon|metrics|assets|.*\.(js|css|ico|json|txt|webmanifest|woff|woff2))(.*)$ {
location ~* ^/(?!ngsw\.json)(ngx_pagespeed_beacon|metrics|assets|.*\.(js|css|ico|json|txt|webmanifest|woff|woff2))(.*)$ {
allow all;
auth_basic off;

Expand Down
31 changes: 29 additions & 2 deletions scripts/build-pwa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
// https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json/51401577

const fs = require('fs');
const path = require('path');
const execSync = require('child_process').execSync;

/**
* remove service worker cache check for resources (especially index.html)
* https://github.com/angular/angular/issues/23613#issuecomment-415886919
*/
function removeServiceWorkerCacheCheck(args) {
const outputPathArg = args.find(arg => arg.startsWith('--output-path'));

let outputPath = '';
if (outputPathArg) {
// get outputPath from build args (in case of build:multi)
outputPath = outputPathArg.split('=')[1];
} else {
// get default outputPath from angular.json
const angularJson = JSON.parse(fs.readFileSync('./angular.json', { encoding: 'utf-8' }));
outputPath = angularJson.projects[angularJson.defaultProject].architect.build.options.outputPath;
}

const serviceWorkerScript = path.join(outputPath, 'ngsw-worker.js');
if (fs.existsSync(serviceWorkerScript)) {
console.warn('replacing cache check for service worker in', serviceWorkerScript);
const script = fs.readFileSync(serviceWorkerScript, { encoding: 'utf-8' });
fs.writeFileSync(serviceWorkerScript, script.replace('canonicalHash !== cacheBustedHash', 'false'));
}
}

// https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json/64694166#64694166
let configuration = process.env.npm_config_configuration;

if (configuration === 'true') {
Expand All @@ -24,6 +50,7 @@ if (client) {
execSync(`npm run ng -- build ${configString} ${remainingArgs.join(' ')}`, {
stdio: 'inherit',
});
removeServiceWorkerCacheCheck(remainingArgs);
}

if (server) {
Expand Down
14 changes: 13 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ export function app() {

// Serve static files from browser folder
server.get(/\/.*\.(js|css)$/, (req, res) => {
const path = req.originalUrl.substring(1);
// remove all parameters
const path = req.originalUrl.substring(1).replace(/[;?&].*$/, '');

fs.readFile(join(BROWSER_FOLDER, path), { encoding: 'utf-8' }, (err, data) => {
if (err) {
res.sendStatus(404);
Expand All @@ -198,6 +200,16 @@ export function app() {
}
});
});
server.get(/\/ngsw\.json/, (_, res) => {
fs.readFile(join(BROWSER_FOLDER, 'ngsw.json'), { encoding: 'utf-8' }, (err, data) => {
if (err) {
res.sendStatus(404);
} else {
res.set('Content-Type', 'application/json; charset=UTF-8');
res.send(data);
}
});
});
server.get(
'*.*',
express.static(BROWSER_FOLDER, {
Expand Down
2 changes: 1 addition & 1 deletion src/ssr/server-scripts/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app.use((req, res, next) => {

if (!match) {
match = Object.keys(ports).find(config => {
const p = path.join(process.cwd(), 'dist', config, 'browser', req.path);
const p = path.join(process.cwd(), 'dist', config, 'browser', req.path.substring(1).replace(/[;?&].*$/, ''));
return fs.existsSync(p);
});
}
Expand Down

0 comments on commit 2a7a47d

Please sign in to comment.