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
23 changes: 13 additions & 10 deletions packages/runtime/plugin-runtime/src/cli/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ import { resolveSSRMode } from './ssr/mode';
import * as template from './template';
import * as serverTemplate from './template.server';

function getSSRMode(
entry: string,
config: AppToolsNormalizedConfig,
): 'string' | 'stream' | false {
return resolveSSRMode({ entry, config });
}

export const generateCode = async (
entrypoints: Entrypoint[],
appContext: AppToolsContext,
Expand All @@ -45,15 +38,25 @@ export const generateCode = async (
} = appContext;
await Promise.all(
entrypoints.map(async entrypoint => {
const { entryName, isAutoMount, entry, customEntry, customServerEntry } =
entrypoint;
const {
entryName,
isAutoMount,
entry,
customEntry,
customServerEntry,
nestedRoutesEntry,
} = entrypoint;
const { plugins: runtimePlugins } =
await hooks._internalRuntimePlugins.call({
entrypoint,
plugins: [],
});
if (isAutoMount) {
const ssrMode = getSSRMode(entryName, config);
const ssrMode = resolveSSRMode({
entry: entryName,
config,
nestedRoutesEntry,
});
let indexCode = '';
// index.jsx
if (!ssrMode && config.server.rsc) {
Expand Down
29 changes: 25 additions & 4 deletions packages/runtime/plugin-runtime/src/cli/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ServerUserConfig,
} from '@modern-js/app-tools';
import type { CLIPluginAPI } from '@modern-js/plugin';
import type { Entrypoint } from '@modern-js/types';
import { LOADABLE_STATS_FILE, isUseSSRBundle } from '@modern-js/utils';
import type { RsbuildPlugin } from '@rsbuild/core';
import { resolveSSRMode } from './mode';
Expand Down Expand Up @@ -43,12 +44,32 @@ const hasStringSSREntry = (userConfig: AppToolsNormalizedConfig): boolean => {
return false;
};

/**
* Check if any entry uses string SSR mode.
* Returns true if at least one entry uses 'string' SSR mode.
*/
const checkUseStringSSR = (
config: AppToolsNormalizedConfig,
appDirectory?: string,
entrypoints?: Entrypoint[],
): boolean => {
const ssrMode = resolveSSRMode({ config, appDirectory });
return ssrMode === 'string';
// If entrypoints are provided, check each entry
if (entrypoints && entrypoints.length > 0) {
for (const entrypoint of entrypoints) {
const ssrMode = resolveSSRMode({
entry: entrypoint.entryName,
config,
appDirectory,
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
});
if (ssrMode === 'string') {
return true;
}
}
return false;
}

return true;
};

const ssrBuilderPlugin = (
Expand All @@ -70,12 +91,12 @@ const ssrBuilderPlugin = (
: 'node';

const appContext = modernAPI.getAppContext();
const { appDirectory } = appContext;
const { appDirectory, entrypoints } = appContext;

const useLoadablePlugin =
isUseSSRBundle(userConfig) &&
!isServerEnvironment &&
checkUseStringSSR(userConfig, appDirectory);
checkUseStringSSR(userConfig, appDirectory, entrypoints);

return mergeEnvironmentConfig(config, {
source: {
Expand Down
17 changes: 5 additions & 12 deletions packages/runtime/plugin-runtime/src/cli/ssr/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export function resolveSSRMode(params: {
entry?: string;
config: AppToolsNormalizedConfig;
appDirectory?: string;
nestedRoutesEntry?: string;
}): SSRMode {
const { entry, config, appDirectory } = params;
const { entry, config, appDirectory, nestedRoutesEntry } = params;

// 1. Check if SSG is enabled first (SSG takes precedence over SSR when both are configured)
const isSsgEnabled =
Expand All @@ -29,19 +30,11 @@ export function resolveSSRMode(params: {
: Object.keys(config.output.ssgByEntries).length > 0));

if (isSsgEnabled) {
// If user explicitly disables conventional routing (non-conventional routing), force 'string'
const entryRouterConfig = entry
? config.runtimeByEntries?.[entry]?.router
: undefined;
const routerConfig =
entryRouterConfig !== undefined
? entryRouterConfig
: config.runtime?.router;

if (!routerConfig) {
if (nestedRoutesEntry) {
return 'stream';
} else {
return 'string';
}

if (appDirectory) {
return isReact18(appDirectory) ? 'stream' : 'string';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export const generateCode = async (
entry: entrypoint.entryName,
config,
appDirectory: appContext.appDirectory,
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
});

if (ssrMode === 'stream') {
Expand Down