Skip to content
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

Refactoring to improve performance #1717

Merged
merged 3 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/subapp-util/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function loadSubAppByName(name) {
return container[name];
}

function loadSubAppServerByName(name) {
function loadSubAppServerByName(name, serverSideRendering) {
const manifest = subAppManifest()[name];
const { subAppDir, serverEntry } = manifest;

Expand All @@ -295,16 +295,16 @@ function loadSubAppServerByName(name) {
);
} else if (serverEntry === false) {
return {};
}

// generate a server from subapp's main file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified to generate server from subapps main file only if ssr is enabled

const subapp = es6Require(
manifest.module ? manifest.entry : Path.resolve(appSrcDir(), subAppDir, manifest.entry)
);
} else if (serverSideRendering) {
const subapp = es6Require(
manifest.module ? manifest.entry : Path.resolve(appSrcDir(), subAppDir, manifest.entry)
);

return {
StartComponent: subapp.Component
};
return {
StartComponent: subapp.Component
};
}
return {};
}

function refreshSubAppByName(name) {
Expand Down
3 changes: 3 additions & 0 deletions packages/subapp-util/test/data/subapp4/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
foo: () => 123
};
4 changes: 4 additions & 0 deletions packages/subapp-util/test/data/subapp4/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
name: "subapp1-server",
listen: (port = 8080) => `server is listening on port: ${port}`
};
6 changes: 6 additions & 0 deletions packages/subapp-util/test/data/subapp4/src/subapp-conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
name: "Subapp4",
subAppDir: "Subapp4/src",
entry: "index.js",
serverEntry: false
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
name: "subapp4",
subAppDir: "subapp4/src",
entry: "index.js",
serverEntry: false
};
8 changes: 4 additions & 4 deletions packages/subapp-util/test/spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe("subapp-util", function() {
process.env.APP_SRC_DIR = "test/data";
const subapp = getAllSubAppManifest();
expect(subapp).to.exist;
expect(Object.keys(subapp).length).to.equal(4);
expect(Object.keys(subapp).length).to.equal(5);
});
});

Expand Down Expand Up @@ -209,13 +209,13 @@ describe("subapp-util", function() {
});

it("should generate `serverEntry` given in subapp", () => {
const server = loadSubAppServerByName("Entry");
const server = loadSubAppServerByName("Entry", true);
expect(server).to.have.property("StartComponent");
});

it("should return empty `serverEntry` if subapp manifest sets serverEntry to false", () => {
const server = loadSubAppServerByName("Entry");
expect(server).to.have.property("StartComponent");
const server = loadSubAppServerByName("subapp4");
expect(server).to.not.have.property("StartComponent");
});

it("should not load subapp server by name if NODE_ENV = production but lib does not exist", () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/subapp-web/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ ${inlineRuntimeJS}
</script>`;

// check if any subapp has server side code with initialize method and load them
const subAppServers = Object.keys(subappUtil.getAllSubAppManifest())
Copy link
Contributor Author

@divyakarippath divyakarippath Aug 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run this only for subapps for that specific route. In subapp-server, we set the subapps for each route to setupContext.routeOptions.__internals

.map(name => subappUtil.loadSubAppServerByName(name))
const { subApps } = setupContext.routeOptions.__internals;
const subAppServers = subApps
.map(({ subapp }) => {
subappUtil.loadSubAppServerByName(subapp.name, false);
})
.filter(x => x && x.initialize);

return {
Expand Down
32 changes: 17 additions & 15 deletions packages/subapp-web/lib/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Response: ${err || body}`
//
const prepareSubAppSplitBundles = async context => {
const { assets, includedBundles } = context.user;
const entryName = subApp.name.toLowerCase();
const entryName = name.toLowerCase();
//
const entryPoints = assets.entryPoints[entryName];
const cdnJsBundles = util.getCdnJsBundles(assets, setupContext.routeOptions);
Expand Down Expand Up @@ -193,10 +193,9 @@ Response: ${err || body}`

const loadSubApp = () => {
subApp = loadSubAppByName(name);
subAppServer = loadSubAppServerByName(name);
subAppServer = loadSubAppServerByName(name, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we will call loadSubapp only if ssr is enabled. Setting second argument as true so that loadSubAppServerByName will generate server from subapp's main file if serverEntry is not there.

};

loadSubApp();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need this only if ssr is enabled

prepareSubAppJsBundle();

const verifyUseStream = props => {
Expand All @@ -221,10 +220,6 @@ Response: ${err || body}`

context.user.numOfSubapps = context.user.numOfSubapps || 0;

if (request.app.webpackDev && subAppLoadTime < request.app.webpackDev.compileTime) {
Copy link
Contributor Author

@divyakarippath divyakarippath Aug 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needto call loadSubApp only if ssr is enabled. So moved it to line 356

subAppLoadTime = request.app.webpackDev.compileTime;
loadSubApp();
}

let { group = "_" } = props;
group = [].concat(group);
Expand Down Expand Up @@ -337,14 +332,6 @@ ${stack}`,
};

const processSubapp = async () => {
const ref = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need this only for ssr flow. So moved it to line 358

context,
subApp,
subAppServer,
options: props,
ssrGroups
};

context.user.numOfSubapps++;
const { bundles, scripts, preLoads } = await prepareSubAppSplitBundles(context);
outputSpot.add(`${comment}`);
Expand All @@ -363,6 +350,21 @@ ${stack}`,
if (!context.user[`prepare-grp-${props.group}`]) {
context.user[`prepare-grp-${props.group}`] = Date.now();
}

if (
!request.app.webpackDev ||
(request.app.webpackDev && subAppLoadTime < request.app.webpackDev.compileTime)
) {
subAppLoadTime = _.get(request, "app.webpackDev.compileTime", 0);
loadSubApp();
}
const ref = {
context,
subApp,
subAppServer,
options: props,
ssrGroups
};
const lib = (ssrInfo.lib = util.getFramework(ref));
ssrInfo.awaitData = lib.handlePrepare();

Expand Down
20 changes: 18 additions & 2 deletions packages/subapp-web/test/spec/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ describe("init", function () {

const initToken = init({
routeOptions: {
__internals: {},
__internals: {
subApps: [
{
subapp: {
name: "mainbody"
}
}
]
},
cdn: {},
stats: Path.join(__dirname, "../data/prod-stats.json")
}
Expand All @@ -42,7 +50,15 @@ describe("init", function () {

const initToken = init({
routeOptions: {
__internals: {},
__internals: {
subApps: [
{
subapp: {
name: "mainbody"
}
}
]
},
cdn: {},
prodBundleBase: "/js",
stats: Path.join(__dirname, "../data/prod-stats.json")
Expand Down