Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/memory router #5247

Merged
merged 7 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions packages/create-app-shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.2.5

- [feat] be able to pass arguments to memory router

## 1.2.4

- [fix] support history for custom configuration
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-app-shared",
"version": "1.2.4",
"version": "1.2.5",
"description": "",
"author": "ice-admin@alibaba-inc.com",
"homepage": "https://github.com/alibaba/ice#readme",
Expand Down
14 changes: 12 additions & 2 deletions packages/create-app-shared/src/createInitHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type CreateHistory = (options: {
location?: Location;
routes?: Route[];
customHistory?: History;
initialIndex?: number;
initialEntries?: string[];
}) => History<unknown>
type InitialContext = null | { location?: Location }
type Options = {
Expand All @@ -32,8 +34,16 @@ export default (createHistory: CreateHistory) =>
}
const { initialContext = null, staticConfig = { routes: [] } } = options;
const { router } = appConfig;
const { type = DEFAULT_APP_CONFIG.router.type, basename, history: customHistory } = router;
const { type = DEFAULT_APP_CONFIG.router.type, basename, history: customHistory, initialEntries, initialIndex } = router;
const location = initialContext ? initialContext.location : null;
const newHistory = createHistory({ type, basename, location, customHistory, routes: staticConfig.routes });
const newHistory = createHistory({
type,
basename,
location,
customHistory,
routes: staticConfig.routes,
initialEntries,
initialIndex
});
appConfig.router.history = newHistory;
};
2 changes: 2 additions & 0 deletions packages/create-app-shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface AppConfig {
type?: 'hash' | 'browser' | 'memory' | 'static';
history?: History;
basename?: string;
initialIndex?: number;
initialEntries?: string[];
},
}

Expand Down
14 changes: 12 additions & 2 deletions packages/create-app-shared/src/web/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import type { CreateHistory, InitHistory } from '../createInitHistory';
import createInitHistory from '../createInitHistory';
import { setHistory } from '../storage';

const createHistory: CreateHistory = ({ type, basename, location, customHistory }) => {
const createHistory: CreateHistory = ({
type,
basename,
location,
customHistory,
initialIndex,
initialEntries,
}) => {
let history: History;
if (process.env.__IS_SERVER__) {
history = createMemoryHistory();
Expand All @@ -16,7 +23,10 @@ const createHistory: CreateHistory = ({ type, basename, location, customHistory
} else if (type === 'browser') {
history = createBrowserHistory({ basename });
} else {
history = createMemoryHistory();
history = createMemoryHistory({
initialIndex,
ClarkXia marked this conversation as resolved.
Show resolved Hide resolved
initialEntries
});
}
setHistory(history);
return history;
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-icestark/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.5.3

- [fix] revert from version 2.5.2 and suport memory router

## 2.5.2

- [fix] support custom history configuration.
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-icestark/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-icestark",
"version": "2.5.2",
"version": "2.5.3",
"description": "Easy use `icestark` in icejs.",
"author": "ice-admin@alibaba-inc.com",
"homepage": "",
Expand Down
13 changes: 10 additions & 3 deletions packages/plugin-icestark/src/runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ const module = ({
getApps,
$$props
} = (icestark || {}) as IPrivateIceStark;
const { type, basename, modifyRoutes: runtimeModifyRoutes, fallback, history: customHistory } = router;
const {
type,
basename,
modifyRoutes: runtimeModifyRoutes,
fallback,
initialIndex,
initialEntries,
} = router;
// compatible with deprecated runtime API
const wrapperComponent = wrapperPageComponent || wrapperRouteComponent;
const createAppHistory = createHistory || ((options: any) => applyRuntimeAPI('createHistory', options));
Expand All @@ -54,7 +61,7 @@ const module = ({

const childBasename = isInIcestark() ? getBasename() : basename;

const history = createAppHistory({ type, basename: childBasename, customHistory });
const history = createAppHistory({ type, basename: childBasename, initialIndex, initialEntries });

addDOMRender(({ App, appMountNode }) => {
return new Promise(resolve => {
Expand Down Expand Up @@ -136,7 +143,7 @@ const module = ({
modifyRoutes(removeRootLayout);
}
const RootApp = ({ routes }) => {
const [routerHistory] = useState(createAppHistory({ type, basename, customHistory }));
const [routerHistory] = useState(createAppHistory({ type, basename, initialEntries, initialIndex }));
const routerProps = {
type,
routes,
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.3

- [fix] be able to pass arguments to memory router

## 2.1.2

- [fix] bump version of `@builder/pack`(^0.6.0)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-router",
"version": "2.1.2",
"version": "2.1.3",
"description": "build-plugin-ice-router",
"author": "ice-admin@alibaba-inc.com",
"homepage": "",
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-router/src/runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ const module = ({ setRenderApp, appConfig, modifyRoutes, modifyRoutesComponent,
...customRouterProps,
};
if (!routerProps.history) {
routerProps.history = applyRuntimeAPI('createHistory', { type: appConfigRouter.type, basename: appConfigRouter.basename });
routerProps.history = applyRuntimeAPI('createHistory', {
type: appConfigRouter.type,
basename: appConfigRouter.basename,
initialIndex: appConfigRouter.initialIndex,
initialEntries: appConfigRouter.initialEntries
});
}
if (process.env.__IS_SERVER__) {
const { initialContext = {} } = context;
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-router/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface IAppRouterProps {
modifyRoutes?: IModifyRoutes;
fallback?: React.ReactNode;
history?: History;
initialIndex?: number;
initialEntries?: string[];
}

export interface IRouterConfig extends DefaultRouteProps {
Expand Down