Skip to content

Commit

Permalink
fix: remove helmet when in client bundle umi.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ycjcl868 committed May 15, 2020
1 parent fb58852 commit 97e4bf4
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions packages/plugin-helmet/src/templates/runtime.tpl
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
{{#SSR}}
import { Helmet } from '{{{ HelmetPkg }}}';

export const ssr = {
modifyServerHTML: (html, { cheerio }) => {
const helmet = Helmet.renderStatic();
if (!helmet) return html;
const title = helmet.title.toString();
const htmlAttributes = helmet.htmlAttributes.toComponent();
const meta = helmet.meta.toString();
const link = helmet.link.toString();
const $ = cheerio.load(html);
if (title) {
$('head').prepend(title);
}
if (meta) {
$('head').append(meta);
}
if (link) {
$('link').append(link);
let ssr = {};
// remove when client bundle umi.js
if (process.env.__IS_SERVER) {
ssr = {
modifyServerHTML: (html, { cheerio }) => {
const { Helmet } = require('{{{ HelmetPkg }}}');
const helmet = Helmet.renderStatic();
if (!helmet) return html;
const title = helmet.title.toString();
const htmlAttributes = helmet.htmlAttributes.toComponent();
const meta = helmet.meta.toString();
const link = helmet.link.toString();
const $ = cheerio.load(html);
if (title) {
$('head').prepend(title);
}
if (meta) {
$('head').append(meta);
}
if (link) {
$('link').append(link);
}
if (Object.keys(htmlAttributes)) {
Object.keys(htmlAttributes).forEach(attrKey => {
$('html').attr(attrKey, htmlAttributes[attrKey]);
});
}
return $.html();
}
if (Object.keys(htmlAttributes)) {
Object.keys(htmlAttributes).forEach(attrKey => {
$('html').attr(attrKey, htmlAttributes[attrKey]);
});
}
return $.html();
}
};
}

export {
ssr
};
{{/SSR}}

0 comments on commit 97e4bf4

Please sign in to comment.