Skip to content

fix($core): preserve original meta charset and viewport (#2332) #2333

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sakihet
Copy link
Contributor

@sakihet sakihet commented Apr 22, 2020

Summary

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Docs
  • Build-related changes
  • Other, please describe:

If changing the UI of default theme, please provide the before/after screenshot:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

  • When resolving a specific issue, it's referenced in the PR's title (e.g. fix #xxx[,#xxx], where "xxx" is the issue number)

You have tested in the following browsers: (Providing a detailed version will be better.)

  • Chrome
  • Firefox
  • Safari
  • Edge
  • IE

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature
  • Related documents have been updated
  • Related tests have been updated

To avoid wasting your time, it's best to open a feature request issue first and wait for approval before working on it.

Other information:

@sakihet sakihet force-pushed the fix-meta branch 2 times, most recently from 804193a to b37ccfb Compare April 22, 2020 15:12
Copy link
Contributor

@haoranpb haoranpb left a comment

Choose a reason for hiding this comment

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

Hi @sakihet , thanks for your work!

The fix does preserve chartset and viewport, but as mentioned in #2332 , updateMeta'll remove all meta tags from the template. Like <meta name="generator" ... in the default ssr template.

I'd suggest we extract templateMeta from the template and unionBy them together, what do you think?

@sakihet
Copy link
Contributor Author

sakihet commented Apr 24, 2020

hi @ludanxer , thanks for suggestion.

I'd suggest we extract templateMeta from the template and unionBy them together, what do you think?

looks good. i'm going to fix.

@sakihet sakihet force-pushed the fix-meta branch 2 times, most recently from 1cef33f to c27c506 Compare April 24, 2020 23:11
@sakihet
Copy link
Contributor Author

sakihet commented Apr 24, 2020

i updated my PR. please review @ludanxer

const obj = {}
obj[attr] = tag.getAttribute(attr)
templateMeta.push(obj)
} else if (names.every(e => ['name', 'content'].includes(e))) {
Copy link

@NeverBehave NeverBehave May 2, 2020

Choose a reason for hiding this comment

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

wondering about why these if statements are necessary, simply assign each attribute with their name should be fine?

Choose a reason for hiding this comment

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

wondering about why these if statements are necessary, simply assign each attribute with their name should be fine?

e.g. https://github.com/vuejs/vuepress/pull/2360/files#diff-436014f2c7425af1edd74dfd675d069eR42

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NeverBehave some meta tags doesn't have name attribute. then, i wrote these if statements.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta

Copy link

@NeverBehave NeverBehave Jul 8, 2020

Choose a reason for hiding this comment

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

I am a bit confused, I think the getAttributeNames() will return all names of the attributes from the elements:

> a
<meta charset="utf-8">

> a.getAttributeNames()
Array [ "charset" ]

So basically loop through each of these attributes should be fine? What exactly the name you reference to? If <meta name=""/> then it is also included in the function returns.

> document.querySelectorAll('meta')[1]
<meta http-equiv="X-UA-Compatible" content="IE=Edge">

> document.querySelectorAll('meta')[1].getAttributeNames()
Array [ "http-equiv", "content" ]

> document.querySelectorAll('meta')[1].getAttribute('http-equiv')
"X-UA-Compatible"

The example I provided previous did pass the test tho, correct me if I miss anything

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, i misunderstood. thank you for the information.

@@ -35,10 +35,26 @@ export default {

getMergedMetaTags () {
const pageMeta = this.$page.frontmatter.meta || []
const templateMeta = []
if (typeof document !== 'undefined') {
const templateMetaTags = document.querySelectorAll('meta')
Copy link

@NeverBehave NeverBehave May 2, 2020

Choose a reason for hiding this comment

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

this.currentMetaTags already gathers meta tag on mount() and reusing it may provide better code readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NeverBehave thanks!

@NeverBehave
Copy link

Wondering how this pr goes since the related issue really hurts experience on mobile devices.

@haoranpb
Copy link
Contributor

haoranpb commented Jul 8, 2020

@sakihet Sorry for the super delay.

After some thoughts, change how currentMetaTags is initialized seems to preserve all template metas.

Change

this.currentMetaTags = [...document.querySelectorAll('meta')]

into

// init currentMetaTags from pageMeta
this.currentMetaTags = this.getMergedMetaTags().map(m => {
  let selector = 'meta'
  for (const [key, value] of Object.entries(m)) {
    selector += `[${key}="${value}"]`
  }
  return [...document.querySelectorAll(selector)]
}).flat()

What do you think?

@sakihet
Copy link
Contributor Author

sakihet commented Jul 12, 2020

@ludanxer no problem.

After some thoughts, change how currentMetaTags is initialized seems to preserve all template metas.

looks good. thank you for your help.
i updated PR. please check again.

@angeliski
Copy link

Hey @ulivz, do you think you can merge this?

@marchingon12
Copy link

...sooo why isn't this merged yet?

@NeverBehave
Copy link

NeverBehave commented Jun 10, 2021

...sooo why isn't this merged yet?

Checkout the references of #2369 : their own websites also affected by this bug and still nobody seems to be worried.

Probably Vuepress believes that nobody uses Firefox and everyone likes the Chrome's wrong behavior. Maybe fire a bug report to Firefox for following the standard?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants