Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cloudflare guide to use importHonoContextStorage #897

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/guides/cloudflare.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,23 @@ Waku currently uses [Hono](https://hono.dev/) in the [advanced mode worker funct
Waku uses Hono's [context storage middleware](https://hono.dev/docs/middleware/builtin/context-storage) to make the [Hono context](https://hono.dev/docs/api/context) accessible from anywhere. You can access Cloudflare bindings and execution context from the Hono context:

```ts
import { getContext } from 'hono/context-storage';
import { importHonoContextStorage } from 'waku/unstable_hono';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. It doesn't work with cloudflare case either.
Well, as you may guess, we don't like this. It's far better to request users to install hono, which should allow the normal one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#898 should fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'm looking forward to trying #898. It is interesting how you used a dynamic import and an empty virtual module to fix it.

Copy link
Owner

@dai-shi dai-shi Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was actually not very nice. I'm trying a different workaround in #902.


const getHonoContext = async () => {
try {
const { getContext } = await importHonoContextStorage();
const c = getContext<{ Bindings: Env }>();
if (!c) {
return null;
}
return c;
} catch (e) {
return null;
}
};

const getData = async () => {
const c = getContext<{ Bindings: Env }>();
const c = await getHonoContext();
if (!c) {
return null;
}
Expand Down Expand Up @@ -76,8 +89,8 @@ You can access static assets from your server code. For example, if you want to

```ts
const get404Html = async () => {
const c = getContext<{ Bindings: Env }>();
return c.env.ASSETS
const c = await getHonoContext();
return c?.env.ASSETS
? await (await c.env.ASSETS.fetch('https://example.com/404.html')).text()
: '';
};
Expand Down
Loading