Skip to content

Commit

Permalink
feat: move setupAppConfigMiddleware to platform-web (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
Repraance authored Dec 28, 2023
1 parent 6b188d3 commit 876d946
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 22 deletions.
14 changes: 12 additions & 2 deletions packages/platform-web/src/node/features/html-render/serverHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { ServerResponse } from 'http';
import {
createAsyncParallelHook,
createAsyncSeriesHook,
createAsyncSeriesWaterfallHook
createAsyncSeriesWaterfallHook,
createSyncBailHook
} from '@shuvi/hook';
import { ShuviRequest } from '@shuvi/service';
import { IServerPluginContext, ShuviRequest } from '@shuvi/service';
import { IAppContext } from '@shuvi/platform-shared/shared';
import { IHtmlDocument } from '../html-render';

Expand All @@ -28,6 +29,12 @@ export type ISendHtml = (
requestContext: RequestContext
) => Promise<void>;

type AppConfigCtx = {
req: ShuviRequest;
};

type AppConfig = IServerPluginContext['appConfig'];

const getPageData = createAsyncParallelHook<
void,
IAppContext,
Expand All @@ -37,8 +44,11 @@ const handlePageRequest = createAsyncSeriesWaterfallHook<IHandlePageRequest>();
const modifyHtml = createAsyncSeriesHook<IHtmlDocument, ModifyHtmlContext>();
const sendHtml = createAsyncSeriesWaterfallHook<ISendHtml>();

export const getAppConfig = createSyncBailHook<void, AppConfigCtx, AppConfig>();

export const extendedHooks = {
getPageData,
getAppConfig,
handlePageRequest,
modifyHtml,
sendHtml
Expand Down
2 changes: 2 additions & 0 deletions packages/platform-web/src/node/features/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { DevMiddleware } from '@shuvi/service/lib/server/middlewares/dev/devMidd
import { OnDemandRouteManager } from './on-demand-compile-page';
import { getApiMiddleware, getMiddlewareMiddleware } from './filesystem-routes';
import { getPageMiddleware } from './html-render';
import { getSetupAppConfigMiddleware } from './setup-app-config';

export const getMiddlewares = async (
context: IServerPluginContext
): Promise<IServerMiddleware[]> => {
return [
getMiddlewareMiddleware(context),
getApiMiddleware(context),
getSetupAppConfigMiddleware(context),
await getPageMiddleware(context)
].filter(Boolean);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { IServerPluginContext } from '../plugin';
import { ShuviRequestHandler } from '../shuviServerTypes';
import { IServerPluginContext, ShuviRequestHandler } from '@shuvi/service';

export const setupAppConfigMiddleware = (
export const getSetupAppConfigMiddleware = (
context: IServerPluginContext
): ShuviRequestHandler => {
return async (req, _res, next) => {
return (req, _res, next) => {
const appConfig = context.serverPluginRunner.getAppConfig({ req });
if (appConfig) {
if (typeof appConfig.router.basename !== 'string') {
Expand Down
1 change: 1 addition & 0 deletions packages/platform-web/src/node/shuvi-runtime-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare global {
namespace ShuviService {
interface CustomServerPluginHooks {
getPageData: typeof extendedHooks.getPageData;
getAppConfig: typeof extendedHooks.getAppConfig;
handlePageRequest: typeof extendedHooks.handlePageRequest;
modifyHtml: typeof extendedHooks.modifyHtml;
sendHtml: typeof extendedHooks.sendHtml;
Expand Down
13 changes: 2 additions & 11 deletions packages/service/src/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
createAsyncParallelHook,
createHookManager,
createSyncBailHook,
IPluginInstance,
IPluginHandlers,
HookMap
} from '@shuvi/hook';
import { createPluginCreator } from '@shuvi/shared/plugins';
import { IPluginContext } from '../core';
import { CustomServerPluginHooks } from './pluginTypes';
import { ShuviRequest } from './shuviServerTypes';

export * from './pluginTypes';

Expand All @@ -24,25 +22,18 @@ export type PluginRunner = PluginManager['runner'];

const listen = createAsyncParallelHook<{ port: number; hostname?: string }>();

type AppConfigCtx = {
req: ShuviRequest;
};

type AppConfig = {
router: {
basename: string;
};
};
const getAppConfig = createSyncBailHook<void, AppConfigCtx, AppConfig>();

const internalHooks = {
listen,
getAppConfig
listen
};

export interface BuiltInServerPluginHooks extends HookMap {
listen: typeof listen;
getAppConfig: typeof getAppConfig;
}

export interface ServerPluginHooks
Expand Down Expand Up @@ -80,7 +71,7 @@ export const initServerPlugins = (
const serverContext = Object.assign(
{
serverPluginRunner: manager.runner,
// default appConfig, can be override by setupAppConfigMiddleware
// default appConfig, can be override by `getAppConfig` hook
appConfig: {
router: {
basename: ''
Expand Down
3 changes: 0 additions & 3 deletions packages/service/src/server/shuviDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { applyHttpProxyMiddleware } from './middlewares/httpProxyMiddleware';
import { getAssetMiddleware } from './middlewares/getAssetMiddleware';
import { ShuviDevServerOptions, ShuviRequestHandler } from './shuviServerTypes';
import { loadDotenvConfig } from '../config/env';
import { setupAppConfigMiddleware } from './middlewares/setupAppConfigMiddleware';

export class ShuviDevServer extends ShuviServer {
private _bundler: Bundler;
Expand Down Expand Up @@ -61,8 +60,6 @@ export class ShuviDevServer extends ShuviServer {
next();
}) as ShuviRequestHandler);

server.use(setupAppConfigMiddleware(context));

if (this._options.getMiddlewaresBeforeDevMiddlewares) {
const serverMiddlewaresBeforeDevMiddleware = [
this._options.getMiddlewaresBeforeDevMiddlewares(devMiddleware, context)
Expand Down
2 changes: 0 additions & 2 deletions packages/service/src/server/shuviProdServer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ShuviServer } from './shuviServer';
import { getAssetMiddleware } from './middlewares/getAssetMiddleware';
import { setupAppConfigMiddleware } from './middlewares/setupAppConfigMiddleware';
export class ShuviProdServer extends ShuviServer {
async init() {
const { _serverContext: context } = this;
this._server.use(getAssetMiddleware(context));
this._server.use(setupAppConfigMiddleware(context));
await this._initMiddlewares();
}
}

0 comments on commit 876d946

Please sign in to comment.