Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into cloudflare-page-non-g…
Browse files Browse the repository at this point in the history
…et-req
  • Loading branch information
danielroe committed Feb 21, 2023
2 parents 954d8be + 8f76db8 commit 1d4a591
Show file tree
Hide file tree
Showing 65 changed files with 3,020 additions and 2,036 deletions.
19 changes: 10 additions & 9 deletions .eslintrc → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"extends": ["eslint-config-unjs"],
"rules": {
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: ["eslint-config-unjs"],
rules: {
"space-before-function-paren": 0,
"arrow-parens": 0,
"comma-dangle": 0,
"semi": 0,
semi: 0,
"unicorn/prevent-abbreviations": 0,
"quotes": 0,
quotes: 0,
"keyword-spacing": 0,
"no-undef": 0,
"indent": 0,
indent: 0,
"unicorn/catch-error-name": 0,
"unicorn/no-null": 0,
"unicorn/no-useless-undefined": 0,
"unicorn/no-await-expression-member": 0,
"unicorn/no-array-push-push": 0,
"unicorn/filename-case": 0,
"@typescript-eslint/no-unused-vars": 0,
"@typescript-eslint/no-non-null-assertion": 0
}
}
"@typescript-eslint/no-non-null-assertion": 0,
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ Temporary Items
.netlify
.vercel
staticwebapp.config.json
.eslintcache
1,642 changes: 951 additions & 691 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/content/1.guide/1.introduction/5.cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const cachedFn = cachedEventHandler(fn, options);
- `getKey`: A function that accepts same arguments of normal function and should generate cache key. If not provided, a built-in hash function will be used.
- `integrity`: A value that changing it, will invalidate all caches for function. By default will be computed from **function code**.
- `maxAge`: Maximum age that cache is valid in seconds. Default is `1` second.
- `staleMaxAge`: Maximum age that a stale cache is valid in seconds. If set to `-1` a stale value will still be sent to the client, while updating the cache in the background.
- `swr`: Enable Stale-While-Revalidate behavior. Enabled by default.
- `base`: Name of the storage mointpoint to use for caching (`/cache` by default)
- `shouldInvalidateCache`: A function that returns a boolean to invalidate the current cache and create a new one.
- `shouldBypassCache`: A function that returns a boolean to bypass the current cache without invalidating the existing entry.

## Examples

Expand Down
2 changes: 1 addition & 1 deletion docs/content/1.guide/1.introduction/6.assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ All assets in `public/` directory will be automatically served.

## Server Assets

All assets in `server/assets/` directory will be added automatically to the server bundle.
All assets in `assets/` directory will be added automatically to the server bundle.

They can be addressed by the key `assets:server:path:to:asset` using [storage layer](/guide/introduction/storage) and `useStorage`.

Expand Down
4 changes: 4 additions & 0 deletions docs/content/2.deploy/providers/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ description: 'Discover Cloudflare preset for Nitro!'
**Note:** This preset uses [service-worker syntax](https://developers.cloudflare.com/workers/learning/service-worker/) for deployment.
::

::alert{type="warning"}
**Warning:** Please be aware that `runtimeConfig` cannot be updated via Cloudflare's environment variables (see [#272](https://github.com/unjs/nitro/issues/272) for more). As a workaround, you can use the Cloudflare env variables as constants in the code.
::

Login to your [Cloudflare Workers](https://workers.cloudflare.com) account and obtain your `account_id` from the sidebar.

Create a `wrangler.toml` in your root directory:
Expand Down
17 changes: 15 additions & 2 deletions docs/content/2.deploy/providers/digitalocean.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Nitro supports deploying on the [Digital Ocean App Platform](https://docs.digita

[More information](https://docs.digitalocean.com/products/app-platform/how-to/use-environment-variables/).

1. Finally, you will need to ensure you set an `engines.node` field in your app's `package.json` to ensure Digital Ocean uses a supported version of Node.js:
1. You will need to ensure you set an `engines.node` field in your app's `package.json` to ensure Digital Ocean uses a supported version of Node.js:
```json
{
Expand All @@ -30,5 +30,18 @@ Nitro supports deploying on the [Digital Ocean App Platform](https://docs.digita
```
[See more information](https://docs.digitalocean.com/products/app-platform/languages-frameworks/nodejs/#node-version).
You can now follow [the rest of the Digital Ocean deployment guide](https://docs.digitalocean.com/products/app-platform/how-to/manage-deployments/).
1. You'll also need to add a run command so Digital Ocean knows what command to run after a build. You can do so by adding a start script to your `package.json`:

```json
{
"scripts": {
"start": "node .output/server/index.mjs"
}
}
```

1. Finally, you'll need to add this start script to your Digital Ocean app's run command. Go to `Components > Settings > Commands`, click "Edit", then add `npm run start`

Your app should be live at a Digital Ocean generated URL and you can now follow [the rest of the Digital Ocean deployment guide](https://docs.digitalocean.com/products/app-platform/how-to/manage-deployments/).
48 changes: 48 additions & 0 deletions docs/content/2.deploy/providers/edgio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: Edgio
description: 'Discover Edgio (formerly Layer0) preset for Nitro!'
---

**Preset:** `edgio` ([switch to this preset](/deploy/#changing-the-deployment-preset))

Nitro provides a built-in preset to generate output format compatible with [Edgio (formerly Layer0)](https://edg.io/).

Edgio (formerly Layer0) extends the capabilities of a traditional CDN by not only hosting your static content, but also providing server-side rendering for progressive web applications as well as caching both your APIs and HTML at the network edge to provide your users with the fastest browsing experience.

If this is your first time deploying to Edgio, the interactive CLI as part of the `deploy` command will prompt to authenticate using your browser. You may also [sign up](https://app.layer0.co/signup) prior to deployment.

## Install the Edgio CLI

```bash
npm i -g @edgio/cli
```

## Testing production build locally with Edgio

You can use Nitropack to test your app's developement experience locally:

```bash
NITRO_PRESET=edgio npx nitropack build
```

To simulate on local how your app would run in production with Edgio, run the following command:

```bash
edgio build && edgio run --production
```

## Deploying from your local machine

Once you have tested your application locally, you may deploy using:

```bash
edgio deploy
```

## Deploying using CI/CD

If you are deploying from a non-interactive environment, you will need to create an account on [Edgio Developer Console](https://app.layer0.co) first and setup a [deploy token](https://docs.edg.io/guides/basics/deployments#deploy-from-ci). Once the deploy token is created, save it as a secret to your environment. You can start the deploy by running:

```bash
edgio deploy --token=XXX
```
3 changes: 2 additions & 1 deletion docs/content/2.deploy/providers/firebase.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Firebase
description: 'Discover Firebase preset for Nitro!'
description: "Discover Firebase preset for Nitro!"
---

**Preset:** `firebase` ([switch to this preset](/deploy/#changing-the-deployment-preset))
Expand Down Expand Up @@ -45,6 +45,7 @@ yarn global add firebase-tools
npm install -g firebase-tools
```

**Note**: You need to be on [^11.18.0](https://github.com/firebase/firebase-tools/releases/tag/v11.18.0) to deploy a nodejs18 function.

#### Initialize your Firebase project

Expand Down
47 changes: 46 additions & 1 deletion docs/content/2.deploy/providers/heroku.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Heroku
description: 'Discover Herolu preset for Nitro!'
description: 'Discover Heroku preset for Nitro!'
---

**Preset:** `heroku` ([switch to this preset](/deploy/#changing-the-deployment-preset))
Expand Down Expand Up @@ -35,3 +35,48 @@ Nitro supports deploying on [Heroku](https://heroku.com/) with minimal configura
"start": "node .output/server/index.mjs"
}
```
## With Nginx
1. Add the heroku Nginx buildpack [here](https://github.com/heroku/heroku-buildpack-nginx.git)
1. Change to the 'node' preset in your `nuxt.config`
```Json5
"nitro":{
"preset":"node",
}
```
1. From the **Existing app** section of buildpack doc, 2 key steps are required to get things running
Step 1: Listen on a socket at 'tmp/nginx.socket'
Step 2: Create a file '/tmp/app-initialized' when your app is ready to accept connections
1. Create custom app runner, eg: apprunner.mjs at the root of the project (or any other prefered location), in this file, create a server, using the listener generated by the node preset, then listen on the socket as detailed in the buildpack doc
```Js
import { createServer } from 'node:http'
import {listener} from './.output/server/index.mjs'
const server = createServer(listener)
server.listen('/tmp/nginx.socket') //following the buildpack doc
```
1. To create the 'tmp/app-initialized' file, use a nitro plugin, create file 'initServer.ts' at the root of the project (or any other prefered location)
```Js
import fs from "fs"
export default defineNitroPlugin((nitroApp) => {
if((process.env.NODE_ENV || 'development') != 'development')fs.openSync('/tmp/app-initialized', 'w')
})
```
1. Finally, create file 'Procfile' at the root of the project, with the Procfile, we tell heroku to start nginx and use the custom apprunner.mjs to start the server
web: bin/start-nginx node apprunner.mjs
1. Bonus: create file 'config/nginx.conf.erb' to customize your nginx config. With the node preset, by default, static files handlers will not be generated, you can use nginx to server static files, just add the right location rule to the server block(s), or, force the node preset to generate handlers for the static files by setting serveStatic to true
22 changes: 22 additions & 0 deletions docs/content/2.deploy/providers/lagon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Lagon
description: "Discover Lagon preset for Nitro!"
---

> Lagon is an open source platform that allows you to run TypeScript and JavaScript close to your users.
🚧 This is an early access and in progress preset. Please followup via [unjs/nitro#966](https://github.com/unjs/nitro/issues/966)

**Preset:** `lagon` ([switch to this preset](/deploy/#changing-the-deployment-preset))

Nitro supports deploying on [Lagon](https://lagon.app/) with minimal configuration - see [documentation](https://docs.lagon.app/).

## Set up application

1. Build your Nitro app with `NITRO_PRESET=lagon`

1. Install [Lagon CLI](https://docs.lagon.app/cli) and login using `npx @lagon/cli login`.

1. Deploy with `lagon deploy .output/server/index.mjs -p .output/public`. Lagon will ask if you want to link to an existing function or create a new one.

You should be good to go!
45 changes: 1 addition & 44 deletions docs/content/2.deploy/providers/layer0.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
---
title: Layer0
description: 'Discover Layer0 preset for Nitro!'
redirect: /deploy/providers/edgio
---

**Preset:** `layer0` ([switch to this preset](/deploy/#changing-the-deployment-preset))

Nitro provides a built-in preset to generate output format compatible with [Layer0](https://www.layer0.co/).

Layer0 extends the capabilities of a traditional CDN by not only hosting your static content, but also providing server-side rendering for progressive web applications as well as caching both your APIs and HTML at the network edge to provide your users with the fastest browsing experience.

If this is your first time deploying to Layer0, the interactive CLI as part of the `deploy` command will prompt to authenticate using your browser. You may also [sign up](https://app.layer0.co/signup) prior to deployment.

## Testing locally

You can use Layer0 to test your app locally:

```bash
NITRO_PRESET=layer0 yarn build

# .output/server directory
npm install && 0 build && 0 run -p
```

## Deploying from your local machine

Once you have tested your application locally, you may deploy using:

```bash
# .output/server directory
npm install && 0 deploy
```

It is recommended you install Layer0's CLI globally on your machine for a more seamless integration:

```bash
npm i -g @layer0/cli@latest
```

## Deploying using CI/CD

If you are deploying from a non-interactive environment, you will need to create an account on [Layer0 Developer Console](https://app.layer0.co) first and setup a [deploy token](https://docs.layer0.co/guides/deploy_apps#section_deploy_from_ci). Once the deploy token is created, save it as a secret to your environment. You can start the deploy by running:

```bash
0 deploy --token=XXX
```
2 changes: 1 addition & 1 deletion docs/content/2.deploy/providers/netlify.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ For new sites, Netlify will detect that you are using Nitro and set the publish

If you are upgrading an existing site you should check these and update them if needed.

If you want to add custom redirects, you can do so with [`routeRules`](/config#routerules) or by adding a [`_redirects`](https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file) file.
If you want to add custom redirects, you can do so with [`routeRules`](/config#routerules) or by adding a [`_redirects`](https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file) file to your `public` directory.

For deployment, just push to your git repository [as you would normally do for Netlify](https://docs.netlify.com/configure-builds/get-started/).

Expand Down
19 changes: 10 additions & 9 deletions docs/content/3.config/index.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
---
title: Configuration
aside: false
description: 'Customize your Nitro app with a configuration file!'
description: "Customize your Nitro app with a configuration file!"
---

In order to customize nitro's behavior, we create a file named `nitro.config.ts`.

```js
// nitro.config.ts
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from "nitropack";

export default defineNitroConfig({
})
export default defineNitroConfig({});
```

## Config Reference
Expand Down Expand Up @@ -219,11 +218,11 @@ Path to a custom runtime error handler. Replacing nitro's built-in error page.
**Example:**

```js [nitro.config]
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from "nitropack";

export default defineNitroConfig({
errorHandler: '~/error'
})
errorHandler: "~/error",
});
```

```js [error.ts]
Expand Down Expand Up @@ -252,8 +251,10 @@ When `cache` option is set, handlers matching pattern will be automatically wrap
'/blog/**': { static: true },
'/blog/**': { cache: { /* cache options*/ } },
'/assets/**': { headers: { 'cache-control': 's-maxage=0' } },
'/api/v1/**': { cors: true, headers: { 'access-control-allowed-methods': 'GET' } },
'/old-page': { redirect: '/new-page' }
'/api/v1/**': { cors: true, headers: { 'access-control-allow-methods': 'GET' } },
'/old-page': { redirect: '/new-page' },
'/proxy/example': { proxy: 'https://example.com' },
"/proxy/**": { proxy: '/api/**' },
}
}
```
Expand Down
File renamed without changes
File renamed without changes
2 changes: 2 additions & 0 deletions examples/cached-handler/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default defineCachedEventHandler(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return `Response generated at ${new Date().toISOString()} (took 1 second)`;
}, {
shouldBypassCache: (e) => e.node.req.url.includes("preview")
});
Loading

0 comments on commit 1d4a591

Please sign in to comment.