Skip to content

Commit

Permalink
Merge branch 'main' into doc-fix-runtime-hooks-url
Browse files Browse the repository at this point in the history
  • Loading branch information
kspace-trk authored Mar 9, 2024
2 parents 884add6 + b0f08c1 commit 8fb036c
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 456 deletions.
51 changes: 51 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,57 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## v2.9.2

[compare changes](https://github.com/unjs/nitro/compare/v2.9.1...v2.9.2)

### 🩹 Fixes

- **database:** CamelCase the connector name ([#2228](https://github.com/unjs/nitro/pull/2228))
- Respect `imports.autoImport: false` ([#2226](https://github.com/unjs/nitro/pull/2226))
- **server-assets:** Mark `yaml`, `json`, `json5` and `csv` as text ([#2229](https://github.com/unjs/nitro/pull/2229))
- **import-meta:** Import `process` from `node:process` for node compatible builds (deno) ([#2225](https://github.com/unjs/nitro/pull/2225))

### 📖 Documentation

- **deploy:** Add link to zero config providers ([#2206](https://github.com/unjs/nitro/pull/2206))
- **fetch:** Fix typo ([#2209](https://github.com/unjs/nitro/pull/2209))
- Fix typo ([#2211](https://github.com/unjs/nitro/pull/2211))
- Fix typo ([#2205](https://github.com/unjs/nitro/pull/2205))
- Remove lagon ([#2204](https://github.com/unjs/nitro/pull/2204))
- Update url for experimental database feature ([#2210](https://github.com/unjs/nitro/pull/2210))
- **providers:** Improve formatting & use new `undocs` components ([#2202](https://github.com/unjs/nitro/pull/2202))
- Improve cache page ([674089b3](https://github.com/unjs/nitro/commit/674089b3))

### 🏡 Chore

- **release:** V2.9.1 ([d8491cdc](https://github.com/unjs/nitro/commit/d8491cdc))
- **examples:** Update hello-world ([169ec572](https://github.com/unjs/nitro/commit/169ec572))
- Update deps and lockfile ([3875e50d](https://github.com/unjs/nitro/commit/3875e50d))
- Update lockfile ([e3f555ac](https://github.com/unjs/nitro/commit/e3f555ac))
- Remove lagon ([#2203](https://github.com/unjs/nitro/pull/2203))
- Update `hello-world` example ([920b399e](https://github.com/unjs/nitro/commit/920b399e))
- Update lockfile ([08da539f](https://github.com/unjs/nitro/commit/08da539f))
- Improve notes in pr template ([#2212](https://github.com/unjs/nitro/pull/2212))
- Update dependencies ([be2c70be](https://github.com/unjs/nitro/commit/be2c70be))
- Use type import in tests ([1cb410db](https://github.com/unjs/nitro/commit/1cb410db))
- Increase test timeout ([7a65d1ef](https://github.com/unjs/nitro/commit/7a65d1ef))

### ❤️ Contributors

- Pooya Parsa ([@pi0](http://github.com/pi0))
- Divy Srivastava ([@littledivy](http://github.com/littledivy))
- Alexander Lichter <[email protected]>
- Daniel Roe ([@danielroe](http://github.com/danielroe))
- Sébastien Chopin ([@Atinux](http://github.com/Atinux))
- Christian Preston ([@cpreston321](http://github.com/cpreston321))
- Phan Khắc Đạo <[email protected]>
- Maxime Pauvert ([@maximepvrt](http://github.com/maximepvrt))
- Jeff Galbraith ([@hawkeye64](http://github.com/hawkeye64))
- Rajeev R Sharma <[email protected]>
- Julien Vanelian ([@JulienVanelian](http://github.com/JulienVanelian))
- Gangan ([@shinGangan](http://github.com/shinGangan))

## v2.9.1

[compare changes](https://github.com/unjs/nitro/compare/v2.9.0...v2.9.1)
Expand Down
148 changes: 81 additions & 67 deletions docs/1.guide/6.cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ icon: ri:speed-line
## Cached event handlers

To cache an event handler, you've just to use the `defineCachedEventHandler` function.
To cache an event handler, you've just to use the `defineCachedEventHandler` method.

It works like the `defineEventHandler` function but with an additional second [options](#options) parameter.
It works like [`defineEventHandler`](https://h3.unjs.io/guide/event-handler) but with an additional second [options](#options) parameter.

```ts [routes/cached.ts]
// Cache an API handler
Expand All @@ -19,7 +19,6 @@ export default defineCachedEventHandler((event) => {
}, { maxAge: 60 * 60 /* 1 hour */ });
```


With this example, the response will be cached for 1 hour and a stale value will be sent to the client while the cache is being updated in the background. If you want to immediately return the updated response set `swr: false`.

::important
Expand All @@ -28,9 +27,13 @@ All incoming request headers are dropped when handling cached responses. If you

See the [options](#options) section for more details about the available options.

::note
You can also use the `cachedEventHandler` method as alias of `defineCachedEventHandler`.
::

## Cached functions

You can also cache a function using the `cachedFunction` function. This is useful to cache the result of a function that is not an event handler but a part of it and reuse it in multiple handlers.
You can also cache a function using the `defineCachedFunction` function. This is useful to cache the result of a function that is not an event handler but a part of it and reuse it in multiple handlers.

For example, you could want to cache for 1 hour the result of an API call:

Expand Down Expand Up @@ -62,6 +65,10 @@ The stars will be cached in development inside **.nitro/cache/functions/ghStars/
{"expires":1677851092249,"value":43991,"mtime":1677847492540,"integrity":"ZUHcsxCWEH"}
```

::note
You can also use the `cachedFunction` method as alias of `defineCachedFunction`.
::

## Caching route rules

This feature enables you to add caching routes based on a glob pattern directly in the main configuration file. This is especially useful to have a global cache strategy for a part of your application.
Expand Down Expand Up @@ -180,80 +187,87 @@ export default defineNuxtConfig({
```
::


## Options

The `cachedEventHandler` and `cachedFunction` functions accept the following options:

### `name`

- Type: `String`
- Default: Guessed from function name if not provided and fallback to `_` otherwise.

Handler name.

### `group`

- Type: `String`
- Default: `'nitro/handlers'` for handlers and `'nitro/functions'` for functions.

Part of cache name. Useful to organize cache storage.

### `getKey`

- Type: `Function`
- Default: If not provided, a built-in hash function will be used that will generate a key based on the function arguments.

A function that accepts the same arguments of the function and returns a cache key (`String`).

### `integrity`

- Type: `String`
- Default: Computed from **function code**, used in development to invalidate the cache when the function code changes.

A value that invalidates the cache when changed.

### `maxAge`

- Type: `Number`
- Default: `1` (second).

Maximum age that cache is valid in seconds.

### `staleMaxAge`

- Type: `Number`
- Default: `0` (disabled).

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`

- Default: `true`

Enable `stale-while-revalidate` behavior.

### `base`

- Default: `cache`.

Name of the storage mountpoint to use for caching.

### `shouldInvalidateCache`
::field-group
::field{name="base" type="string"}
Name of the storage mountpoint to use for caching, default is `'cache'`.
::
::field{name="name" type="string"}
Guessed from function name if not provided and fallback to `'_'` otherwise.
::
::field{name="group" type="string"}
Default to `'nitro/handlers'` for handlers and `'nitro/functions'` for functions.
::
::field{name="getKey()" type="(...args) => string"}
A function that accepts the same arguments of the function and returns a cache key (`String`). :br
If not provided, a built-in hash function will be used that will generate a key based on the function arguments.
::
::field{name="integrity" type="string"}
By default, it is computed from **function code**, used in development to invalidate the cache when the function code changes.
::
::field{name="integrity" type="string"}
A value that invalidates the cache when changed. :br
By default, it is computed from **function code**, used in development to invalidate the cache when the function code changes.
::
::field{name="maxAge" type="number"}
Maximum age that cache is valid in seconds. :br
Default to `1` (second).
::
::field{name="staleMaxAge" type="number"}
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. :br
Default to `0` (disabled).
::
::field{name="swr" type="boolean"}
Enable `stale-while-revalidate` behavior to serve a stale cached response while asynchronously revalidating it. :br
Default to `true`.
::
::field{name="base" type="string"}
Name of the storage mountpoint to use for caching. :br
Default to `cache`.
::
::field{name="shouldInvalidateCache()" type="(..args) => boolean"}
A function that returns a `boolean` to invalidate the current cache and create a new one.
::
::field{name="shouldBypassCache()" type="(..args) => boolean"}
A function that returns a `boolean` to bypass the current cache without invalidating the existing entry.
::
::field{name="varies" type="string[]"}
An array of request headers to be considered for the cache, [learn more](https://github.com/unjs/nitro/issues/1031).
::
::

- Type: `Function`
## Cache keys and invalidation

A function that returns a `Boolean` to invalidate the current cache and create a new one.
When using the `defineCachedFunction` or `defineCachedEventHandler` functions, the cache key is generated using the following pattern:

### `shouldBypassCache`
```ts
`${options.group}:${options.name}:${options.getKey(...args)}.json`
```

- Type: `Function`
For example, the following function:

A function that returns a boolean to bypass the current cache without invalidating the existing entry.
```ts
const getAccessToken = defineCachedFunction(() => {
return String(Date.now())
}, {
maxAge: 10,
name: 'getAccessToken',
getKey: () => 'default'
})
```

### `varies`
Will generate the following cache key:

- Type: `string[]`
```ts
nitro:functions:getAccessToken:default.json
```

An array of request headers to be considered for the cache
You can invalidate the cached function entry with:

```ts
await useStorage('cache').removeItem('nitro:functions:getAccessToken:default.json')
```
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nitropack",
"version": "2.9.1",
"version": "2.9.2",
"description": "Build and Deploy Universal JavaScript Servers",
"repository": "unjs/nitro",
"license": "MIT",
Expand Down Expand Up @@ -56,7 +56,7 @@
},
"resolutions": {
"nitropack": "link:.",
"undici": "^6.6.2"
"undici": "^6.7.0"
},
"dependencies": {
"@cloudflare/kv-asset-handler": "^0.3.1",
Expand All @@ -72,15 +72,15 @@
"@types/http-proxy": "^1.17.14",
"@vercel/nft": "^0.26.4",
"archiver": "^7.0.0",
"c12": "^1.9.0",
"c12": "^1.10.0",
"chalk": "^5.3.0",
"chokidar": "^3.6.0",
"citty": "^0.1.6",
"consola": "^3.2.3",
"cookie-es": "^1.0.0",
"croner": "^8.0.1",
"crossws": "^0.2.4",
"db0": "^0.1.3",
"db0": "^0.1.4",
"defu": "^6.1.4",
"destr": "^2.0.3",
"dot-prop": "^8.0.2",
Expand All @@ -98,7 +98,7 @@
"klona": "^2.0.6",
"knitwork": "^1.0.0",
"listhen": "^1.7.2",
"magic-string": "^0.30.7",
"magic-string": "^0.30.8",
"mime": "^4.0.1",
"mlly": "^1.6.1",
"mri": "^1.2.0",
Expand All @@ -110,8 +110,8 @@
"perfect-debounce": "^1.0.0",
"pkg-types": "^1.0.3",
"pretty-bytes": "^6.1.1",
"radix3": "^1.1.0",
"rollup": "^4.12.0",
"radix3": "^1.1.1",
"rollup": "^4.12.1",
"rollup-plugin-visualizer": "^5.12.0",
"scule": "^1.3.0",
"semver": "^7.6.0",
Expand All @@ -128,9 +128,9 @@
},
"devDependencies": {
"@azure/functions": "^3.5.1",
"@azure/static-web-apps-cli": "^1.1.6",
"@azure/static-web-apps-cli": "^1.1.7",
"@cloudflare/workers-types": "^4.20240222.0",
"@types/aws-lambda": "^8.10.134",
"@types/aws-lambda": "^8.10.136",
"@types/bun": "^1.0.8",
"@types/estree": "^1.0.5",
"@types/etag": "^1.8.3",
Expand All @@ -148,13 +148,13 @@
"execa": "^8.0.1",
"expect-type": "^0.18.0",
"firebase-admin": "^12.0.0",
"firebase-functions": "^4.7.0",
"firebase-functions": "^4.8.0",
"get-port-please": "^3.1.2",
"miniflare": "^3.20240223.0",
"miniflare": "^3.20240304.0",
"prettier": "^3.2.5",
"typescript": "^5.3.3",
"typescript": "^5.4.2",
"unbuild": "^2.0.0",
"undici": "^6.6.2",
"undici": "^6.7.1",
"vitest": "^1.3.1",
"xml2js": "^0.6.2"
},
Expand All @@ -180,4 +180,4 @@
]
}
}
}
}
Loading

0 comments on commit 8fb036c

Please sign in to comment.