Skip to content
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
4 changes: 2 additions & 2 deletions packages/runtime/plugin-i18n/src/runtime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
type RuntimePlugin,
type TRuntimeContext,
isBrowser,
useRuntimeContext,
} from '@modern-js/runtime';
import { merge } from '@modern-js/runtime-utils/merge';
import type { TInternalRuntimeContext } from '@modern-js/runtime/internal';
import type React from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import type {
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface I18nPluginOptions {
[key: string]: any;
}

interface RuntimeContextWithI18n extends TRuntimeContext {
interface RuntimeContextWithI18n extends TInternalRuntimeContext {
i18nInstance?: I18nInstance;
}

Expand Down
8 changes: 5 additions & 3 deletions packages/runtime/plugin-i18n/src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { TRuntimeContext } from '@modern-js/runtime';
import { isBrowser } from '@modern-js/runtime';
import { getGlobalBasename } from '@modern-js/runtime/context';
import {
type TInternalRuntimeContext,
getGlobalBasename,
} from '@modern-js/runtime/context';

export const getPathname = (context: TRuntimeContext): string => {
export const getPathname = (context: TInternalRuntimeContext): string => {
if (isBrowser()) {
return window.location.pathname;
}
Expand Down
28 changes: 15 additions & 13 deletions packages/runtime/plugin-runtime/src/core/compat/requestContext.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import type { BaseSSRServerContext } from '@modern-js/types';
// this plugin is use to provide request context to runtime context
import type { TRuntimeContext } from '../context';
import type { TInternalRuntimeContext } from '../context';
import type { RuntimePlugin } from '../plugin/types';
import type { RequestContext } from '../types';

export const makeRequestContext = (context: TRuntimeContext) => {
export const makeRequestContext = (
context: TInternalRuntimeContext,
): RequestContext => {
const baseSSRContext = context.ssrContext;
const requestContext = baseSSRContext
? {
isBrowser: context.isBrowser,
request:
baseSSRContext.request || ({} as BaseSSRServerContext['request']),
response:
baseSSRContext.response || ({} as BaseSSRServerContext['response']),
logger: baseSSRContext.logger || ({} as BaseSSRServerContext['logger']),
}
: {};
if (baseSSRContext) {
return {
request: baseSSRContext.request,
response: baseSSRContext.response,
};
}

return requestContext;
return {
request: {} as BaseSSRServerContext['request'],
response: {} as BaseSSRServerContext['response'],
};
};

export const requestContextPlugin = (): RuntimePlugin => ({
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime/plugin-runtime/src/core/context/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { RouteObject } from '@modern-js/runtime-utils/router';
import type { StaticHandlerContext } from '@modern-js/runtime-utils/router';
import type { BaseSSRServerContext } from '@modern-js/types';
import { ROUTE_MANIFEST } from '@modern-js/utils/universal/constants';
import { createContext, useContext } from 'react';
import type { RouteManifest } from '../../router/runtime/types';
import type { SSRServerContext } from '../types';
import type { RequestContext, SSRServerContext } from '../types';

export interface TRuntimeContext {
initialData?: Record<string, unknown>;
isBrowser: boolean;
routes?: RouteObject[];
ssrContext?: SSRServerContext;
context: RequestContext;
[key: string]: unknown;
}

Expand All @@ -20,6 +21,7 @@ export interface TInternalRuntimeContext extends TRuntimeContext {
routeManifest?: RouteManifest;
routerContext?: StaticHandlerContext;
unstable_getBlockNavState?: () => boolean;
ssrContext?: SSRServerContext;
_internalContext?: any;
_internalRouterBaseName?: any;
}
Expand All @@ -43,6 +45,10 @@ export const getInitialContext = (
routeManifest:
routeManifest ||
(typeof window !== 'undefined' && (window as any)[ROUTE_MANIFEST]),
context: {
request: {} as BaseSSRServerContext['request'],
response: {} as BaseSSRServerContext['response'],
},
});

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/plugin-runtime/src/core/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type {
} from '@modern-js/plugin';
import type { Hooks } from '@modern-js/plugin/runtime';
import type { RuntimeConfig as BaseRuntimeConfig } from '../../common';
import type { TRuntimeContext } from '../context/runtime';
import type { TInternalRuntimeContext } from '../context/runtime';

export type RuntimeHooks = Hooks<RuntimeConfig, TRuntimeContext>;
export type RuntimeHooks = Hooks<RuntimeConfig, TInternalRuntimeContext>;

export type RuntimeExtends = Required<
RuntimePluginExtends<RuntimeConfig, TRuntimeContext, {}, {}>
RuntimePluginExtends<RuntimeConfig, TInternalRuntimeContext, {}, {}>
>;

export type RuntimePlugin<Extends extends RuntimePluginExtends = {}> =
Expand Down
37 changes: 32 additions & 5 deletions packages/runtime/plugin-runtime/src/core/react/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import React from 'react';
import type React from 'react';
import {
InternalRuntimeContext,
RuntimeContext,
type TInternalRuntimeContext,
type TRuntimeContext,
} from '../context';

export function wrapRuntimeContextProvider(
App: React.ReactElement,
contextValue: TRuntimeContext,
) {
return React.createElement(
InternalRuntimeContext.Provider,
{ value: contextValue },
React.createElement(RuntimeContext.Provider, { value: contextValue }, App),
const {
isBrowser,
initialData,
routes,
context,
routeManifest,
routerContext,
unstable_getBlockNavState,
ssrContext,
_internalContext,
_internalRouterBaseName,
...rest
} = contextValue as TInternalRuntimeContext;

const runtimeContextValue: TRuntimeContext = {
isBrowser,
initialData,
routes,
context,
...rest,
};

return (
<InternalRuntimeContext.Provider
value={contextValue as TInternalRuntimeContext}
>
<RuntimeContext.Provider value={runtimeContextValue}>
{App}
</RuntimeContext.Provider>
</InternalRuntimeContext.Provider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ function createSSRContext(
resource,
params,
responseProxy,
logger,
metrics,
reporter,
} = options;

Expand Down Expand Up @@ -154,8 +152,6 @@ function createSSRContext(
loaderContext,
htmlModifiers: [],
baseUrl: route.urlPath,
logger,
metrics,
request: {
url: request.url.replace(url.host, host).replace(url.protocol, protocol),
userAgent: headers.get('user-agent')!,
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime/plugin-runtime/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export type SSRServerContext = Pick<
| 'mode'
| 'loaderContext'
| 'reporter'
| 'logger'
| 'metrics'
| 'routeManifest'
> & {
request: BaseSSRServerContext['request'] & {
Expand All @@ -61,3 +59,8 @@ export type SSRServerContext = Pick<
onTiming: OnTiming;
useJsonScript?: boolean;
};

export type RequestContext = {
request: BaseSSRServerContext['request'];
response: BaseSSRServerContext['response'];
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import type {
MouseEventHandler,
TouchEventHandler,
} from 'react';
import { RuntimeContext } from '../../core';
import type { TInternalRuntimeContext } from '../../core/context/runtime';
import { InternalRuntimeContext } from '../../core/context';
import type { RouteAssets, RouteManifest } from './types';

interface PrefetchHandlers {
Expand Down Expand Up @@ -188,7 +187,7 @@ const getDataHref = (

const PrefetchPageLinks: React.FC<{ path: Path }> = ({ path }) => {
const { pathname } = path;
const context = useContext(RuntimeContext) as TInternalRuntimeContext;
const context = useContext(InternalRuntimeContext);
const { routeManifest, routes } = context;
const { routeAssets } = routeManifest || {};
const matches = Array.isArray(routes) ? matchRoutes(routes, pathname) : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,9 @@ export const routerPlugin = (
const _basename =
baseUrl === '/' ? urlJoin(baseUrl, basename) : baseUrl;

const { reporter } = context.ssrContext!;
const requestContext = createRequestContext(
context.ssrContext?.loaderContext,
);
// TODO: we may remove it or put it to other runtime plugins in next version
requestContext.set(reporterCtx, reporter);
const hooks = api.getHooks();

await hooks.onBeforeCreateRoutes.call(context);
Expand Down
5 changes: 0 additions & 5 deletions packages/server/core/src/plugins/monitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ export const injectloggerPlugin = (inputLogger: Logger): ServerPlugin => ({
name: 'inject-logger',

handler: (async (c: Context<ServerEnv>, next) => {
// TODO: remove in next version
if (!c.get('logger')) {
c.set('logger', logger);
}

const pathname = c.req.path;

const loggerMonitor: CoreMonitor = event => {
Expand Down
6 changes: 1 addition & 5 deletions packages/server/core/src/plugins/render/csrRscRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const csrRscRender = async (req: Request, options: SSRRenderOptions) => {
loaderContext,
reporter,
monitors,
logger,
metrics,
onError,
onTiming,
staticGenerate,
Expand Down Expand Up @@ -47,14 +45,12 @@ export const csrRscRender = async (req: Request, options: SSRRenderOptions) => {
rscServerManifest,

locals,
reporter,
staticGenerate,
logger,
metrics,
monitors,

onError,
onTiming,
reporter: reporter,
};

if (!serverBundle) {
Expand Down
2 changes: 0 additions & 2 deletions packages/server/core/src/plugins/render/dataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const dataHandler = async (
{
routeInfo,
serverRoutes,
reporter,
monitors,
onError,
onTiming,
Expand All @@ -29,7 +28,6 @@ export const dataHandler = async (
request,
serverRoutes,
context: {
reporter,
monitors,
loaderContext,
},
Expand Down
5 changes: 1 addition & 4 deletions packages/server/core/src/plugins/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function createRenderHandler(
render: Render,
): Middleware<ServerNodeEnv & ServerEnv> {
return async (c, _) => {
const logger = c.get('logger');
const reporter = c.get('reporter');
const monitors = c.get('monitors');
const templates = c.get('templates') || {};
Expand All @@ -93,10 +92,7 @@ function createRenderHandler(
const res = await render(request, {
nodeReq,
monitors,
logger,
reporter,
templates,
metrics,
serverManifest,
rscServerManifest,
rscClientManifest,
Expand All @@ -106,6 +102,7 @@ function createRenderHandler(
matchPathname,
matchEntryName,
contextForceCSR,
reporter,
});

const { body, status, headers } = res;
Expand Down
3 changes: 1 addition & 2 deletions packages/server/core/src/plugins/render/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export const injectRenderHandlerPlugin = ({
return;
}

const onFallback: OnFallback = async (reason, utils, error) => {
const onFallback: OnFallback = async (reason, error) => {
// For other framework can report ssr fallback reason & error.
await hooks.fallback.call({
reason,
...utils,
error,
});
};
Expand Down
10 changes: 3 additions & 7 deletions packages/server/core/src/plugins/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ export async function createRender({
return async (
req,
{
logger,
reporter,
metrics,
monitors,
nodeReq,
templates,
Expand All @@ -141,6 +138,7 @@ export async function createRender({
matchPathname,
loaderContext,
contextForceCSR,
reporter,
},
) => {
const forMatchpathname = matchPathname ?? getPathname(req);
Expand All @@ -155,7 +153,7 @@ export async function createRender({

const fallbackWrapper: FallbackWrapper = async (reason, error?) => {
fallbackReason = reason;
return onFallback?.(reason, { logger, reporter, metrics }, error);
return onFallback?.(reason, error);
};

if (!routeInfo) {
Expand Down Expand Up @@ -219,11 +217,8 @@ export async function createRender({
config,
nodeReq,
cacheConfig,
reporter,
serverRoutes: routes,
params,
logger,
metrics,
monitors,
locals,
rscClientManifest,
Expand All @@ -233,6 +228,7 @@ export async function createRender({
loaderContext: loaderContext || new Map(),
onError,
onTiming,
reporter,
};

if (fallbackReason) {
Expand Down
Loading