Skip to content

Commit

Permalink
async chunking
Browse files Browse the repository at this point in the history
  • Loading branch information
clintandrewhall committed Oct 27, 2024
1 parent 3b7468e commit 8a763bf
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 67 deletions.
1 change: 1 addition & 0 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ jobs:
with:
projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
exitOnceUploaded: true
buildScriptName: 'build:storybook'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"storybook": "cross-env BROWSER=\"google chrome\" OPEN_MATCH_HOST_ONLY=true storybook dev -p 6006",
"build:storybook": "storybook build",
"upgrade:storybook": "yarn dlx storybook@latest upgrade",
"chromatic": "chromatic"
"chromatic": "chromatic --build-script-name=build:storybook"
},
"dependencies": {
"@linaria/atomic": "^6.2.0",
Expand Down
44 changes: 27 additions & 17 deletions src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import { useEffect } from 'react';
import { css as csl } from '@linaria/core';
import 'ress';
import 'unfonts.css';
import { Outlet, useLocation } from 'react-router-dom';

import { Meta } from '@components/meta';
import { css, cx } from '@lib/css';
import { theme } from '@theme';

import { Routes } from './routing';
export const App = () => {
// Extracts pathname property(key) from an object
const { pathname } = useLocation();

export const App = () => (
<>
<Meta />
<div
className={cx(
csl`
// Automatically scrolls to top whenever pathname changes
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);

return (
<>
<Meta />
<div
className={cx(
csl`
${theme.decl.font.size.step0}
${theme.decl.font.sansSerif.regular}
`,
css`
${theme.page.body}
${theme.definitions}
`,
)}
>
<Routes />
</div>
</>
);
css`
${theme.page.body}
${theme.definitions}
`,
)}
>
<Outlet />
</div>
</>
);
};
2 changes: 0 additions & 2 deletions src/components/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { cx } from '@lib/css';

import { Footer } from './footer';
import { Header } from './header';
import { Section } from './section';

import styles from './layout.styles';

Expand All @@ -27,6 +26,5 @@ const Component = forwardRef<HTMLDivElement, LayoutProps>(

export const Layout = Object.assign(Component, {
Header,
Section,
Footer,
});
21 changes: 9 additions & 12 deletions src/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { HelmetProvider } from 'react-helmet-async';
import { BrowserRouter } from 'react-router-dom';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';

// if (!Object.hasOwn) {
// Object.hasOwn = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
// }
import { App } from './app';
import { routes } from './routing';

const root = document.getElementById('root');
const router = createBrowserRouter(routes);

if (root?.hasChildNodes()) {
ReactDOM.hydrateRoot(
root,
<React.StrictMode>
<BrowserRouter>
<HelmetProvider>
<App />
</HelmetProvider>
</BrowserRouter>
<HelmetProvider>
<RouterProvider router={router} />
</HelmetProvider>
</React.StrictMode>,
);
} else {
ReactDOM.createRoot(root!).render(
<React.StrictMode>
<BrowserRouter>
<HelmetProvider>
<App />
</HelmetProvider>
</BrowserRouter>
<HelmetProvider>
<RouterProvider router={router} />
</HelmetProvider>
</React.StrictMode>,
);
}
6 changes: 2 additions & 4 deletions src/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ReactDOMServer from 'react-dom/server';
import { HelmetProvider, type HelmetServerState } from 'react-helmet-async';
import { StaticRouter } from 'react-router-dom/server';

import { App } from './app';
import { AppRoutes } from './routing';

interface HelmetContext {
helmet?: HelmetServerState;
Expand All @@ -13,9 +13,7 @@ export function SSRRender(url: string | Partial<Location>) {

const html = ReactDOMServer.renderToString(
<StaticRouter location={url}>
<HelmetProvider context={helmetContext}>
<App />
</HelmetProvider>
<HelmetProvider context={helmetContext}>{AppRoutes}</HelmetProvider>
</StaticRouter>,
);

Expand Down
4 changes: 2 additions & 2 deletions src/pages/article/article_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Layout } from '@components/layout';
import { Section } from '@components/layout';

import { ArticleHeader } from './article_header';
import { ArticleMarkdown } from './article_markdown';
Expand All @@ -19,7 +19,7 @@ const Component = ({ article }: ArticleLayoutProps) => (
);

export const ArticleLayout = Object.assign(Component, {
Root: Layout.Section,
Root: Section,
Header: ArticleHeader,
Markdown: ArticleMarkdown,
});
6 changes: 3 additions & 3 deletions src/pages/portfolio/portfolio_list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Layout, type SectionProps } from '@components/layout';
import { Section, type SectionProps } from '@components/layout';
import { PortfolioGrid, usePortfolioItems } from '@components/portfolio';

import styles from './portfolio_list.styles';
Expand All @@ -22,8 +22,8 @@ const Component = () => {
};

export const PortfolioList = Object.assign(Component, {
Header: () => <Layout.Section.Header {...attributes} />,
Header: () => <Section.Header {...attributes} />,
Root: ({ children }: Pick<SectionProps, 'children'>) => (
<Layout.Section id="portfolio">{children}</Layout.Section>
<Section id="portfolio">{children}</Section>
),
});
43 changes: 17 additions & 26 deletions src/routing/index.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
import { useEffect } from 'react';
import { Home } from '@pages/home';
import { Portfolio } from '@pages/portfolio';
import { Outlet, Route as ReactRoute, Routes as ReactRoutes, useLocation } from 'react-router-dom';
import { createRoutesFromElements, Route, Routes } from 'react-router-dom';

import { Article } from './article';
import { App } from '../app';

const ScrollToTop = () => {
// Extracts pathname property(key) from an object
const { pathname } = useLocation();
const others = (
<Route element={<App />}>
<Route path="/" lazy={async () => ({ Component: (await import('../pages/home')).Home })} />
<Route
path="/portfolio"
lazy={async () => ({ Component: (await import('../pages/portfolio')).Portfolio })}
/>
<Route
path="/portfolio/:id"
lazy={async () => ({ Component: (await import('./article')).Article })}
/>
</Route>
);

// Automatically scrolls to top whenever pathname changes
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
export const AppRoutes = <Routes>{others}</Routes>;

return <Outlet />;
};

export const Routes = () => {
return (
<ReactRoutes>
<ReactRoute element={<ScrollToTop />}>
<ReactRoute path="/" element={<Home />} />
<ReactRoute path="/portfolio" element={<Portfolio />} />
<ReactRoute path="/portfolio/:id" element={<Article />} />
</ReactRoute>
</ReactRoutes>
);
};
export const routes = createRoutesFromElements(others);

0 comments on commit 8a763bf

Please sign in to comment.