Skip to content

Commit

Permalink
fix: unnecessary to import runtime module and routes when csr (#6507)
Browse files Browse the repository at this point in the history
* fix: unnecessary to import runtime module and routes when csr

* fix: optimize code
  • Loading branch information
ClarkXia authored Sep 4, 2023
1 parent 6444226 commit 68f19ea
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/hot-pants-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ice/runtime': patch
'@ice/app': patch
---

fix: should not import runtime module and routes when csr
2 changes: 1 addition & 1 deletion examples/with-store/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStore } from 'ice';
import user from './models/user';

console.log('app store depend on window', window);
export default createStore({ user });
9 changes: 8 additions & 1 deletion packages/ice/templates/core/entry.server.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import type { RenderMode, DistType } from '@ice/runtime';
import type { RenderToPipeableStreamOptions } from 'react-dom/server';
// @ts-ignore
import assetsManifest from 'virtual:assets-manifest.json';
<% if(hydrate) {-%>
import createRoutes from './routes';
<% } else { -%>
import routesManifest from './route-manifest.json';
<% } -%>
import routesConfig from './routes-config.bundle.mjs';
<% if(dataLoaderImport.imports) {-%><%-dataLoaderImport.imports%><% } -%>
<%- runtimeOptions.imports %>
<% if(hydrate) {-%><%- runtimeOptions.imports %><% } -%>
<% if(!hydrate) {-%>
// Do not inject runtime modules when render mode is document only.
const commons = [];
const statics = [];
const createRoutes = () => routesManifest;
<% } -%>
const runtimeModules = { commons, statics };

Expand Down Expand Up @@ -90,6 +95,7 @@ function mergeOptions(options) {
basename: basename || getRouterBasename(),
renderMode,
routesConfig,
<% if(hydrate) {-%>
runtimeOptions: {
<% if (runtimeOptions.exports) { -%>
<%- runtimeOptions.exports %>
Expand All @@ -98,5 +104,6 @@ function mergeOptions(options) {
...<%- JSON.stringify(customRuntimeOptions) %>,
<% } _%>
},
<% } -%>
};
}
2 changes: 1 addition & 1 deletion packages/runtime/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const Data: DataType = (props: DataProps) => {
} = props;

const matchedIds = matches.map(match => match.route.id);
const routePath = encodeURI(getCurrentRoutePath(matches));
const routePath = matches.length > 0 ? encodeURI(getCurrentRoutePath(matches)) : '';
const windowContext: WindowContext = {
appData,
loaderData,
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/runServerApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ function renderDocument(options: RenderDocumentOptions): Response {
const {
matches,
renderOptions,
routePath,
routePath = '',
downgrade,
routes,
documentData,
Expand Down

0 comments on commit 68f19ea

Please sign in to comment.