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

feat: add useRuntimeContext api data structure #6496

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions packages/runtime/plugin-runtime/src/core/compatible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createLoaderManager } from './loader/loaderManager';
import { type Plugin, registerPlugin, type runtime } from './plugin';
import { getGlobalRunner } from './plugin/runner';
import { wrapRuntimeContextProvider } from './react/wrapper';
import type { TSSRContext } from './types';

const IS_REACT18 = process.env.IS_REACT18 === 'true';

Expand Down Expand Up @@ -226,8 +227,20 @@ export const bootstrap: BootStrap = async (
export const useRuntimeContext = () => {
const context = useContext(RuntimeReactContext);

const baseSSRContext = context.ssrContext;
const tSSRContext = baseSSRContext
? {
isBrowser: context.isBrowser,
request: baseSSRContext.request || ({} as TSSRContext['request']),
response: baseSSRContext.response || ({} as TSSRContext['response']),
logger: baseSSRContext.logger || ({} as TSSRContext['logger']),
}
: ({} as TSSRContext);

// TODO: Here we should not provide all the RuntimeReactContext to the user
const pickedContext: TRuntimeContext = {
...context,
context: tSSRContext,
request: context.ssrContext?.request,
response: context.ssrContext?.response,
};
Expand Down
15 changes: 7 additions & 8 deletions packages/runtime/plugin-runtime/src/core/context/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { createContext } from 'react';
import type { RouteManifest } from '../../router/runtime/types';
import { createLoaderManager } from '../loader/loaderManager';
import type { PluginRunner, runtime } from '../plugin';
import type { SSRServerContext } from '../types';
import type { SSRServerContext, TSSRContext } from '../types';

export interface BaseRuntimeContext {
interface BaseRuntimeContext {
initialData?: Record<string, unknown>;
loaderManager: ReturnType<typeof createLoaderManager>;
isBrowser: boolean;
runner: ReturnType<typeof runtime.init>;
// ssr type
ssrContext?: SSRServerContext;
Expand All @@ -39,10 +40,12 @@ export const RuntimeReactContext = createContext<RuntimeContext>({} as any);

export const ServerRouterContext = createContext({} as any);

export interface BaseTRuntimeContext extends Partial<BaseRuntimeContext> {
export interface TRuntimeContext extends Partial<BaseRuntimeContext> {
initialData?: Record<string, unknown>;
// ssr type
context: TSSRContext;
/** @deprecated use context.request field instead */
request?: SSRServerContext['request'];
/** @deprecated use context.response field instead */
response?: SSRServerContext['response'];
// store type
store?: Store;
Expand All @@ -52,10 +55,6 @@ export interface BaseTRuntimeContext extends Partial<BaseRuntimeContext> {
};
}

export interface TRuntimeContext extends BaseTRuntimeContext {
[key: string]: any;
}

export const getInitialContext = (
runner: PluginRunner,
isBrowser = true,
Expand Down
22 changes: 22 additions & 0 deletions packages/runtime/plugin-runtime/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface SSRContainer {

type BuildHtmlCb = (tempalte: string) => string;

/* 在服务端获取的 SSRContext */
export type SSRServerContext = Pick<
BaseSSRServerContext,
| 'redirection'
Expand All @@ -70,3 +71,24 @@ export type SSRServerContext = Pick<
onError?: (e: unknown) => void;
onTiming?: (name: string, dur: number) => void;
};

/* 通过 useRuntimeContext 获取的 SSRContext */
interface TSSRBaseContext {
request: BaseSSRServerContext['request'] & {
userAgent: string;
cookie: string;
};
[propName: string]: any;
}

interface ServerContext extends TSSRBaseContext {
isBrowser: false;
response: BaseSSRServerContext['response'];
logger: BaseSSRServerContext['logger'];
}

interface ClientContext extends TSSRBaseContext {
isBrowser: true;
}

export declare type TSSRContext = ServerContext | ClientContext;
6 changes: 1 addition & 5 deletions packages/runtime/plugin-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ export type { Plugin } from './core';
export type { AppConfig, RuntimeConfig } from './common';
export { isBrowser } from './common';

export type {
BaseRuntimeContext,
RuntimeContext,
BaseTRuntimeContext,
} from './core/context/runtime';
export type { RuntimeContext } from './core/context/runtime';
export type { RuntimeUserConfig } from './config';

export {
Expand Down
Loading