Created
September 1, 2025 22:25
-
-
Save Nooshu/235ef1c57213479cd305f88f3389f6cf to your computer and use it in GitHub Desktop.
Header generator file stripped of all comments used in my "Asset fingerprinting and the preload header in 11ty" blog post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fs from 'fs'; | |
| import path from 'path'; | |
| export function generatePreloadHeaders() { | |
| console.log('๐ Generating preload headers for CSS files...'); | |
| const cssDir = path.join('./_site', 'css'); | |
| if (!fs.existsSync(cssDir)) { | |
| console.log('CSS directory not found, skipping header generation'); | |
| return; | |
| } | |
| const cssFiles = fs.readdirSync(cssDir) | |
| .filter(file => { | |
| return file.match(/^index-[a-f0-9]{10}\.css(\.br)?$/); | |
| }); | |
| if (cssFiles.length === 0) { | |
| console.log('No fingerprinted CSS files found, skipping header generation'); | |
| return; | |
| } | |
| cssFiles.sort((a, b) => { | |
| if (a.endsWith('.br') && !b.endsWith('.br')) return -1; | |
| if (!a.endsWith('.br') && b.endsWith('.br')) return 1; | |
| return 0; | |
| }); | |
| const cssFile = cssFiles[0]; | |
| const cssPath = `/css/${cssFile}`; | |
| console.log(`๐ Found CSS file: ${cssFile}`); | |
| try { | |
| const sourceHeadersPath = path.join('./public', '_headers'); | |
| const targetHeadersPath = path.join('./_site', '_headers'); | |
| if (!fs.existsSync(sourceHeadersPath)) { | |
| console.log('Source _headers file not found'); | |
| return; | |
| } | |
| let headersContent = fs.readFileSync(sourceHeadersPath, 'utf8'); | |
| const preloadHeader = ` Link: <${cssPath}>; rel=preload; as=style; nopush`; | |
| const lines = headersContent.split('\n'); | |
| let globalSectionIndex = -1; | |
| let nextSectionIndex = -1; | |
| for (let i = 0; i < lines.length; i++) { | |
| if (lines[i].trim() === '/*') { | |
| globalSectionIndex = i; | |
| break; | |
| } | |
| } | |
| if (globalSectionIndex !== -1) { | |
| for (let i = globalSectionIndex + 1; i < lines.length; i++) { | |
| if (lines[i].trim() !== '' && !lines[i].startsWith(' ')) { | |
| nextSectionIndex = i; | |
| break; | |
| } | |
| } | |
| } | |
| if (globalSectionIndex !== -1) { | |
| let linkHeaderExists = false; | |
| for (let i = globalSectionIndex + 1; i < (nextSectionIndex === -1 ? lines.length : nextSectionIndex); i++) { | |
| if (lines[i].includes('Link:')) { | |
| lines[i] = preloadHeader; | |
| linkHeaderExists = true; | |
| break; | |
| } | |
| } | |
| if (!linkHeaderExists) { | |
| let lastHeaderIndex = globalSectionIndex; | |
| for (let i = globalSectionIndex + 1; i < (nextSectionIndex === -1 ? lines.length : nextSectionIndex); i++) { | |
| if (lines[i].trim() !== '' && lines[i].startsWith(' ')) { | |
| lastHeaderIndex = i; | |
| } | |
| } | |
| lines.splice(lastHeaderIndex + 1, 0, preloadHeader); | |
| } | |
| headersContent = lines.join('\n'); | |
| } else { | |
| console.log('Could not find global section in _headers file'); | |
| return; | |
| } | |
| fs.writeFileSync(targetHeadersPath, headersContent); | |
| console.log(`Generated preload header: Link: <${cssPath}>; rel=preload; as=style; nopush`); | |
| } catch (error) { | |
| console.error('Error generating preload headers:', error); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment