Skip to content

Commit

Permalink
Feat: enhance single router mode without react-router (#6761)
Browse files Browse the repository at this point in the history
* feat: enhance single router mode without react-router

* fix: test case
  • Loading branch information
ClarkXia authored Jan 25, 2024
1 parent b85feaa commit 8d57a24
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-apes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/runtime': minor
---

feat: enhance router mode without react-router
23 changes: 23 additions & 0 deletions examples/single-route/src/pages/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Outlet, defineDataLoader, useData } from 'ice';

export default function Layout() {
const data = useData();
console.log('layout data', data);
return (
<div>
<h1>Layout</h1>
<Outlet />
</div>
);
}

export const dataLoader = defineDataLoader(() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
name: 'layout',
});
}, 1 * 100);
});
});

9 changes: 2 additions & 7 deletions packages/runtime/src/ClientRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createRouter } from '@remix-run/router';
import type { To, History, Router } from '@remix-run/router';
import type { ClientAppRouterProps } from './types.js';
import App from './App.js';
import { DataContextProvider } from './singleRouter.js';
import { useAppContext } from './AppContext.js';
import { setHistory } from './history.js';
import { disableHistoryWarning } from './utils/deprecatedHistory.js';
Expand All @@ -25,7 +24,7 @@ function createRouterHistory(history: History, router: Router) {

let router: Router = null;
function ClientRouter(props: ClientAppRouterProps) {
const { Component, loaderData, routerContext } = props;
const { Component, routerContext } = props;
const { revalidate } = useAppContext();

function clearRouter() {
Expand Down Expand Up @@ -60,11 +59,7 @@ function ClientRouter(props: ClientAppRouterProps) {
if (process.env.ICE_CORE_ROUTER === 'true') {
element = <RouterProvider router={router} fallbackElement={null} />;
} else {
element = (
<DataContextProvider value={loaderData}>
<Component />
</DataContextProvider>
);
element = <Component />;
}
return (
<App>
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export function createRouteLoader(options: RouteLoaderOptions): LoaderFunction {
}
// Await dataLoader before render.
return async (params) => {
const result = getData(import.meta.renderer === 'client' ? getClientLoaderContext(params.request.url) : defaultRequestContext);
const result = getData(import.meta.renderer === 'client' && params ? getClientLoaderContext(params.request.url) : defaultRequestContext);
let routeData;
try {
if (Array.isArray(result)) {
Expand Down
15 changes: 4 additions & 11 deletions packages/runtime/src/runClientApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
AppContext, WindowContext, AppExport, RouteItem, RuntimeModules, AppConfig, AssetsManifest, ClientAppRouterProps,
ErrorStack,
} from './types.js';
import { createHistory as createHistorySingle } from './singleRouter.js';
import { createHistory as createHistorySingle, getSingleRoute } from './singleRouter.js';
import { setHistory } from './history.js';
import Runtime from './runtime.js';
import { getAppData } from './appData.js';
Expand Down Expand Up @@ -179,14 +179,8 @@ async function render({ history, runtime, needHydrate }: RenderOptions) {
v7_prependBasename: true,
},
};
let singleComponent = null;
let routeData = null;
if (process.env.ICE_CORE_ROUTER !== 'true') {
const singleRoute = matchRoutes(routes, location, basename)[0];
const { Component, loader } = await loadRouteModule(singleRoute.route, routeModuleCache);
singleComponent = Component || singleRoute.route.Component;
routeData = loader && await loader();
}
const SingleComponent = process.env.ICE_CORE_ROUTER !== 'true' &&
await getSingleRoute(routes, basename, routeModuleCache);
const renderRoot = appRender(
root,
<AppContextProvider value={appContext}>
Expand All @@ -195,8 +189,7 @@ async function render({ history, runtime, needHydrate }: RenderOptions) {
routerContext={routerOptions}
routes={routes}
location={history.location}
Component={singleComponent}
loaderData={routeData}
Component={SingleComponent}
/>
</AppRuntimeProvider>
</AppContextProvider>,
Expand Down
1 change: 0 additions & 1 deletion packages/runtime/src/runServerApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import ServerRouter from './ServerRouter.js';
import { renderHTMLToJS } from './renderHTMLToJS.js';
import addLeadingSlash from './utils/addLeadingSlash.js';


export interface RenderOptions {
app: AppExport;
assetsManifest: AssetsManifest;
Expand Down
Loading

0 comments on commit 8d57a24

Please sign in to comment.