@salesforce/pwa-kit-runtime 3.17.1-preview.0 → 3.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salesforce/pwa-kit-runtime",
|
|
3
|
-
"version": "3.17.1
|
|
3
|
+
"version": "3.17.1",
|
|
4
4
|
"description": "The PWAKit Runtime",
|
|
5
5
|
"homepage": "https://github.com/SalesforceCommerceCloud/pwa-kit/tree/develop/packages/pwa-kit-runtime#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -48,11 +48,11 @@
|
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@loadable/component": "^5.15.3",
|
|
51
|
-
"@salesforce/pwa-kit-dev": "3.17.1
|
|
51
|
+
"@salesforce/pwa-kit-dev": "3.17.1",
|
|
52
52
|
"@serverless/event-mocks": "^1.1.1",
|
|
53
53
|
"aws-lambda-mock-context": "^3.2.1",
|
|
54
54
|
"fs-extra": "^11.1.1",
|
|
55
|
-
"internal-lib-build": "3.17.1
|
|
55
|
+
"internal-lib-build": "3.17.1",
|
|
56
56
|
"nock": "^13.3.0",
|
|
57
57
|
"nodemon": "^2.0.22",
|
|
58
58
|
"sinon": "^13.0.2",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"supertest": "^4.0.2"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"@salesforce/pwa-kit-dev": "3.17.1
|
|
63
|
+
"@salesforce/pwa-kit-dev": "3.17.1"
|
|
64
64
|
},
|
|
65
65
|
"peerDependenciesMeta": {
|
|
66
66
|
"@salesforce/pwa-kit-dev": {
|
|
@@ -74,5 +74,5 @@
|
|
|
74
74
|
"publishConfig": {
|
|
75
75
|
"directory": "dist"
|
|
76
76
|
},
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "d09ed57f84432913ab3e55e3073802d319c5dc3c"
|
|
78
78
|
}
|
|
@@ -27,6 +27,7 @@ var _loggerInstance = _interopRequireDefault(require("../../utils/logger-instanc
|
|
|
27
27
|
var _httpProxyMiddleware = require("http-proxy-middleware");
|
|
28
28
|
var _hybridProxy = require("../../utils/ssr-server/hybrid-proxy");
|
|
29
29
|
var _convertExpressRoute = require("../../utils/ssr-server/convert-express-route");
|
|
30
|
+
var _ssrConfig = require("../../utils/ssr-config");
|
|
30
31
|
var _serverlessAdapter = require("@h4ad/serverless-adapter");
|
|
31
32
|
var _default = require("@h4ad/serverless-adapter/lib/handlers/default");
|
|
32
33
|
var _callback = require("@h4ad/serverless-adapter/lib/resolvers/callback");
|
|
@@ -445,6 +446,7 @@ const RemoteServerFactory = exports.RemoteServerFactory = {
|
|
|
445
446
|
* @private
|
|
446
447
|
*/
|
|
447
448
|
_setupBasePathMiddleware(app) {
|
|
449
|
+
var _getConfig, _getConfig$app, _getConfig$app$url;
|
|
448
450
|
// Cache the express route regexes to avoid re-calculating them on every request.
|
|
449
451
|
let expressRouteRegexes;
|
|
450
452
|
|
|
@@ -490,11 +492,19 @@ const RemoteServerFactory = exports.RemoteServerFactory = {
|
|
|
490
492
|
* If the server receives a request containing the base path, remove it before allowing
|
|
491
493
|
* the request through to the other express endpoints
|
|
492
494
|
*
|
|
493
|
-
* We scope base path removal to /mobify routes
|
|
494
|
-
* (For example /callback or /worker.js)
|
|
495
|
+
* We scope base path removal to /mobify routes, /__pwa-kit routes (when showBasePath
|
|
496
|
+
* is false), and routes defined by the express app (For example /callback or /worker.js).
|
|
495
497
|
* This is to avoid affecting React Router routes where a site id or locale might be present
|
|
496
498
|
* that is equal to the base path.
|
|
497
499
|
*
|
|
500
|
+
* /__pwa-kit routes are a special case. These are internal PWA Kit routes
|
|
501
|
+
* (e.g. /__pwa-kit/refresh) that are registered as React Router routes and are invoked
|
|
502
|
+
* by the Storefront Preview code on Runtime Admin (which always appends a base path if set).
|
|
503
|
+
* When showBasePath is false, React Router has no basename, so we redirect to the clean
|
|
504
|
+
* URL (without base path) so the browser navigates to a path that React Router can match
|
|
505
|
+
* and hydrate correctly on the client.
|
|
506
|
+
* When showBasePath is true, React Router handles the base path via its basename prop.
|
|
507
|
+
*
|
|
498
508
|
* For example, if you have a base path of /us and a site id of /us we don't want
|
|
499
509
|
* to remove the /us from www.example.com/us/en-US/category/... as this route is handled by
|
|
500
510
|
* React Router and the PWA multisite implementation.
|
|
@@ -502,6 +512,7 @@ const RemoteServerFactory = exports.RemoteServerFactory = {
|
|
|
502
512
|
* @param req {express.req} the incoming request - modified in-place
|
|
503
513
|
* @private
|
|
504
514
|
*/
|
|
515
|
+
const showBasePath = ((_getConfig = (0, _ssrConfig.getConfig)()) === null || _getConfig === void 0 ? void 0 : (_getConfig$app = _getConfig.app) === null || _getConfig$app === void 0 ? void 0 : (_getConfig$app$url = _getConfig$app.url) === null || _getConfig$app$url === void 0 ? void 0 : _getConfig$app$url.showBasePath) === true;
|
|
505
516
|
const removeBasePathMiddleware = (req, res, next) => {
|
|
506
517
|
const basePath = (0, _ssrNamespacePaths.getEnvBasePath)();
|
|
507
518
|
|
|
@@ -515,6 +526,17 @@ const RemoteServerFactory = exports.RemoteServerFactory = {
|
|
|
515
526
|
return next();
|
|
516
527
|
}
|
|
517
528
|
|
|
529
|
+
// /__pwa-kit routes: when showBasePath is false, the browser URL still has the
|
|
530
|
+
// base path but client-side React Router has no basename, so hydration would fail.
|
|
531
|
+
// Redirect to the clean URL so the browser navigates to a path React Router matches.
|
|
532
|
+
if (!showBasePath && req.path.startsWith(`${basePath}/__pwa-kit`)) {
|
|
533
|
+
const cleanPath = removeBasePathFromPath(req.path);
|
|
534
|
+
const {
|
|
535
|
+
search
|
|
536
|
+
} = (0, _ssrServer.parseRequestUrl)(req);
|
|
537
|
+
return res.redirect(302, cleanPath + search);
|
|
538
|
+
}
|
|
539
|
+
|
|
518
540
|
// For other routes, only proceed if path equals basePath or path starts with basePath + '/'
|
|
519
541
|
const pathMatchesBasePath = req.path === basePath || req.path.startsWith(basePath + '/');
|
|
520
542
|
if (!pathMatchesBasePath) {
|
|
@@ -1192,6 +1192,11 @@ describe('SLAS private client proxy', () => {
|
|
|
1192
1192
|
}));
|
|
1193
1193
|
});
|
|
1194
1194
|
describe('Base path tests', () => {
|
|
1195
|
+
beforeEach(() => {
|
|
1196
|
+
// Re-establish the getConfig mock that afterEach's restoreAllMocks clears.
|
|
1197
|
+
// _setupBasePathMiddleware reads getConfig() at setup time.
|
|
1198
|
+
jest.spyOn(ssrConfig, 'getConfig').mockReturnValue({});
|
|
1199
|
+
});
|
|
1195
1200
|
afterEach(() => {
|
|
1196
1201
|
jest.restoreAllMocks();
|
|
1197
1202
|
});
|
|
@@ -1268,4 +1273,39 @@ describe('Base path tests', () => {
|
|
|
1268
1273
|
expect(response.body.message).toBe('test');
|
|
1269
1274
|
});
|
|
1270
1275
|
}), 15000);
|
|
1276
|
+
test('should redirect /__pwa-kit routes to clean URL when showBasePath is false', /*#__PURE__*/_asyncToGenerator(function* () {
|
|
1277
|
+
jest.spyOn(ssrNamespacePaths, 'getEnvBasePath').mockReturnValue('/basepath');
|
|
1278
|
+
jest.spyOn(ssrConfig, 'getConfig').mockReturnValue({
|
|
1279
|
+
app: {
|
|
1280
|
+
url: {
|
|
1281
|
+
showBasePath: false
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
});
|
|
1285
|
+
const app = _buildRemoteServer.RemoteServerFactory._createApp(opts());
|
|
1286
|
+
return (0, _supertest.default)(app).get('/basepath/__pwa-kit/refresh?referrer=/some-page').expect(302).then(response => {
|
|
1287
|
+
// Should redirect to the clean URL without base path
|
|
1288
|
+
expect(response.headers.location).toBe('/__pwa-kit/refresh?referrer=/some-page');
|
|
1289
|
+
});
|
|
1290
|
+
}), 15000);
|
|
1291
|
+
test('should not remove base path from /__pwa-kit routes when showBasePath is true', /*#__PURE__*/_asyncToGenerator(function* () {
|
|
1292
|
+
jest.spyOn(ssrNamespacePaths, 'getEnvBasePath').mockReturnValue('/basepath');
|
|
1293
|
+
jest.spyOn(ssrConfig, 'getConfig').mockReturnValue({
|
|
1294
|
+
app: {
|
|
1295
|
+
url: {
|
|
1296
|
+
showBasePath: true
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
const app = _buildRemoteServer.RemoteServerFactory._createApp(opts());
|
|
1301
|
+
let capturedPath = null;
|
|
1302
|
+
app.use((req, res, next) => {
|
|
1303
|
+
capturedPath = req.path;
|
|
1304
|
+
next();
|
|
1305
|
+
});
|
|
1306
|
+
return (0, _supertest.default)(app).get('/basepath/__pwa-kit/refresh').then(() => {
|
|
1307
|
+
// Base path should NOT be stripped since React Router handles it via basename
|
|
1308
|
+
expect(capturedPath).toBe('/basepath/__pwa-kit/refresh');
|
|
1309
|
+
});
|
|
1310
|
+
}), 15000);
|
|
1271
1311
|
});
|