使用电极,我注意到这种奇怪的行为 -
当我在页面完全加载所有 api 调用和数据后查看页面源时,我只能查看静态内容,例如超链接、标题、页脚链接等。
我创建了一个自定义令牌处理程序,它检查context
对象并填充index.html
. 因此,无论何时,我console.log(context.user.content)
只记录静态数据,例如超链接、标题、页脚链接。
我想这是问题所在,但我无法理解为什么电极无法识别动态呈现的内容。
令牌处理程序.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();
}
};
};
默认.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 在电极中启用。