Skip to content

Commit

Permalink
refactor mimeFor
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfortis committed Sep 19, 2023
1 parent fc88a29 commit 6fe2ece
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 42 deletions.
4 changes: 1 addition & 3 deletions static-pages-bundler/builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export async function buildProduction(router, routes, sitemapDomain, cspNginxVar

write(pDist + route, html)
}


if (sitemapDomain)
write(pDistSitemap, routes
Expand Down Expand Up @@ -127,7 +128,6 @@ export function httpGet(url) {
http.get(url, response => {
if (response.statusCode !== 200)
reject(`URL: ${url}, Status: ${response.statusCode}`)

response.setEncoding('utf8')
let body = ''
response.on('data', chunk => { body += chunk })
Expand All @@ -145,7 +145,6 @@ function cspNonce(data) {
function reportSizes(reportFilename, baseDir, files) {
const oldReport = JSON.parse(read(reportFilename))
const newReport = {}

for (const f of files) {
const fPath = join(baseDir, f)
const size = sizeOf(fPath)
Expand All @@ -155,7 +154,6 @@ function reportSizes(reportFilename, baseDir, files) {
size: size
}
}

console.table(newReport)
saveAsJSON(reportFilename, newReport)
}
Expand Down
15 changes: 8 additions & 7 deletions static-pages-bundler/example-blog/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
all: lint slint opm build

build:
node app.js production


dev:
node app.js development

Expand All @@ -10,10 +16,5 @@ slint:
opm:
../media-optimizer.sh root/media

prod:
sh -c "time node app.js production"

all: lint slint opm prod

.PHONY: dev lint slint opm prod all


.PHONY: dev lint slint opm build all
2 changes: 1 addition & 1 deletion static-pages-bundler/example-blog/packed-sizes.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"/index": {
"hash": "JdW60FhAGi3HYGNlgU4BF1ol0Qs",
"delta": -5,
"delta": 0,
"size": 20322
}
}
1 change: 0 additions & 1 deletion static-pages-bundler/example-blog/root/htmlTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default function htmlTemplate(docRoutes, html, iRoute) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="assets/base.css">
Expand Down
14 changes: 8 additions & 6 deletions static-pages-bundler/example-docs/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
all: lint slint opm build

build:
node app.js production


dev:
node app.js development

Expand All @@ -10,9 +16,5 @@ slint:
opm:
../media-optimizer.sh root/media

prod:
sh -c "time node app.js production"

all: lint slint opm prod

.PHONY: dev lint slint opm prod all

.PHONY: dev lint slint opm build all
1 change: 0 additions & 1 deletion static-pages-bundler/example-docs/root/htmlTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default function htmlTemplate(RouteDefs, html, iRoute) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>${title}</title>
Expand Down
38 changes: 15 additions & 23 deletions static-pages-bundler/routerForStaticPages.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
import fs from 'node:fs'
import { extname } from 'node:path'


const mimes = { // JPG is deliberately omitted (explained in ./media-optimizer)
'.js': 'application/javascript',
'.zip': 'application/zip',
'.json': 'application/json',

'.ico': 'image/x-icon',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.avif': 'image/avif',
'.webp': 'image/webp',

'.css': 'text/css',
'.html': 'text/html',

'.txt': 'text/plain', // e.g. for robots.txt when running lighthouse
'.less': 'text/plain',
'.scss': 'text/plain',

'.mp4': 'video/mp4'
'html': 'text/html; charset=utf8',
'txt': 'text/plain; charset=utf8', // e.g. robots.txt when running lighthouse
'js': 'application/javascript; charset=utf8',
'json': 'application/json; charset=utf8',
'css': 'text/css; charset=utf8',
'less': 'text/plain; charset=utf8',
'svg': 'image/svg+xml; charset=utf8',
'zip': 'application/zip',
'ico': 'image/x-icon',
'png': 'image/png',
'avif': 'image/avif',
'webp': 'image/webp',
'mp4': 'video/mp4'
}

function mimeFor(filename) {
const mime = mimes[extname(filename)]
const mime = mimes[filename.replace(/.*\./, '')]
if (mime)
return mime
throw `Missing MIME for ${filename}`
}



export function routerForStaticPages(rootPath, routes, template, templateArg) {
const altRoot = '/root-meta' // @Convention

Expand All @@ -53,7 +45,7 @@ export function routerForStaticPages(rootPath, routes, template, templateArg) {

else if (routes.includes(url)) { /* Serve Documents */
const html = await fs.promises.readFile(rootPath + url + '.html')
response.setHeader('Content-Type', 'text/html')
response.setHeader('Content-Type', mimeFor('html'))
response.end(htmlTemplate(html, routes.indexOf(url)))
}

Expand Down

0 comments on commit 6fe2ece

Please sign in to comment.