diff --git a/src/http/any-catchall/_get-preflight.mjs b/src/http/any-catchall/_get-preflight.mjs new file mode 100644 index 0000000..6beb51c --- /dev/null +++ b/src/http/any-catchall/_get-preflight.mjs @@ -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 {} + } +} + diff --git a/src/http/any-catchall/router.mjs b/src/http/any-catchall/router.mjs index 2b300ce..8b08340 100644 --- a/src/http/any-catchall/router.mjs +++ b/src/http/any-catchall/router.mjs @@ -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' @@ -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'), '') @@ -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({ diff --git a/test/_get-preflight.mjs b/test/_get-preflight.mjs new file mode 100644 index 0000000..ea64654 --- /dev/null +++ b/test/_get-preflight.mjs @@ -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') +}) diff --git a/test/mock-preflight/app/preflight.mjs b/test/mock-preflight/app/preflight.mjs new file mode 100644 index 0000000..004985f --- /dev/null +++ b/test/mock-preflight/app/preflight.mjs @@ -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' +}