Unverified Commit 72b52e7c authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

docs: update ISR self-hosting example code (#74211)

Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 790efc59
...@@ -220,11 +220,15 @@ module.exports = class CacheHandler { ...@@ -220,11 +220,15 @@ module.exports = class CacheHandler {
// Iterate over all entries in the cache // Iterate over all entries in the cache
for (let [key, value] of cache) { for (let [key, value] of cache) {
// If the value's tags include the specified tag, delete this entry // If the value's tags include the specified tag, delete this entry
if (value.tags.some((tag) => tags.include(tag))) { if (value.tags.some((tag) => tags.includes(tag))) {
cache.delete(key) cache.delete(key)
} }
} }
} }
// If you want to have temporary in memory cache for a single request that is reset
// before the next request you can leverage this method
public resetRequestCache() {}
} }
``` ```
...@@ -311,49 +315,50 @@ If you want to use `after` on custom infrastructure, check your provider documen ...@@ -311,49 +315,50 @@ If you want to use `after` on custom infrastructure, check your provider documen
<summary>Reference: supporting `after` for serverless platforms</summary> <summary>Reference: supporting `after` for serverless platforms</summary>
Using `after` in a serverless context requires waiting for asynchronous tasks to finish after the response has been sent. In Next.js and Vercel, this is achieved using a primitive called `waitUntil(promise)`, which extends the lifetime of a serverless invocation until all promises passed to [`waitUntil`](https://vercel.com/docs/functions/functions-api-reference#waituntil) have settled. Using `after` in a serverless context requires waiting for asynchronous tasks to finish after the response has been sent. In Next.js and Vercel, this is achieved using a primitive called `waitUntil(promise)`, which extends the lifetime of a serverless invocation until all promises passed to [`waitUntil`](https://vercel.com/docs/functions/functions-api-reference#waituntil) have settled.
If you want your users to be able to run `after`, you will have to provide your implementation of `waitUntil` that behaves in an analogous way. If you want your users to be able to run `after`, you will have to provide your implementation of `waitUntil` that behaves in an analogous way.
When `after` is called, Next.js will access `waitUntil` like this: When `after` is called, Next.js will access `waitUntil` like this:
```jsx ```jsx
const RequestContext = globalThis[Symbol.for('@next/request-context')] const RequestContext = globalThis[Symbol.for('@next/request-context')]
const contextValue = RequestContext?.get() const contextValue = RequestContext?.get()
const waitUntil = context?.waitUntil const waitUntil = context?.waitUntil
``` ```
Which means that `globalThis[Symbol.for('@next/request-context')]` is expected to contain an object like this: Which means that `globalThis[Symbol.for('@next/request-context')]` is expected to contain an object like this:
```tsx ```tsx
type NextRequestContext = { type NextRequestContext = {
get(): NextRequestContextValue | undefined get(): NextRequestContextValue | undefined
} }
type NextRequestContextValue = { type NextRequestContextValue = {
waitUntil?: (promise: Promise<any>) => void waitUntil?: (promise: Promise<any>) => void
} }
``` ```
Here is an example of the implementation. Here is an example of the implementation.
```tsx ```tsx
import { AsyncLocalStorage } from 'node:async_hooks' import { AsyncLocalStorage } from 'node:async_hooks'
const RequestContextStorage = new AsyncLocalStorage<NextRequestContextValue>() const RequestContextStorage = new AsyncLocalStorage<NextRequestContextValue>()
// Define and inject the accessor that next.js will use // Define and inject the accessor that next.js will use
const RequestContext: NextRequestContext = { const RequestContext: NextRequestContext = {
get() { get() {
return RequestContextStorage.getStore() return RequestContextStorage.getStore()
}, },
} }
globalThis[Symbol.for('@next/request-context')] = RequestContext globalThis[Symbol.for('@next/request-context')] = RequestContext
const handler = (req, res) => { const handler = (req, res) => {
const contextValue = { waitUntil: YOUR_WAITUNTIL } const contextValue = { waitUntil: YOUR_WAITUNTIL }
// Provide the value // Provide the value
return RequestContextStorage.run(contextValue, () => nextJsHandler(req, res)) return RequestContextStorage.run(contextValue, () => nextJsHandler(req, res))
} }
``` ```
</details> </details>
</AppOnly> </AppOnly>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment