Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't add extra meta tag if it exists #1802

Merged
merged 1 commit into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class HtmlWebpackPlugin {
// Default metaOptions if no template is provided
if (!userOptions.template && options.templateContent === false && options.meta) {
const defaultMeta = {
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
// TODO remove in the next major release
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid to add any meta tags in the next major release, we can provide a good default HTML template, it should be inside template or we should fully parse HTML document and check if we already have duplications

// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
viewport: 'width=device-width, initial-scale=1'
};
options.meta = Object.assign({}, options.meta, defaultMeta, userOptions.meta);
Expand Down Expand Up @@ -966,8 +967,15 @@ function hookIntoCompiler (compiler, options, plugin) {
const htmlRegExp = /(<html[^>]*>)/i;
const headRegExp = /(<\/head\s*>)/i;
const bodyRegExp = /(<\/body\s*>)/i;
const metaViewportRegExp = /<meta[^>]+name=["']viewport["'][^>]*>/i;
const body = assetTags.bodyTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
const head = assetTags.headTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
const head = assetTags.headTags.filter((item) => {
if (item.tagName === 'meta' && item.attributes && item.attributes.name === 'viewport' && metaViewportRegExp.test(html)) {
return false;
}

return true;
}).map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));

if (body.length) {
if (bodyRegExp.test(html)) {
Expand Down
15 changes: 15 additions & 0 deletions spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,21 @@ describe('HtmlWebpackPlugin', () => {
}, [/<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">/], null, done);
});

it('avoid duplicate meta tags for default template', done => {
testHtmlPlugin({
mode: 'production',
entry: path.join(__dirname, 'fixtures/index.js'),
context: path.join(__dirname, 'fixtures'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [
new HtmlWebpackPlugin()
]
}, [/<head><meta charset="utf-8"\/><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover"><title>src\/index\.ejs<\/title><script defer="defer" src="index_bundle.js"><\/script><\/head>/], null, done);
});

it('adds a meta tag with short notation', done => {
testHtmlPlugin({
mode: 'production',
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover">
<title>src/index.ejs</title>
</head>
<body>
Expand Down