-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { ContentScope } from "@src/site-configs"; | ||
import { cache } from "react"; | ||
|
||
// https://github.com/vercel/next.js/discussions/43179#discussioncomment-11192893 | ||
/** | ||
* This is a workaround until Next.js adds support for true Server Context | ||
* It works by using React `cache` to store the value for the lifetime of one rendering. | ||
* Meaning it's available to all server components down the tree after it's set. | ||
* Do not use this unless necessary. | ||
* | ||
* @warning This is a temporary workaround. | ||
*/ | ||
function createServerContext<T>(defaultValue: T): [() => T, (v: T) => void] { | ||
const getRef = cache(() => ({ current: defaultValue })); | ||
|
||
const getValue = (): T => getRef().current; | ||
|
||
const setValue = (value: T) => { | ||
getRef().current = value; | ||
}; | ||
|
||
return [getValue, setValue]; | ||
} | ||
|
||
export const [getNotFoundContext, setNotFoundContext] = createServerContext<ContentScope | null>(null); |