【发布时间】:2019-02-28 05:17:13
【问题描述】:
使用电极,我注意到了这种奇怪的行为 -
当我在页面完全加载所有 api 调用和数据后查看页面源时,我只能查看静态内容,例如超链接、标题、页脚链接等。
我创建了一个自定义令牌处理程序,它检查context 对象并填充index.html 中存在的自定义令牌。
因此,无论何时,我 console.log(context.user.content),只会记录静态数据,例如超链接、标题、页脚链接。
我想这是问题所在,但我无法理解为什么电极无法识别动态呈现的内容。
Token-Handler.js 文件
import Helmet from 'react-helmet';
const emptyTitleRegex = /<title[^>]*><\/title>/;
module.exports = function setup(options) {
// console.log({ options });
return {
INITIALIZE: context => {
context.user.helmet = Helmet.renderStatic();
},
PAGE_TITLE: context => {
const helmet = context.user.helmet;
const helmetTitleScript = helmet.title.toString();
const helmetTitleEmpty = helmetTitleScript.match(emptyTitleRegex);
return helmetTitleEmpty ? `<title>${options.routeOptions.pageTitle}</title>` : helmetTitleScript;
},
REACT_HELMET_SCRIPTS: context => {
const scriptsFromHelmet = ["link", "style", "script", "noscript"]
.map(tagName => context.user.helmet[tagName].toString())
.join("");
return `<!--scripts from helmet-->${scriptsFromHelmet}`;
},
META_TAGS: context => {
console.log(context,'123') //this is where I am checking
return context.user.helmet.meta.toString();
}
};
};
default.js
module.exports = {
port: portFromEnv() || "3000",
webapp: {
module: "electrode-react-webapp/lib/express",
options: {
prodBundleBase: '/buy-used-car/js/',
insertTokenIds: false,
htmlFile: "./{{env.APP_SRC_DIR}}/client/index.html",
paths: {
"*": {
content: {
module: "./{{env.APP_SRC_DIR}}/server/views/index-view"
},
}
},
serverSideRendering: true,
tokenHandler: "./{{env.APP_SRC_DIR}}/server/token-handler"
}
}
};
有什么线索吗?
编辑 1
但是,在元标记上发生的任何后续更新都会被呈现。我不确定这是电极允许的还是react-helmet 的功能。
编辑 2
在电极中启用了 SSR。
【问题讨论】: