vitepress icon indicating copy to clipboard operation
vitepress copied to clipboard

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

Open newives opened this issue 1 year ago • 14 comments
trafficstars

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 avatar Jan 08 '24 15:01 newives

图片 I'm sure this is a bug My updated demo

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

newives avatar Jan 09 '24 03:01 newives

图片 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

Octobug avatar Jan 09 '24 03:01 Octobug

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

newives avatar Jan 09 '24 04:01 newives

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

brc-dd avatar Jan 09 '24 05:01 brc-dd

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')

Octobug avatar Jan 09 '24 05:01 Octobug

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.

brc-dd avatar Jan 09 '24 05:01 brc-dd

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

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

图片

newives avatar Jan 09 '24 06:01 newives

Strange, I cannot reproduce the bug.

Seems to be only happening on firefox.

Oh right. On Firefox it could reproduce.

Octobug avatar Jan 09 '24 06:01 Octobug

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.

brc-dd avatar Jan 09 '24 06:01 brc-dd

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

Octobug avatar Jan 09 '24 09:01 Octobug

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).

brc-dd avatar Jan 09 '24 10:01 brc-dd

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.

zhangyx1998 avatar Jan 10 '24 17:01 zhangyx1998

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.

Octobug avatar Jan 11 '24 02:01 Octobug

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.

svanimpe avatar Jan 16 '24 11:01 svanimpe