diff --git a/.gitignore b/.gitignore index 638c6d8f..f9a1fa3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vitepress/cache +.vitepress/.temp dist node_modules diff --git a/.vitepress/config.ts b/.vitepress/config.ts index 6bc13ebd..64abe5d0 100644 --- a/.vitepress/config.ts +++ b/.vitepress/config.ts @@ -8,7 +8,6 @@ import { groupIconVitePlugin, } from 'vitepress-plugin-group-icons' - import { markdownItImageSize } from 'markdown-it-image-size' import packageJson from '../package.json' with { type: 'json' } import { buildEnd } from './buildEnd.config' @@ -95,30 +94,6 @@ export default defineConfig({ 'link', { rel: 'alternate', type: 'application/rss+xml', href: '/blog.rss' }, ], - ['link', { rel: 'preconnect', href: 'https://fonts.googleapis.com' }], - [ - 'link', - { - rel: 'preconnect', - href: 'https://fonts.gstatic.com', - crossorigin: 'true', - }, - ], - [ - 'link', - { - rel: 'preload', - href: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap', - as: 'style', - }, - ], - [ - 'link', - { - rel: 'stylesheet', - href: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap', - }, - ], inlineScript('banner.js'), ['link', { rel: 'me', href: 'https://m.webtoo.ls/@vite' }], ['meta', { property: 'og:type', content: 'website' }], @@ -475,16 +450,34 @@ export default defineConfig({ level: [2, 3], }, }, - transformPageData(pageData) { - const canonicalUrl = `${ogUrl}/${pageData.relativePath}` - .replace(/\/index\.md$/, '/') - .replace(/\.md$/, '') - pageData.frontmatter.head ??= [] - pageData.frontmatter.head.unshift( - ['link', { rel: 'canonical', href: canonicalUrl }], - ['meta', { property: 'og:title', content: pageData.title }], - ) - return pageData + transformHead(ctx) { + const path = ctx.page.replace(/(^|\/)index\.md$/, '$1').replace(/\.md$/, '') + + if (path !== '404') { + const canonicalUrl = path ? `${ogUrl}/${path}` : ogUrl + ctx.head.push( + ['link', { rel: 'canonical', href: canonicalUrl }], + ['meta', { property: 'og:title', content: ctx.pageData.title }], + ) + } + + // For the landing page, move the google font links to the top for better performance + if (path === '') { + const googleFontLinks: HeadConfig[] = [] + for (let i = 0; i < ctx.head.length; i++) { + const tag = ctx.head[i] + if ( + tag[0] === 'link' && + (tag[1]?.href?.includes('fonts.googleapis.com') || + tag[1]?.href?.includes('fonts.gstatic.com')) + ) { + ctx.head.splice(i, 1) + googleFontLinks.push(tag) + i-- + } + } + ctx.head.unshift(...googleFontLinks) + } }, markdown: { // languages used for twoslash and jsdocs in twoslash diff --git a/index.md b/index.md index f4779b94..6a459285 100644 --- a/index.md +++ b/index.md @@ -9,6 +9,22 @@ layout: home aside: false editLink: false markdownStyles: false + +head: + - - link + - rel: preconnect + href: https://fonts.googleapis.com + - - link + - rel: preconnect + href: https://fonts.gstatic.com + crossorigin: '' + - - link + - rel: preload + href: https://fonts.googleapis.com/css2?family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap + as: style + - - link + - rel: stylesheet + href: https://fonts.googleapis.com/css2?family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap ---