-
Notifications
You must be signed in to change notification settings - Fork 395
Incorrect server request url on server side, when deployed on ccv2 #11016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Blocks #10903 |
We are working on a hotfix that will be included in the next patch release.
import { StaticProvider } from '@angular/core';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { Request } from 'express';
import { SERVER_REQUEST_ORIGIN, SERVER_REQUEST_URL } from '@spartacus/core';
export const fixServerRequestProviders: StaticProvider[] = [
{
provide: SERVER_REQUEST_URL,
useFactory: getRequestUrl,
deps: [REQUEST],
},
{
provide: SERVER_REQUEST_ORIGIN,
useFactory: getRequestOrigin,
deps: [REQUEST],
},
];
function getRequestUrl(req: Request): string {
return getRequestOrigin(req) + req.originalUrl;
}
function getRequestOrigin(req: Request): string {
// If express is resolving and trusting X-Forwarded-Host, we want to take it
// into an account to properly generate request origin.
const trustProxyFn = req.app.get('trust proxy fn');
let forwardedHost = req.get('X-Forwarded-Host');
if (forwardedHost && trustProxyFn(req.connection.remoteAddress, 0)) {
if (forwardedHost.indexOf(',') !== -1) {
// Note: X-Forwarded-Host is normally only ever a
// single value, but this is to be safe.
forwardedHost = forwardedHost
.substring(0, forwardedHost.indexOf(','))
.trimRight();
}
return req.protocol + '://' + forwardedHost;
} else {
return req.protocol + '://' + req.get('host');
}
}
(below |
Edited above workaround. Changed
The SERVER_REQUEST_ORIGIN and SERVER_REQUEST_URL should not be provided in CSR by design. |
Edited the above workaround with regards to the latest bugfix 37e8926 |
This mostly affects Automatic Multi-Site Configuration that in many cases won't be able to resolve proper base site.
How to reproduce:
app.module.ts
The text was updated successfully, but these errors were encountered: