link-headers-experiment
Divyansh Singh 3 months ago
parent 0166b8efa1
commit e936b34c13

@ -1,4 +1,6 @@
import * as cheerio from 'cheerio' import * as cheerio from 'cheerio'
import fs from 'node:fs'
import path from 'node:path'
import { import {
defineConfig, defineConfig,
resolveSiteDataByRoute, resolveSiteDataByRoute,
@ -10,8 +12,6 @@ import {
localIconLoader localIconLoader
} from 'vitepress-plugin-group-icons' } from 'vitepress-plugin-group-icons'
import llmstxt from 'vitepress-plugin-llms' import llmstxt from 'vitepress-plugin-llms'
import fs from 'node:fs'
import path from 'node:path'
const prod = !!process.env.NETLIFY const prod = !!process.env.NETLIFY
@ -125,9 +125,9 @@ export default defineConfig({
apiKey: '52f578a92b88ad6abde815aae2b0ad7c', apiKey: '52f578a92b88ad6abde815aae2b0ad7c',
indexName: 'vitepress' indexName: 'vitepress'
} }
}, }
carbonAds: { code: 'CEBDT27Y', placement: 'vuejsorg' } // carbonAds: { code: 'CEBDT27Y', placement: 'vuejsorg' } // TODO: temporarily disabled
}, },
locales: { locales: {
@ -176,22 +176,36 @@ export default defineConfig({
} }
: undefined, : undefined,
transformHtml: prod // TODO: add only on prod
? (code, id, ctx) => { transformHtml: (code, id, ctx) => {
if (id.endsWith('/404.html')) return if (id.endsWith('/404.html')) return
// TODO: provide this as manifest // TODO: provide this as manifest
const $ = cheerio.load(code) const $ = cheerio.load(code)
const { links } = $.extract({ const m = $.extract({
links: [ links: [
{ {
selector: 'link:is([rel*=preload],[rel*=preconnect])', selector: 'link:is([rel*=preload],[rel*=preconnect])',
value: (el) => el.attribs 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' }
}
return null
}
}
] ]
}) })
const toPreload: HeadConfig[] = links.map((link) => ['link', link]) const toPreload: HeadConfig[] = [...m.links, ...m.scripts].map((link) => {
return ['link', link]
})
id = id id = id
.slice(ctx.siteConfig.outDir.length) .slice(ctx.siteConfig.outDir.length)
@ -204,11 +218,9 @@ export default defineConfig({
id, id,
'Link: ' + toPreload.map((link) => toLinkHeader(link)).join(', ') 'Link: ' + toPreload.map((link) => toLinkHeader(link)).join(', ')
]) ])
} },
: undefined,
buildEnd: prod buildEnd: (siteConfig) => {
? (siteConfig) => {
const _headers = const _headers =
headers headers
.sort( .sort(
@ -226,7 +238,6 @@ export default defineConfig({
'utf-8' 'utf-8'
) )
} }
: undefined
}) })
function toLinkHeader([_, { href, ...attributes }]: HeadConfig): string { function toLinkHeader([_, { href, ...attributes }]: HeadConfig): string {

Loading…
Cancel
Save