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
7 changes: 2 additions & 5 deletions packages/runtime/plugin-i18n/src/runtime/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@ export const useModernI18n = (
// Update i18n instance
await i18nInstance.changeLanguage(newLang);

// Update context language state
if (updateLanguage) {
updateLanguage(newLang);
}

// Update URL if locale detection is enabled, we're in browser, and router is available
if (
enableLocaleDetection &&
Expand Down Expand Up @@ -174,6 +169,8 @@ export const useModernI18n = (

// Use history API to navigate without page reload
window.history.pushState(null, '', newUrl);
} else if (updateLanguage) {
updateLanguage(newLang);
}
} catch (error) {
console.error('Failed to change language:', error);
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime/plugin-i18n/src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { getGlobalBasename } from '@modern-js/runtime/context';
import { MAIN_ENTRY_NAME } from '@modern-js/utils/universal/constants';

export const getEntryPath = (entryName: string = MAIN_ENTRY_NAME) => {
return entryName === MAIN_ENTRY_NAME ? '' : `/${entryName}`;
export const getEntryPath = (entryName?: string): string => {
const basename = getGlobalBasename();
if (basename) {
return basename === '/' ? '' : basename;
}
return '';
};
/**
* Helper function to get language from current pathname
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime/plugin-runtime/src/cli/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,16 @@ export const generateCode = async (
// runtime-global-context.js
let contextCode = '';
if (!config.server.rsc || entrypoint.nestedRoutesEntry) {
const route = serverRoutes.find(r => r.entryName === entryName);
const basename = route?.urlPath || '/';
contextCode = template.runtimeGlobalContext({
entryName,
srcDirectory,
internalSrcAlias,
metaName,
entry,
customEntry,
basename,
});
} else {
const AppProxyPath = path.join(
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime/plugin-runtime/src/cli/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ export const runtimeGlobalContext = ({
metaName,
entry,
customEntry,
basename,
}: {
entryName: string;
srcDirectory: string;
internalSrcAlias: string;
metaName: string;
entry: string;
customEntry?: boolean;
basename?: string;
}) => {
return `import { setGlobalContext } from '@${metaName}/runtime/context'

Expand All @@ -289,9 +291,11 @@ import App from '${
}';

const entryName = '${entryName}';
const basename = '${basename || '/'}';
setGlobalContext({
entryName,
App,
basename,
});`;
};

Expand Down
9 changes: 9 additions & 0 deletions packages/runtime/plugin-runtime/src/core/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ interface GlobalContext {
* page router _app.tsx export layout app
*/
layoutApp?: React.ComponentType;
/**
* Entry basename for routing
*/
basename?: string;

internalRuntimeContext?: InternalRuntimeContext<RuntimeExtends>;
/**
Expand Down Expand Up @@ -64,6 +68,7 @@ export function setGlobalContext(
globalContext.routes = context.routes;
globalContext.appInit = context.appInit;
globalContext.layoutApp = context.layoutApp;
globalContext.basename = context.basename;
globalContext.RSCRoot = context.RSCRoot;
globalContext.isRscClient = context.isRscClient;
globalContext.enableRsc = context.enableRsc;
Expand Down Expand Up @@ -98,3 +103,7 @@ export function getGlobalRoutes(): undefined | (NestedRoute | PageRoute)[] {
export function getGlobalLayoutApp() {
return globalContext.layoutApp;
}

export function getGlobalBasename() {
return globalContext.basename;
}
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ export const runtimeGlobalContext = async ({
internalSrcAlias,
globalApp,
rscType = false,
basename,
}: {
entryName: string;
metaName: string;
Expand All @@ -566,6 +567,7 @@ export const runtimeGlobalContext = async ({
internalSrcAlias: string;
globalApp?: string | false;
rscType?: 'server' | 'client' | false;
basename?: string;
}) => {
const imports = [
`import { setGlobalContext } from '@${metaName}/runtime/context';`,
Expand Down Expand Up @@ -619,11 +621,13 @@ export const runtimeGlobalContext = async ({
import { routes } from './routes';

const entryName = '${entryName}';
const basename = '${basename || '/'}';
setGlobalContext({
entryName,
layoutApp,
routes,
appInit,
basename,
isRscClient: true,
enableRsc: true,
});
Expand All @@ -634,11 +638,13 @@ export const runtimeGlobalContext = async ({
import { routes } from './routes';

const entryName = '${entryName}';
const basename = '${basename || '/'}';
setGlobalContext({
entryName,
layoutApp,
routes,
appInit,
basename,
enableRsc: ${enableRsc},
});
`;
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/plugin-runtime/src/router/cli/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export async function handleGeneratorEntryCode(
await Promise.all(
entrypoints.map(async entrypoint => {
if (entrypoint.nestedRoutesEntry || entrypoint.pageRoutesEntry) {
const route = appContext.serverRoutes.find(
r => r.entryName === entrypoint.entryName,
);
const basename = route?.urlPath || '/';
generatorRegisterCode(
internalDirectory,
entrypoint.entryName,
Expand All @@ -45,6 +49,7 @@ export async function handleGeneratorEntryCode(
internalSrcAlias: appContext.internalSrcAlias,
globalApp: entrypoint.fileSystemRoutes?.globalApp,
rscType: enableRsc ? 'client' : undefined,
basename,
}),
);
if (enableRsc) {
Expand All @@ -59,6 +64,7 @@ export async function handleGeneratorEntryCode(
internalSrcAlias: appContext.internalSrcAlias,
globalApp: entrypoint.fileSystemRoutes?.globalApp,
rscType: 'server',
basename,
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import type {
RsbuildPlugin,
RspackChain,
} from '@rsbuild/core';
import type { HtmlUserConfig } from '../../../types/config/html';
import { BottomTemplatePlugin } from '../bundlerPlugins';
import type { BuilderOptions } from '../types';

const createVirtualModule = (content: string) =>
`data:text/javascript,${content}`;
`data:text/javascript;charset=utf-8,${encodeURIComponent(content)}`;

export const builderPluginAdapterHtml = (
options: BuilderOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/solutions/app-tools/src/config/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MAIN_ENTRY_NAME } from '@modern-js/utils';
import { DEFAULT_ENTRY_NAME, MAIN_ENTRY_NAME } from '@modern-js/utils';
import type { AppUserConfig } from '../types';
import type { AppToolsContext } from '../types/plugin';
import { getAutoInjectEnv } from '../utils/env';
Expand Down Expand Up @@ -37,7 +37,7 @@ export function createDefaultConfig(
alias: Record<string, string>;
} = {
entries: undefined,
mainEntryName: MAIN_ENTRY_NAME,
mainEntryName: DEFAULT_ENTRY_NAME,
enableAsyncEntry: false,
disableDefaultEntries: false,
entriesDir: './src',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const collectHtmlRoutes = (
server: { baseUrl, routes, ssr, ssrByEntries, rsc },
deploy,
} = config;

const { packageName } = appContext;
const workerSSR = deploy?.worker?.ssr;

Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/utils/src/cli/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { InternalPlugins } from '@modern-js/types';
export {
NESTED_ROUTE_SPEC_FILE,
MAIN_ENTRY_NAME,
DEFAULT_ENTRY_NAME,
ROUTE_SPEC_FILE,
SERVER_BUNDLE_DIRECTORY,
SERVER_RENDER_FUNCTION_NAME,
Expand Down
5 changes: 5 additions & 0 deletions packages/toolkit/utils/src/universal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export const NESTED_ROUTE_SPEC_FILE = 'nestedRoutes.json';
*/
export const MAIN_ENTRY_NAME = 'main';

/**
* default entry name
*/
export const DEFAULT_ENTRY_NAME = 'index';

/**
* server side bundles directory, which relative to dist.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/i18n/app-csr/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineConfig({
languages: ['zh', 'en'],
fallbackLanguage: 'en',
localeDetectionByEntry: {
main: {
index: {
enable: false,
},
},
Expand Down