vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
11.48k stars 1.86k forks source link

The anchor point of markdown file is incorrect after using vue component #3428

Open newives opened 4 months ago

newives commented 4 months ago

Describe the bug

The anchor point of markdown file is incorrect after using vue component

Reproduction

https://stackblitz.com/edit/vite-ntyrly?file=docs%2Fexample.md

Expected behavior

Anchor point correct

System Info

mac
Firefox

Additional context

图片

Validations

newives commented 4 months ago
图片

I'm sure this is a bug My updated demo

https://stackblitz.com/edit/vite-ntyrly?file=docs%2Ftest.vue

Octobug commented 4 months ago

图片 I'm sure this is a bug My updated demo https://stackblitz.com/edit/vite-ntyrly?file=docs%2Ftest.vue

How do you get to the #powered-by-vite URL in this Image? Click on the outline item, click on the # in front of the "Powered by Vite", or access directly through a copied URL? @newives

newives commented 4 months ago

Click on Powered by Vite on the left and refresh the webpage to reproduce

brc-dd commented 4 months ago

Ah right, it's not maintaining position on reload. Doesn't seem to be happening on our docs site though 👀

Octobug commented 4 months ago

Click on Powered by Vite on the left and refresh the webpage to reproduce

Strange, I cannot reproduce the bug. Unless I

Click on Powered by Vite on the left, then scroll to somewhere else, and refresh the webpage: the scrollbar is automatically scrolled to somewhere else by the browser. (this is because of not setting history.scrollRestoration = 'manual')

brc-dd commented 4 months ago

Strange, I cannot reproduce the bug.

Seems to be only happening on firefox.

the scrollbar is automatically scrolled to somewhere else by the browser

This is expected behavior IMO. But this too doesn't work on firefox. In build, it's moving to the top, and in dev, it's just weird.

newives commented 4 months ago

The demo below does not locate the accurate anchor point in any browser.

https://stackblitz.com/edit/vite-fjgh8q?file=docs%2Fexample.md

图片
Octobug commented 4 months ago

Strange, I cannot reproduce the bug.

Seems to be only happening on firefox.

Oh right. On Firefox it could reproduce.

brc-dd commented 4 months ago

The demo below does not locate the accurate anchor point in any browser.

That's just layout shift. You haven't added height/width or aspect ratio to that image - https://web.dev/articles/optimize-cls

Add that and it will work fine in Chromium/Safari.

Octobug commented 4 months ago

The demo below does not locate the accurate anchor point in any browser.

That's just layout shift. You haven't added height/width or aspect ration to that image - https://web.dev/articles/optimize-cls

Add that and it will work fine in Chromium/Safari.

I've read this part of the article Modern best practice for setting image dimensions. It looks a bit inconvenient for writing a markdown post with images...

Could it be done by postponing the execution of scrollTo(), something like:

window.onload = () => {
  scrollTo(...);
}

This should also solve the Firefox problem. The incorrect refresh scroll is done by firefox automatically. It seems that Chrome also does it before scrollTo(), so in Chrome it works fine.

But with window.onload it might be a problem for loading="lazy" images, When window.onload Triggers With Lazy Loaded Images

brc-dd commented 4 months ago

It looks a bit inconvenient for writing a markdown post with images

You can use some markdown-it plugin like https://github.com/boyum/markdown-it-image-size or https://github.com/ulfschneider/markdown-it-fitmedia though. There are also vite plugins out there that can optimize the images (create modern formats or blurhash like fallbacks).

zhangyx1998 commented 4 months ago

The demo below does not locate the accurate anchor point in any browser.

That's just layout shift. You haven't added height/width or aspect ration to that image - https://web.dev/articles/optimize-cls Add that and it will work fine in Chromium/Safari.

I've read this part of the article Modern best practice for setting image dimensions. It looks a bit inconvenient for writing a markdown post with images...

Could it be done by postponing the execution of scrollTo(), something like:

window.onload = () => {
  scrollTo(...);
}

This should also solve the Firefox problem. The incorrect refresh scroll is done by firefox automatically. It seems that Chrome also does it before scrollTo(), so in Chrome it works fine.

But with window.onload it might be a problem for loading="lazy" images, When window.onload Triggers With Lazy Loaded Images

I think you need to take into account the scenario when user scrolls away before onload fires, otherwise the page will jump back even if user has already scrolled away.

That means you also need to listen for onscroll, and disable onload behavior upon user scroll.

Octobug commented 4 months ago

I think you need to take into account the scenario when user scrolls away before onload fires, otherwise the page will jump back even if user has already scrolled away.

That means you also need to listen for onscroll, and disable onload behavior upon user scroll.

Yeah, my thought was to postpone the execution of scrollTo(). But I wasn't sure in what way it could be done perfectly.

svanimpe commented 4 months ago

I had a similar issue. In my case, it was caused by a Vue component that added an <img> but that image didn't have a width and height specified. Adding those resolved the issue.