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

docs: add custom preset instructions #1409

Merged
merged 6 commits into from
Jul 14, 2023
Merged
Changes from all 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
97 changes: 94 additions & 3 deletions docs/content/2.deploy/3.custom-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,98 @@ icon: ri:file-code-line
# Custom Preset

::alert{type="warning"}
**Work in Progress**
:br
Documentation for this section is not yet complete.
Custom local preset support is an experimental feature.
::

If you want to use a provider that Nitro doesn't support, or want to modify an existing one, you can create a local custom preset in your project.

Custom presets are local files that have a preset entry that defines builder configuration and a runtime entry point.

## Example

::alert
Check [unjs/nitro-preset-starter](https://github.com/unjs/nitro-preset-starter) for a ready-to-use template.
::

First, we have to define our preset entry point in a local directory `preset/index.ts`

```ts [./preset/index.ts]
import type { NitroPreset } from "nitropack";
import { fileURLToPath } from "node:url"

export default <NitroPreset>{
// extends: "node-server", // You can extend existing presets
entry: fileURLToPath(new URL("./entry.ts", import.meta.url)),
hooks: {
compiled() {
// ...
},
},
};
```

The entry point will be used by your server or provider, and you can fully customize its behavior.

::code-group
```ts [preset/entry.ts (Workers)]
import "#internal/nitro/virtual/polyfill";

const nitroApp = useNitroApp();

export default {
fetch(request: Request) {
const url = new URL(request.url);
return nitroApp.localFetch(url.pathname + url.search, {
context: {},
host: url.hostname,
protocol: url.protocol,
method: request.method,
headers: request.headers,
body: undefined,
});
},
};
```

```ts [preset/entry.ts (Node.js)]
import "#internal/nitro/virtual/polyfill";
import { Server } from "node:http";
import { toNodeListener } from "h3";

const nitroApp = useNitroApp();
const server = new Server(toNodeListener(nitroApp.h3App));

// @ts-ignore
server.listen(3000, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Listening on http://localhost:3000 (custom preset)`);
});
```
::


Then in your nitro config file, you can use your custom preset.

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
preset: "./preset",
});
```

```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
preset: "./preset",
}
});
```
::


Refer to the Nitro [source code](https://github.com/unjs/nitro/tree/main/src) directly to have a better understanding of presets and entry points.