-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
Script
and HasIsland
available at multiple runtimes with `A…
…syncLocalStorage` (#168) * feat: Use AsyncLocalStorage to share context in same request * feat: replace reactApiImportSource also in server components * feat: use `any` instead of `FC` for components for multi-runtime compatibility * test: add test for server/components
- Loading branch information
Showing
8 changed files
with
86 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import type { FC } from 'hono/jsx' | ||
import { useRequestContext } from 'hono/jsx-renderer' | ||
import { IMPORTING_ISLANDS_ID } from '../../constants.js' | ||
import { contextStorage } from '../context-storage.js' | ||
|
||
export const HasIslands: FC = ({ children }) => { | ||
const c = useRequestContext() | ||
return <>{c.get(IMPORTING_ISLANDS_ID) ? children : <></>}</> | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const HasIslands = ({ children }: { children: any }): any => { | ||
const c = contextStorage.getStore() | ||
if (!c) { | ||
throw new Error('No context found') | ||
} | ||
return <>{c.get(IMPORTING_ISLANDS_ID) && children}</> | ||
} |
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,3 @@ | ||
import { AsyncLocalStorage } from 'node:async_hooks' | ||
import type { Context } from 'hono' | ||
export const contextStorage = new AsyncLocalStorage<Context>() |
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
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