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

@ -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,22 +176,36 @@ export default defineConfig({
}
: undefined,
transformHtml: prod
? (code, id, ctx) => {
// TODO: add only on prod
transformHtml: (code, id, ctx) => {
if (id.endsWith('/404.html')) return
// TODO: provide this as manifest
const $ = cheerio.load(code)
const { links } = $.extract({
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' }
}
return null
}
}
]
})
const toPreload: HeadConfig[] = links.map((link) => ['link', link])
const toPreload: HeadConfig[] = [...m.links, ...m.scripts].map((link) => {
return ['link', link]
})
id = id
.slice(ctx.siteConfig.outDir.length)
@ -204,11 +218,9 @@ export default defineConfig({
id,
'Link: ' + toPreload.map((link) => toLinkHeader(link)).join(', ')
])
}
: undefined,
},
buildEnd: prod
? (siteConfig) => {
buildEnd: (siteConfig) => {
const _headers =
headers
.sort(
@ -226,7 +238,6 @@ export default defineConfig({
'utf-8'
)
}
: undefined
})
function toLinkHeader([_, { href, ...attributes }]: HeadConfig): string {

Loading…
Cancel
Save