Skip to content

Commit

Permalink
fix: subappV1 handle each route with its own template instance (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Jul 19, 2021
1 parent df08422 commit 7da59d8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/subapp-server/lib/template-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ const otherStats = getOtherStats();
/*eslint-disable max-statements*/
function initializeTemplate(
{ templateFile, tokenHandlers, cacheId, cacheKey, options },
routeOptions
routeOptions,
request
) {
cacheKey = cacheKey || (cacheId && `${templateFile}#${cacheId}`) || templateFile;
const url = _.get(request, "url.path", request.url);

cacheKey = cacheKey || `${templateFile}#${cacheId}#${url}`;

let asyncTemplate = routeOptions._templateCache[cacheKey];
if (asyncTemplate) {
Expand Down Expand Up @@ -54,7 +57,10 @@ function initializeTemplate(

const templateFullPath = resolvePath(templateFile);
const templateModule = require(templateFullPath); // eslint-disable-line
const template = _.get(templateModule, "default", templateModule);

const templateExp = templateModule.templateTags || templateModule.default || templateModule;

const template = typeof templateExp === "function" ? templateExp(routeOptions) : templateExp;

if (template.$$typeof === xarcJsxElement || template.children) {
// JSX
Expand All @@ -67,9 +73,8 @@ function initializeTemplate(
});
} else {
// Tag
const templateTags = _.get(template, "templateTags", template);
asyncTemplate = new TagRenderer({
templateTags,
templateTags: template,
tokenHandlers: finalTokenHandlers.map(x => loadTokenModuleHandler(x)),
insertTokenIds: routeOptions.insertTokenIds,
routeOptions
Expand Down Expand Up @@ -103,7 +108,7 @@ function makeRouteTemplateSelector(routeOptions) {
if (templateSelection && !templateSelection.templateFile && !templateSelection.htmlFile) {
selection = Object.assign({}, templateSelection, defaultSelection);
}
const asyncTemplate = initializeTemplate(selection, routeOptions);
const asyncTemplate = initializeTemplate(selection, routeOptions, options.request);
return asyncTemplate.render(options);
};

Expand Down

0 comments on commit 7da59d8

Please sign in to comment.