Skip to content

Commit

Permalink
fix: avoid accessing non exist subApps
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Oct 12, 2020
1 parent fc9c1fb commit 7372642
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/subapp-web/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,23 @@ ${cdnJs}
${inlineRuntimeJS}
</script>`;

// check if any subapp has server side code with initialize method and load them
const { subApps } = setupContext.routeOptions.__internals;
const subAppServers = subApps
.map(({ subapp }) =>
subappUtil.loadSubAppServerByName(subapp.name, false)
)
.filter(x => x && x.initialize);
let subAppServers;

const getSubAppServers = () => {
if (subAppServers) {
return subAppServers;
}

// TODO: where and how is subApps set in __internals?
const { subApps } = setupContext.routeOptions.__internals;

// check if any subapp has server side code with initialize method and load them
return (subAppServers =
subApps &&
subApps
.map(({ subapp }) => subappUtil.loadSubAppServerByName(subapp.name, false))
.filter(x => x && x.initialize));
};

return {
process: context => {
Expand All @@ -84,9 +94,12 @@ ${inlineRuntimeJS}
if (metricReport.enable && metricReport.reporter) {
context.user.xarcSSREmitter = util.getEventEmiiter(metricReport.reporter);
}

getSubAppServers();

// invoke the initialize method of subapp's server code
if (subAppServers.length > 0) {
for (const server of subAppServers) {
if (subAppServers && subAppServers.length > 0) {
for (const server of getSubAppServers()) {
server.initialize(context);
}
}
Expand Down

0 comments on commit 7372642

Please sign in to comment.