Skip to content

Commit

Permalink
Adds initial pass at preflight.
Browse files Browse the repository at this point in the history
  • Loading branch information
kj committed Dec 8, 2023
1 parent c4876b5 commit f56c9ab
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/http/any-catchall/_get-preflight.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { join } from 'path'
import { pathToFileURL } from 'url'

export default async function GetPreflight({ basePath = '' }) {
try {
const pathToPreflight = pathToFileURL(join(basePath, 'preflight.mjs')).href
const { default: preflight } = await import(pathToPreflight)
return preflight
}
catch (error) {
return {}
}
}

6 changes: 3 additions & 3 deletions src/http/any-catchall/router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import headerTimers from 'header-timers'
import getModule from './_get-module.mjs'
import getElements from './_get-elements.mjs'
import getPageName from './_get-page-name.mjs'
import getPreflight from './_get-preflight.mjs'
import isJSON from './_is-json-request.mjs'
import backfill from './_backfill-params.mjs'
import render from './_render.mjs'
Expand All @@ -26,6 +27,7 @@ export default async function api(options, req) {
let pagePath = getModule(basePath, 'pages', req.rawPath)
let apiBaseUsed = basePath
let pageBaseUsed = basePath
let preflight = await getPreflight({ basePath })

if (altPath) {
let apiPathPart = apiPath && apiPath.replace(path.join(basePath, 'api'), '')
Expand Down Expand Up @@ -141,9 +143,7 @@ export default async function api(options, req) {
let elements = { ...altHeadElements.elements, ...baseHeadElements.elements }
timers.stop('elements')

const store = state.json
? state.json
: {}
const store = Object.assign(preflight(req), state.json ? state.json : {})

function html(str, ...values) {
const _html = enhance({
Expand Down
19 changes: 19 additions & 0 deletions test/_get-preflight.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from 'path'
import test from 'tape'
import url from 'url'
import getPreflight from '../src/http/any-catchall/_get-preflight.mjs'
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

test.only('getPreflight', async t => {
t.plan(1)
const basePath = path.join(__dirname, 'mock-preflight', 'app')
const expected = {
title: 'About',
account: {
username: 'Bob Syouruncle',
id: '23jk24h24'
}
}
const preflight = await getPreflight({ basePath })
t.deepEqual(expected, preflight({ req: { path: '/about' } }), 'Got preflight')
})
19 changes: 19 additions & 0 deletions test/mock-preflight/app/preflight.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function Preflight({ req }) {
return {
title: getPageTitle(req.path),
account: {
username: 'Bob Syouruncle',
id: '23jk24h24'
}
}
}

function getPageTitle(path) {
const titleMap = {
'/': 'Home',
'/about': 'About',
'/account': 'My Account'
}

return titleMap[path] || 'My App Name'
}

0 comments on commit f56c9ab

Please sign in to comment.