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

If I use the canonicalUrl example syntax in vitepress version 1.2.2, an error occurs when building. #3923

Closed
4 tasks done
isishome opened this issue May 27, 2024 · 6 comments
Labels
need more info Further information is requested

Comments

@isishome
Copy link

isishome commented May 27, 2024

Describe the bug

If I use the canonicalUrl example syntax in vitepress version 1.2.2, an error occurs when building.

str.replace is not a function

So, I downgraded to version 1.2.1 and the error did not occur.

Reproduction

install Vitepress v 1.2.2

canonicalUrl example canonical ex

const canonicalUrl = `https://myhome.com/${pageData.relativePath}`.replace(/index\.md$/, '').replace(/\.(md|html)$/, '')
pageData.frontmatter.head ??= []
pageData.frontmatter.head.push(['meta', { property: 'og:title', content: pageData.title }])
pageData.frontmatter.head.push(['meta', { property: 'og:description', content: pageData.description }])
pageData.frontmatter.head.push(['meta', { property: 'og:image', content: 'https://myhome.com/images/og.jpg' }])
pageData.frontmatter.head.push(['meta', { property: 'og:url', content: canonicalUrl }])
pageData.frontmatter.head.push(['link', { rel: 'canonical', href: canonicalUrl }])

npm run docs:build

Expected behavior

failed to build

occur error => str.replace is not a function

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 21.07 GB / 31.92 GB
  Binaries:
    Node: 18.18.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (124.0.2478.97)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    vitepress: ^1.2.2 => 1.2.2

Additional context

No response

Validations

@isishome isishome added the bug: pending triage Maybe a bug, waiting for confirmation label May 27, 2024
@isishome isishome changed the title If you use the canonicalUrl example syntax in vitepress version 1.2.2, an error occurs when building. If I use the canonicalUrl example syntax in vitepress version 1.2.2, an error occurs when building. May 27, 2024
@brc-dd
Copy link
Member

brc-dd commented May 27, 2024

Please share a reproducible example. Most likely your canonicalUrl variable is undefined in some case. Try wrapping the head.push statement inside a condtion.

@brc-dd brc-dd added need more info Further information is requested and removed bug: pending triage Maybe a bug, waiting for confirmation labels May 27, 2024
@isishome
Copy link
Author

isishome commented May 27, 2024

Reproduction

The reproduction has been corrected.

Versions prior to v1.2.2 build with the same code without issue.

@brc-dd
Copy link
Member

brc-dd commented May 27, 2024

It still is not reproducible.

image


Please share an example using https://vitepress.new

Like I said, you might have some attribute value that is not a string.

@isishome
Copy link
Author

isishome commented May 28, 2024

If I run it in developer mode, there is no problem, but if I just run the build, the following error occurs.

If I comment out the canonical syntax, it builds without a problem.

str.replace is not a function TypeError: str.replace is not a function at escapeHtml$1 (file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:19945:14) at file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:46689:24 at Array.map (<anonymous>) at renderAttrs (file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:46687:29) at file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:46671:33 at Array.map (<anonymous>) at renderHead (file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:46670:10) at renderPage (file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:46625:13) at async pMap.concurrency (file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:46770:11) at async file:///D:/Projects/myhome/node_modules/vitepress/dist/node/serve-BwR5EPUJ.js:2618:20

@isishome
Copy link
Author

isishome commented May 28, 2024

I just found the cause, and found that the replace used in the canonicalUrl variable is not the cause, but occurs in pageData.description.

I have a document where the description is written in multiple lines, and I found out that this was causing the problem. If pageData.description is in Array format, no error occurs as it is arbitrarily changed to join to string.
I wonder why the same error didn't occur in previous versions.

@brc-dd
Copy link
Member

brc-dd commented May 28, 2024

Previous escape function was more lax and incorrectly serialised such values. pageData.description should always be a string unless you’ve wrong frontmatter. Most likely it wouldn’t have worked correctly with older versions too. Sure they won’t throw error, but the generated html tags will be wrong. Closing as it’s not a bug with VitePress rather wrong content.

@brc-dd brc-dd closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need more info Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants