yusukebe / hono-remix-adapter

Hono <-> Remix Adapter
https://www.npmjs.com/package/hono-remix-adapter
145 stars 6 forks source link

Add support for asynchronous `getLoadContext` #21

Open predaytor opened 2 days ago

predaytor commented 2 days ago

Currently, if the async version of the getLoadContext is used, the context will not be propagated.

import { type PlatformProxy } from 'wrangler';
import { defaultLocale } from '../app/lib/lingui';
import { linguiServer } from '../app/lib/lingui.server';

type GetLoadContextArgs = {
    request: Request;
    context: {
        cloudflare: Omit<PlatformProxy<Env>, 'dispose' | 'caches' | 'cf'> & {
            caches: PlatformProxy<Env>['caches'] | CacheStorage;
            cf: Request['cf'];
        };
    };
};

declare module '@remix-run/cloudflare' {
    interface AppLoadContext extends Awaited<ReturnType<typeof getLoadContext>> {}
}

export async function getLoadContext({ context, request }: GetLoadContextArgs) {
    return {
        ...context,
        locale: await linguiServer.getLocale(request) ?? defaultLocale,
    };
}

For example, cloudflareDevProxyVitePlugin({ getLoadContext }) from @remix-run/dev supports asynchronous callback.