From e936b34c132a86f0807fc2bdf4d1cb3778253607 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 27 Jun 2025 01:13:29 +0530 Subject: [PATCH] try --- docs/.vitepress/config.ts | 107 +++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 24c7300b..db961c93 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -1,4 +1,6 @@ import * as cheerio from 'cheerio' +import fs from 'node:fs' +import path from 'node:path' import { defineConfig, resolveSiteDataByRoute, @@ -10,8 +12,6 @@ import { localIconLoader } from 'vitepress-plugin-group-icons' import llmstxt from 'vitepress-plugin-llms' -import fs from 'node:fs' -import path from 'node:path' const prod = !!process.env.NETLIFY @@ -125,9 +125,9 @@ export default defineConfig({ apiKey: '52f578a92b88ad6abde815aae2b0ad7c', indexName: 'vitepress' } - }, + } - carbonAds: { code: 'CEBDT27Y', placement: 'vuejsorg' } + // carbonAds: { code: 'CEBDT27Y', placement: 'vuejsorg' } // TODO: temporarily disabled }, locales: { @@ -176,57 +176,68 @@ export default defineConfig({ } : undefined, - transformHtml: prod - ? (code, id, ctx) => { - if (id.endsWith('/404.html')) return + // TODO: add only on prod + transformHtml: (code, id, ctx) => { + if (id.endsWith('/404.html')) return - // TODO: provide this as manifest + // TODO: provide this as manifest - const $ = cheerio.load(code) - const { links } = $.extract({ - links: [ - { - selector: 'link:is([rel*=preload],[rel*=preconnect])', - value: (el) => el.attribs + const $ = cheerio.load(code) + const m = $.extract({ + links: [ + { + selector: 'link:is([rel*=preload],[rel*=preconnect])', + value: (el) => el.attribs + } + ], + scripts: [ + { + selector: 'script[type=module]', + value: (el) => { + const src = el.attribs.src + if (src && !src.startsWith('http')) { + return { href: src, rel: 'modulepreload' } } - ] - }) - const toPreload: HeadConfig[] = links.map((link) => ['link', link]) - - id = id - .slice(ctx.siteConfig.outDir.length) - .replace(/(^|\/)index(?:\.html)?$/, '$1') - if (ctx.siteConfig.cleanUrls) { - id = id.replace(/\.html$/, '') + return null + } } + ] + }) + const toPreload: HeadConfig[] = [...m.links, ...m.scripts].map((link) => { + return ['link', link] + }) + + id = id + .slice(ctx.siteConfig.outDir.length) + .replace(/(^|\/)index(?:\.html)?$/, '$1') + if (ctx.siteConfig.cleanUrls) { + id = id.replace(/\.html$/, '') + } - headers.push([ - id, - 'Link: ' + toPreload.map((link) => toLinkHeader(link)).join(', ') - ]) - } - : undefined, + headers.push([ + id, + 'Link: ' + toPreload.map((link) => toLinkHeader(link)).join(', ') + ]) + }, - buildEnd: prod - ? (siteConfig) => { - const _headers = - headers - .sort( - (a, b) => - b[0].length - a[0].length || - a[0].localeCompare(b[0]) || - a[1].localeCompare(b[1]) - ) - .map(([id, header]) => `${id}\n\t${header}`) - .join('\n\n') + '\n' - - fs.writeFileSync( - path.join(siteConfig.outDir, '_headers'), - _headers, - 'utf-8' + buildEnd: (siteConfig) => { + const _headers = + headers + .sort( + (a, b) => + b[0].length - a[0].length || + a[0].localeCompare(b[0]) || + a[1].localeCompare(b[1]) ) - } - : undefined + .map(([id, header]) => `${id}\n\t${header}`) + .join('\n\n') + '\n' + + fs.writeFileSync( + path.join(siteConfig.outDir, '_headers'), + _headers, + 'utf-8' + ) + } }) function toLinkHeader([_, { href, ...attributes }]: HeadConfig): string {