-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: inject renderHandler to appContext & add default serverPlugins (#…
- Loading branch information
1 parent
c89066a
commit 0e906a1
Showing
11 changed files
with
186 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@modern-js/prod-server': patch | ||
'@modern-js/plugin-bff': patch | ||
'@modern-js/server-core': patch | ||
--- | ||
|
||
feat: inject renderHandler to appContext & add default serverPlugins | ||
feat: 注入 renderHandler 到 appContext & 新增默认 serverPlugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Logger } from '@modern-js/types'; | ||
import type { ServerPlugin } from '../types'; | ||
import { | ||
InjectRenderHandlerOptions, | ||
injectRenderHandlerPlugin, | ||
} from './render'; | ||
import { | ||
initMonitorsPlugin, | ||
injectloggerPluigin, | ||
injectServerTiming, | ||
} from './monitors'; | ||
import { processedByPlugin } from './processedBy'; | ||
import { logPlugin } from './log'; | ||
import { faviconPlugin } from './favicon'; | ||
|
||
export type CreateDefaultPluginsOptions = InjectRenderHandlerOptions & { | ||
logger?: Logger; | ||
}; | ||
|
||
export function createDefaultPlugins( | ||
options: CreateDefaultPluginsOptions = {}, | ||
) { | ||
const plugins: ServerPlugin[] = [ | ||
initMonitorsPlugin(), | ||
injectRenderHandlerPlugin(options), | ||
injectloggerPluigin(options.logger), | ||
injectServerTiming(), | ||
logPlugin(), | ||
processedByPlugin(), | ||
faviconPlugin(), | ||
]; | ||
|
||
return plugins; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
export { | ||
renderPlugin, | ||
injectRenderHandlerPlugin, | ||
type InjectRenderHandlerOptions, | ||
getRenderHandler, | ||
type RenderPluginOptions, | ||
type GetRenderHandlerOptions, | ||
} from './render'; | ||
export { faviconPlugin } from './favicon'; | ||
export { processedByPlugin } from './processedBy'; | ||
export { getLoaderCtx } from './customServer'; | ||
export { logPlugin } from './log'; | ||
export { injectServerTiming, injectloggerPluigin } from './monitors'; | ||
export { | ||
initMonitorsPlugin, | ||
injectServerTiming, | ||
injectloggerPluigin, | ||
} from './monitors'; | ||
createDefaultPlugins, | ||
type CreateDefaultPluginsOptions, | ||
} from './default'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { | ||
CacheConfig, | ||
GetRenderHandlerOptions, | ||
Render, | ||
ServerPlugin, | ||
} from '../../types'; | ||
import { createRender } from './render'; | ||
|
||
export interface InjectRenderHandlerOptions { | ||
staticGenerate?: boolean; | ||
cacheConfig?: CacheConfig; | ||
} | ||
|
||
export const injectRenderHandlerPlugin = ({ | ||
staticGenerate, | ||
cacheConfig, | ||
}: InjectRenderHandlerOptions): ServerPlugin => ({ | ||
name: '@modern-js/plugin-inject-render', | ||
setup(api) { | ||
return { | ||
async prepare() { | ||
const { distDirectory: pwd, routes, metaName } = api.useAppContext(); | ||
|
||
const config = api.useConfigContext(); | ||
|
||
if (!routes) { | ||
return; | ||
} | ||
|
||
const getRenderHandlerOptions: GetRenderHandlerOptions = { | ||
pwd, | ||
routes, | ||
config, | ||
metaName, | ||
cacheConfig: config.render?.cache || cacheConfig, | ||
staticGenerate, | ||
}; | ||
|
||
const render = await getRenderHandler(getRenderHandlerOptions); | ||
|
||
api.setAppContext({ | ||
...api.useAppContext(), | ||
render, | ||
getRenderOptions: getRenderHandlerOptions, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export async function getRenderHandler({ | ||
pwd, | ||
routes, | ||
config, | ||
cacheConfig, | ||
metaName, | ||
staticGenerate, | ||
}: GetRenderHandlerOptions): Promise<Render> { | ||
const ssrConfig = config.server?.ssr; | ||
const forceCSR = typeof ssrConfig === 'object' ? ssrConfig.forceCSR : false; | ||
|
||
const render = createRender({ | ||
routes, | ||
pwd, | ||
config, | ||
staticGenerate, | ||
cacheConfig, | ||
forceCSR, | ||
nonce: config.security?.nonce, | ||
metaName: metaName || 'modern-js', | ||
}); | ||
|
||
return render; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.