Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preflight #185

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions app/docs/md/conventions/preflight.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: Preflight
---


The purpose of the preflight file is to supply default application state.
It can also be used to customize data based on the request per route.

Enhance looks for the preflight file in the root of your app.

<doc-code filename="app/preflight.mjs">

```bash
app
├── api ............... data routes
│ └── index.mjs ..... override default preflight application state with api data
└── preflight.mjs ..... pre-populate application state

```
</doc-code>

The preflight function is passed the request object enabling you to customize data per requested route.
API responses are merged with the default state returned from preflight allowing you to override default state with specific API data per request.

### Basic example

<doc-code filename="app/preflight.mjs">

```javascript
export default function Preflight ({ req }) {
return { /* ...Your data here */ }
}
````
</doc-code>



### Example of setting the page title using preflight

<doc-code filename="app/preflight.mjs">

```javascript
export default function Preflight ({ req }) {
return {
pageTitle: getPageTitle(req.path),
account: {
username: 'bobsyouruncle',
id: '23jk24h24'
}
}
}

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

return titleMap[path] || 'My App Name'
}
````
</doc-code>



### Example of overriding default preflight data with an API response

<doc-code filename="app/preflight.mjs">

```javascript
export default function Preflight ({ req }) {
return {
pageTitle: getPageTitle(req.path),
account: {
username: 'bobsyouruncle',
id: '23jk24h24'
}
}
}
````
</doc-code>

<doc-code filename="app/api/index.mjs">

```javascript
export async function get() {
return {
json: {
account: {
username: 'thisshouldoverride',
id: '39nr34n2'
}
}
}
}
````
</doc-code>

The account object will be overridden by the API response.



1 change: 1 addition & 0 deletions app/docs/nav-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const data = [
'browser',
'public',
'404-errors',
'preflight',
],
},
{
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ preconfigured
prepended
preloaded
preprocessing
preflight
Preflight
rulese(t|ts)
scaleMax
scaleMin
Expand Down