Skip to content

Commit

Permalink
fix: handle ICM static files with the ICM proxy configuration (#1105)
Browse files Browse the repository at this point in the history
* required for hybrid approach otherwise SSR will return 404 for CSS and JS files
  • Loading branch information
shauke committed Apr 29, 2022
1 parent 7e24ba1 commit 29be2c1
Showing 1 changed file with 78 additions and 78 deletions.
156 changes: 78 additions & 78 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,84 @@ export function app() {
);
}

const hybridRedirect = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const url = req.originalUrl;
let newUrl: string;
for (const entry of HYBRID_MAPPING_TABLE) {
const icmUrlRegex = new RegExp(entry.icm);
const pwaUrlRegex = new RegExp(entry.pwa);
if (icmUrlRegex.exec(url) && entry.handledBy === 'pwa') {
newUrl = url.replace(icmUrlRegex, `/${entry.pwaBuild}`);
break;
} else if (pwaUrlRegex.exec(url) && entry.handledBy === 'icm') {
const config: { [is: string]: string } = {};
if (/;lang=[\w_]+/.test(url)) {
const [, lang] = /;lang=([\w_]+)/.exec(url);
config.lang = lang;
}
if (!config.lang) {
config.lang = environment.defaultLocale;
}

if (/;currency=[\w_]+/.test(url)) {
const [, currency] = /;currency=([\w_]+)/.exec(url);
config.currency = currency;
}

if (/;channel=[^;]*/.test(url)) {
config.channel = /;channel=([^;]*)/.exec(url)[1];
} else {
config.channel = environment.icmChannel;
}

if (/;application=[^;]*/.test(url)) {
config.application = /;application=([^;]*)/.exec(url)[1];
} else {
config.application = environment.icmApplication || '-';
}

const build = [ICM_WEB_URL, entry.icmBuild]
.join('/')
.replace(/\$<(\w+)>/g, (match, group) => config[group] || match);
newUrl = url.replace(pwaUrlRegex, build).replace(/;.*/g, '');
break;
}
}
if (newUrl) {
if (logging) {
console.log('RED', newUrl);
}
res.redirect(301, newUrl);
} else {
next();
}
};

if (process.env.SSR_HYBRID) {
server.use('*', hybridRedirect);
}

const icmProxy = proxy(ICM_BASE_URL, {
// preserve original path
proxyReqPathResolver: (req: express.Request) => req.originalUrl,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
proxyReqOptDecorator: (options: any) => {
if (process.env.TRUST_ICM) {
// https://github.com/villadora/express-http-proxy#q-how-to-ignore-self-signed-certificates-
options.rejectUnauthorized = false;
}
return options;
},
// fool ICM so it thinks it's running here
// https://www.npmjs.com/package/express-http-proxy#preservehosthdr
preserveHostHdr: true,
});

if (process.env.PROXY_ICM || process.env.SSR_HYBRID) {
console.log("making ICM available for all requests to '/INTERSHOP'");
server.use('/INTERSHOP', icmProxy);
}

// Serve static files from browser folder
server.get(/\/.*\.(js|css)$/, (req, res) => {
// remove all parameters
Expand Down Expand Up @@ -232,22 +310,6 @@ export function app() {
})
);

const icmProxy = proxy(ICM_BASE_URL, {
// preserve original path
proxyReqPathResolver: (req: express.Request) => req.originalUrl,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
proxyReqOptDecorator: (options: any) => {
if (process.env.TRUST_ICM) {
// https://github.com/villadora/express-http-proxy#q-how-to-ignore-self-signed-certificates-
options.rejectUnauthorized = false;
}
return options;
},
// fool ICM so it thinks it's running here
// https://www.npmjs.com/package/express-http-proxy#preservehosthdr
preserveHostHdr: true,
});

const angularUniversal = (req: express.Request, res: express.Response) => {
if (logging) {
console.log(`SSR ${req.originalUrl}`);
Expand Down Expand Up @@ -311,68 +373,6 @@ export function app() {
);
};

const hybridRedirect = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const url = req.originalUrl;
let newUrl: string;
for (const entry of HYBRID_MAPPING_TABLE) {
const icmUrlRegex = new RegExp(entry.icm);
const pwaUrlRegex = new RegExp(entry.pwa);
if (icmUrlRegex.exec(url) && entry.handledBy === 'pwa') {
newUrl = url.replace(icmUrlRegex, `/${entry.pwaBuild}`);
break;
} else if (pwaUrlRegex.exec(url) && entry.handledBy === 'icm') {
const config: { [is: string]: string } = {};
if (/;lang=[\w_]+/.test(url)) {
const [, lang] = /;lang=([\w_]+)/.exec(url);
config.lang = lang;
}
if (!config.lang) {
config.lang = environment.defaultLocale;
}

if (/;currency=[\w_]+/.test(url)) {
const [, currency] = /;currency=([\w_]+)/.exec(url);
config.currency = currency;
}

if (/;channel=[^;]*/.test(url)) {
config.channel = /;channel=([^;]*)/.exec(url)[1];
} else {
config.channel = environment.icmChannel;
}

if (/;application=[^;]*/.test(url)) {
config.application = /;application=([^;]*)/.exec(url)[1];
} else {
config.application = environment.icmApplication || '-';
}

const build = [ICM_WEB_URL, entry.icmBuild]
.join('/')
.replace(/\$<(\w+)>/g, (match, group) => config[group] || match);
newUrl = url.replace(pwaUrlRegex, build).replace(/;.*/g, '');
break;
}
}
if (newUrl) {
if (logging) {
console.log('RED', newUrl);
}
res.redirect(301, newUrl);
} else {
next();
}
};

if (process.env.SSR_HYBRID) {
server.use('*', hybridRedirect);
}

if (process.env.PROXY_ICM || process.env.SSR_HYBRID) {
console.log("making ICM available for all requests to '/INTERSHOP'");
server.use('/INTERSHOP', icmProxy);
}

if (/^(on|1|true|yes)$/i.test(process.env.PROMETHEUS)) {
const axios = require('axios');
const onFinished = require('on-finished');
Expand Down

0 comments on commit 29be2c1

Please sign in to comment.