Skip to content

Commit 28f0c6a

Browse files
authored
refactor: default entry name should be index (#7779)
1 parent cbce7df commit 28f0c6a

File tree

13 files changed

+48
-12
lines changed

13 files changed

+48
-12
lines changed

packages/runtime/plugin-i18n/src/runtime/context.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ export const useModernI18n = (
138138
// Update i18n instance
139139
await i18nInstance.changeLanguage(newLang);
140140

141-
// Update context language state
142-
if (updateLanguage) {
143-
updateLanguage(newLang);
144-
}
145-
146141
// Update URL if locale detection is enabled, we're in browser, and router is available
147142
if (
148143
enableLocaleDetection &&
@@ -174,6 +169,8 @@ export const useModernI18n = (
174169

175170
// Use history API to navigate without page reload
176171
window.history.pushState(null, '', newUrl);
172+
} else if (updateLanguage) {
173+
updateLanguage(newLang);
177174
}
178175
} catch (error) {
179176
console.error('Failed to change language:', error);

packages/runtime/plugin-i18n/src/runtime/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { getGlobalBasename } from '@modern-js/runtime/context';
12
import { MAIN_ENTRY_NAME } from '@modern-js/utils/universal/constants';
23

3-
export const getEntryPath = (entryName: string = MAIN_ENTRY_NAME) => {
4-
return entryName === MAIN_ENTRY_NAME ? '' : `/${entryName}`;
4+
export const getEntryPath = (entryName?: string): string => {
5+
const basename = getGlobalBasename();
6+
if (basename) {
7+
return basename === '/' ? '' : basename;
8+
}
9+
return '';
510
};
611
/**
712
* Helper function to get language from current pathname

packages/runtime/plugin-runtime/src/cli/code.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,16 @@ export const generateCode = async (
188188
// runtime-global-context.js
189189
let contextCode = '';
190190
if (!config.server.rsc || entrypoint.nestedRoutesEntry) {
191+
const route = serverRoutes.find(r => r.entryName === entryName);
192+
const basename = route?.urlPath || '/';
191193
contextCode = template.runtimeGlobalContext({
192194
entryName,
193195
srcDirectory,
194196
internalSrcAlias,
195197
metaName,
196198
entry,
197199
customEntry,
200+
basename,
198201
});
199202
} else {
200203
const AppProxyPath = path.join(

packages/runtime/plugin-runtime/src/cli/template.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,15 @@ export const runtimeGlobalContext = ({
267267
metaName,
268268
entry,
269269
customEntry,
270+
basename,
270271
}: {
271272
entryName: string;
272273
srcDirectory: string;
273274
internalSrcAlias: string;
274275
metaName: string;
275276
entry: string;
276277
customEntry?: boolean;
278+
basename?: string;
277279
}) => {
278280
return `import { setGlobalContext } from '@${metaName}/runtime/context'
279281
@@ -289,9 +291,11 @@ import App from '${
289291
}';
290292
291293
const entryName = '${entryName}';
294+
const basename = '${basename || '/'}';
292295
setGlobalContext({
293296
entryName,
294297
App,
298+
basename,
295299
});`;
296300
};
297301

packages/runtime/plugin-runtime/src/core/context/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ interface GlobalContext {
3030
* page router _app.tsx export layout app
3131
*/
3232
layoutApp?: React.ComponentType;
33+
/**
34+
* Entry basename for routing
35+
*/
36+
basename?: string;
3337

3438
internalRuntimeContext?: InternalRuntimeContext<RuntimeExtends>;
3539
/**
@@ -64,6 +68,7 @@ export function setGlobalContext(
6468
globalContext.routes = context.routes;
6569
globalContext.appInit = context.appInit;
6670
globalContext.layoutApp = context.layoutApp;
71+
globalContext.basename = context.basename;
6772
globalContext.RSCRoot = context.RSCRoot;
6873
globalContext.isRscClient = context.isRscClient;
6974
globalContext.enableRsc = context.enableRsc;
@@ -98,3 +103,7 @@ export function getGlobalRoutes(): undefined | (NestedRoute | PageRoute)[] {
98103
export function getGlobalLayoutApp() {
99104
return globalContext.layoutApp;
100105
}
106+
107+
export function getGlobalBasename() {
108+
return globalContext.basename;
109+
}

packages/runtime/plugin-runtime/src/router/cli/code/templates.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ export const runtimeGlobalContext = async ({
558558
internalSrcAlias,
559559
globalApp,
560560
rscType = false,
561+
basename,
561562
}: {
562563
entryName: string;
563564
metaName: string;
@@ -566,6 +567,7 @@ export const runtimeGlobalContext = async ({
566567
internalSrcAlias: string;
567568
globalApp?: string | false;
568569
rscType?: 'server' | 'client' | false;
570+
basename?: string;
569571
}) => {
570572
const imports = [
571573
`import { setGlobalContext } from '@${metaName}/runtime/context';`,
@@ -619,11 +621,13 @@ export const runtimeGlobalContext = async ({
619621
import { routes } from './routes';
620622
621623
const entryName = '${entryName}';
624+
const basename = '${basename || '/'}';
622625
setGlobalContext({
623626
entryName,
624627
layoutApp,
625628
routes,
626629
appInit,
630+
basename,
627631
isRscClient: true,
628632
enableRsc: true,
629633
});
@@ -634,11 +638,13 @@ export const runtimeGlobalContext = async ({
634638
import { routes } from './routes';
635639
636640
const entryName = '${entryName}';
641+
const basename = '${basename || '/'}';
637642
setGlobalContext({
638643
entryName,
639644
layoutApp,
640645
routes,
641646
appInit,
647+
basename,
642648
enableRsc: ${enableRsc},
643649
});
644650
`;

packages/runtime/plugin-runtime/src/router/cli/handler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export async function handleGeneratorEntryCode(
3434
await Promise.all(
3535
entrypoints.map(async entrypoint => {
3636
if (entrypoint.nestedRoutesEntry || entrypoint.pageRoutesEntry) {
37+
const route = appContext.serverRoutes.find(
38+
r => r.entryName === entrypoint.entryName,
39+
);
40+
const basename = route?.urlPath || '/';
3741
generatorRegisterCode(
3842
internalDirectory,
3943
entrypoint.entryName,
@@ -45,6 +49,7 @@ export async function handleGeneratorEntryCode(
4549
internalSrcAlias: appContext.internalSrcAlias,
4650
globalApp: entrypoint.fileSystemRoutes?.globalApp,
4751
rscType: enableRsc ? 'client' : undefined,
52+
basename,
4853
}),
4954
);
5055
if (enableRsc) {
@@ -59,6 +64,7 @@ export async function handleGeneratorEntryCode(
5964
internalSrcAlias: appContext.internalSrcAlias,
6065
globalApp: entrypoint.fileSystemRoutes?.globalApp,
6166
rscType: 'server',
67+
basename,
6268
}),
6369
);
6470
}

packages/solutions/app-tools/src/builder/shared/builderPlugins/adapterHtml.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import type {
66
RsbuildPlugin,
77
RspackChain,
88
} from '@rsbuild/core';
9-
import type { HtmlUserConfig } from '../../../types/config/html';
109
import { BottomTemplatePlugin } from '../bundlerPlugins';
1110
import type { BuilderOptions } from '../types';
1211

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

1615
export const builderPluginAdapterHtml = (
1716
options: BuilderOptions,

packages/solutions/app-tools/src/config/default.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MAIN_ENTRY_NAME } from '@modern-js/utils';
1+
import { DEFAULT_ENTRY_NAME, MAIN_ENTRY_NAME } from '@modern-js/utils';
22
import type { AppUserConfig } from '../types';
33
import type { AppToolsContext } from '../types/plugin';
44
import { getAutoInjectEnv } from '../utils/env';
@@ -37,7 +37,7 @@ export function createDefaultConfig(
3737
alias: Record<string, string>;
3838
} = {
3939
entries: undefined,
40-
mainEntryName: MAIN_ENTRY_NAME,
40+
mainEntryName: DEFAULT_ENTRY_NAME,
4141
enableAsyncEntry: false,
4242
disableDefaultEntries: false,
4343
entriesDir: './src',

packages/solutions/app-tools/src/plugins/analyze/getServerRoutes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const collectHtmlRoutes = (
129129
server: { baseUrl, routes, ssr, ssrByEntries, rsc },
130130
deploy,
131131
} = config;
132+
132133
const { packageName } = appContext;
133134
const workerSSR = deploy?.worker?.ssr;
134135

0 commit comments

Comments
 (0)