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

[Tutorial Suggestion] Code Splitting #717

Open
aress31 opened this issue May 1, 2020 · 3 comments
Open

[Tutorial Suggestion] Code Splitting #717

aress31 opened this issue May 1, 2020 · 3 comments

Comments

@aress31
Copy link
Contributor

aress31 commented May 1, 2020

For developers who are starting to experiment with React and mui-treasury, writing a tutorial showing how to code split based on the <Content> of the layout library could be a good idea.

@siriwatknp
Copy link
Owner

I have a few experience with code splitting, can you give me an example of documentation like this?

@aress31
Copy link
Contributor Author

aress31 commented May 1, 2020

Sure will do that sometime this week.

@aress31
Copy link
Contributor Author

aress31 commented May 14, 2020

In fact, I think that the best things to do would be to reference the React official doc - https://reactjs.org/docs/code-splitting.html#code-splitting - as it is well presented and explained and obviously maintained by the React team. What I like with MUI-Layout, is that it makes code splitting with the use of the Content component to wrap around the routes - good for route-based code splitting.

A minimal example of code-splitting would be:

  • App.jsx
import theme from "./styles/theme";
import routes from "./constants/routes";
--- other imports ---

export default () => {
  const muiTheme = React.useMemo(() => responsiveFontSizes(theme()), []);

  return (
    <MuiThemeProvider theme={muiTheme}>
      <CssBaseline />
      <Router>
        <Root themeProviderOmitted scheme={scheme}>
          {({ state: { sidebar } }) => (
            <>
              <Header />
              <Sidebar />
              <Content>
                <React.Suspense fallback={<Loader />}>
                  <Routes>
                    {routes.map((route) => (
                      <Route
                        key={uid(route)}
                        element={<route.element />}
                        path={route.path}
                      />
                    ))}
                  </Routes>
                </React.Suspense>
                )}
              </Content>
              <Footer />
            </>
          )}
        </Root>
      </Router>
    </MuiThemeProvider>
  );
};
  • routes.jsx:
import React from "react";

export default [
  {
    element: React.lazy(() => import("../pages/Landing")),
    path: "/",
  },
  {
    element: React.lazy(() => import("../pages/Foo")),
    path: "/foo",
  },
  {
    element: React.lazy(() => import("../pages/Bar")),
    path: "/bar",
  },
  {
    element: React.lazy(() => import("../pages/NotFound")),
    label: "404 Not Found",
    path: "*",
  },
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants