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

[feature]: Lazy Load/trim unused bytes in main routes #2988

Merged
merged 15 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ services:
- ./packages/venia-concept/static:/usr/src/app/packages/venia-concept/static:rw
- ./packages/venia-ui/.storybook:/usr/src/app/packages/venia-ui/.storybook:rw
- ./packages/venia-ui/lib:/usr/src/app/packages/venia-ui/lib:rw
- ./packages/venia-ui/templates:/usr/src/app/packages/venia-ui/templates:rw
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For starters, I might have scope creeped this a bit but as far as I can tell we no longer use the templates, opting instead to use venia-concept/template.html. @zetlen @revanth0212 can ya'll confirm this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. We got rid of mustache templates in favor of Webpack's HTML Plugin which utilized template.html to render the main HTML file.

environment:
# environment variables consumed by the nginx-proxy service
VIRTUAL_HOST: ${DEV_SERVER_HOST}
Expand Down
17 changes: 16 additions & 1 deletion packages/venia-concept/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@
<link rel="apple-touch-icon" href="/venia-static/icons/venia_square_57.png">
<link rel="apple-touch-icon" sizes="180x180" href="/venia-static/icons/apple-touch-icon.png">
<link rel="preconnect" href="<%= process.env.MAGENTO_BACKEND_URL %>">
<!--
This following CSS is a copy of the css returned by Google's font API that
allows us to "skip" a network round trip.

CSS requests are _render_ blocking, while font requests are only _text_
blocking. By removing the link request we only wait for font request, to
shave off a few ms :)

The CSS below this comment is the response copied wholesale from Google's font
API. If you ever need to update it, get the response and replace what is below
this comment. Remember to replace the preload link above with the src URL!

Example API responses:
https://fonts.googleapis.com/css?family=Muli:400&display=swap
https://fonts.googleapis.com/css?family=Source+Serif+Pro:600&display=swap
-->
<style type="text/css">
/* vietnamese */
@font-face {
Expand Down Expand Up @@ -128,7 +143,7 @@
</style>
</head>
<body>
<div id="root" />
<div id="root"></div>

<!-- Fallback for when JavaScript is disabled. -->
<noscript>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react';
import React, { Fragment, Suspense } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import { Redirect } from '@magento/venia-drivers';
import { useAccountInformationPage } from '@magento/peregrine/lib/talons/AccountInformationPage/useAccountInformationPage';
Expand All @@ -8,10 +8,12 @@ import Button from '../Button';
import { Message } from '../Field';
import { Title } from '../Head';
import { fullPageLoadingIndicator } from '../LoadingIndicator';
import EditModal from './editModal';

import defaultClasses from './accountInformationPage.css';
import AccountInformationPageOperations from './accountInformationPage.gql.js';

const EditModal = React.lazy(() => import('./editModal'));

const AccountInformationPage = props => {
const classes = mergeClasses(defaultClasses, props.classes);

Expand Down Expand Up @@ -103,16 +105,18 @@ const AccountInformationPage = props => {
</Button>
</div>
</div>
<EditModal
formErrors={formErrors}
initialValues={customer}
isDisabled={isDisabled}
isOpen={isUpdateMode}
onCancel={handleCancel}
onChangePassword={handleChangePassword}
onSubmit={handleSubmit}
shouldShowNewPassword={shouldShowNewPassword}
/>
<Suspense fallback={null}>
<EditModal
formErrors={formErrors}
initialValues={customer}
isDisabled={isDisabled}
isOpen={isUpdateMode}
onCancel={handleCancel}
onChangePassword={handleChangePassword}
onSubmit={handleSubmit}
shouldShowNewPassword={shouldShowNewPassword}
/>
</Suspense>
</Fragment>
);
}
Expand Down
9 changes: 6 additions & 3 deletions packages/venia-ui/lib/components/App/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, Suspense } from 'react';
import { useIntl } from 'react-intl';
import { array, func, shape, string } from 'prop-types';

Expand All @@ -9,7 +9,6 @@ import globalCSS from '../../index.css';
import { HeadProvider, Title } from '../Head';
import Main from '../Main';
import Mask from '../Mask';
import Navigation from '../Navigation';
import Routes from '../Routes';
import ToastContainer from '../ToastContainer';
import Icon from '../Icon';
Expand All @@ -20,6 +19,8 @@ import {
Wifi as WifiIcon
} from 'react-feather';

const Navigation = React.lazy(() => import('../Navigation'));

const OnlineIcon = <Icon src={WifiIcon} attrs={{ width: 18 }} />;
const OfflineIcon = <Icon src={CloudOffIcon} attrs={{ width: 18 }} />;
const ErrorIcon = <Icon src={AlertCircleIcon} attrs={{ width: 18 }} />;
Expand Down Expand Up @@ -117,7 +118,9 @@ const App = props => {
<Routes />
</Main>
<Mask isActive={hasOverlay} dismiss={handleCloseDrawer} />
<Navigation />
<Suspense fallback={null}>
<Navigation />
</Suspense>
<ToastContainer />
</HeadProvider>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import React, { Suspense } from 'react';
import { useIntl } from 'react-intl';

import { Section } from '../../Accordion';
import GiftCards from '../GiftCards';

const GiftCards = React.lazy(() => import('../GiftCards'));

const GiftCardSection = props => {
const { setIsCartUpdating } = props;
Expand All @@ -15,7 +16,9 @@ const GiftCardSection = props => {
defaultMessage: 'Apply Gift Card'
})}
>
<GiftCards setIsCartUpdating={setIsCartUpdating} />
<Suspense fallback={null}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the lack of fallbacks in all these added usages? Happy path fast network would mean you'd likely never see it, but perhaps this is still reachable? Tiny example of toggling Suspense on/off with React DevTools:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add loading spinners. I just didn't think it was necessary since all the sections are behind user interaction. You're right though, we should probably include something to indicate loading is happening.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended to be a conversation starter, don't implement anything yet. I honestly haven't been able to catch an unloaded suspense component, even on Slow 3G, so if the use case isn't realistic, the nulls are fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easy enough to add them to these price adjustment accordions. Everything else though, I will not touch, including modals, or dropdowns, or the left nav.

<GiftCards setIsCartUpdating={setIsCartUpdating} />
</Suspense>
</Section>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React from 'react';
import React, { Suspense } from 'react';
import { useIntl } from 'react-intl';
import { func } from 'prop-types';

import { mergeClasses } from '../../../classify';
import { Accordion, Section } from '../../Accordion';
import CouponCode from './CouponCode';
import GiftCardSection from './giftCardSection';
import GiftOptions from './GiftOptions';
import ShippingMethods from './ShippingMethods';

import defaultClasses from './priceAdjustments.css';

const CouponCode = React.lazy(() => import('./CouponCode'));
const GiftOptions = React.lazy(() => import('./GiftOptions'));
const ShippingMethods = React.lazy(() => import('./ShippingMethods'));

/**
* PriceAdjustments is a child component of the CartPage component.
* It renders the price adjustments forms for applying gift cards, coupons, and the shipping method.
Expand Down Expand Up @@ -43,7 +44,11 @@ const PriceAdjustments = props => {
defaultMessage: 'Estimate your Shipping'
})}
>
<ShippingMethods setIsCartUpdating={setIsCartUpdating} />
<Suspense fallback={null}>
<ShippingMethods
setIsCartUpdating={setIsCartUpdating}
/>
</Suspense>
</Section>
<Section
id={'coupon_code'}
Expand All @@ -52,7 +57,9 @@ const PriceAdjustments = props => {
defaultMessage: 'Enter Coupon Code'
})}
>
<CouponCode setIsCartUpdating={setIsCartUpdating} />
<Suspense fallback={null}>
<CouponCode setIsCartUpdating={setIsCartUpdating} />
</Suspense>
</Section>
<GiftCardSection setIsCartUpdating={setIsCartUpdating} />
<Section
Expand All @@ -62,7 +69,9 @@ const PriceAdjustments = props => {
defaultMessage: 'See Gift Options'
})}
>
<GiftOptions />
<Suspense fallback={null}>
<GiftOptions />
</Suspense>
</Section>
</Accordion>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { Fragment } from 'react';
import React, { Fragment, Suspense } from 'react';
import { FormattedMessage } from 'react-intl';
import { gql } from '@apollo/client';
import { useProductListing } from '@magento/peregrine/lib/talons/CartPage/ProductListing/useProductListing';

import { mergeClasses } from '../../../classify';
import LoadingIndicator from '../../LoadingIndicator';
import EditModal from './EditModal';
import defaultClasses from './productListing.css';
import Product from './product';
import { ProductListingFragment } from './productListingFragments';

const EditModal = React.lazy(() => import('./EditModal'));
/**
* A child component of the CartPage component.
* This component renders the product listing on the cart page.
Expand Down Expand Up @@ -60,11 +60,13 @@ const ProductListing = props => {
return (
<Fragment>
<ul className={classes.root}>{productComponents}</ul>
<EditModal
item={activeEditItem}
setIsCartUpdating={setIsCartUpdating}
setActiveEditItem={setActiveEditItem}
/>
<Suspense fallback={null}>
<EditModal
item={activeEditItem}
setIsCartUpdating={setIsCartUpdating}
setActiveEditItem={setActiveEditItem}
/>
</Suspense>
</Fragment>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, useEffect, useMemo } from 'react';
import React, { Fragment, useEffect, useMemo, Suspense } from 'react';
import { FormattedMessage } from 'react-intl';
import { shape, string, func } from 'prop-types';
import { PlusSquare, AlertCircle as AlertCircleIcon } from 'react-feather';
Expand All @@ -9,10 +9,12 @@ import { mergeClasses } from '../../../classify';
import Button from '../../Button';
import defaultClasses from './addressBook.css';
import AddressBookOperations from './addressBook.gql';
import EditModal from '../ShippingInformation/editModal';
import AddressCard from './addressCard';
import Icon from '../../Icon';
import LinkButton from '../../LinkButton';

const EditModal = React.lazy(() => import('../ShippingInformation/editModal'));

const errorIcon = (
<Icon
src={AlertCircleIcon}
Expand Down Expand Up @@ -155,7 +157,9 @@ const AddressBook = props => {

<div className={classes.content}>{addressElements}</div>
</div>
<EditModal shippingData={activeAddress} />
<Suspense fallback={null}>
<EditModal shippingData={activeAddress} />
</Suspense>
</Fragment>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Suspense } from 'react';
import { FormattedMessage } from 'react-intl';
import { Form } from 'informed';
import { shape, func, string, bool, instanceOf } from 'prop-types';
Expand All @@ -9,13 +9,14 @@ import CheckoutError from '@magento/peregrine/lib/talons/CheckoutPage/CheckoutEr
import PaymentMethods from './paymentMethods';
import Summary from './summary';
import { mergeClasses } from '../../../classify';
import EditModal from './editModal';

import paymentInformationOperations from './paymentInformation.gql';

import defaultClasses from './paymentInformation.css';
import LoadingIndicator from '../../LoadingIndicator';

const EditModal = React.lazy(() => import('./editModal'));

const PaymentInformation = props => {
const {
classes: propClasses,
Expand Down Expand Up @@ -72,7 +73,9 @@ const PaymentInformation = props => {
);

const editModal = doneEditing ? (
<EditModal onClose={hideEditModal} isOpen={isEditModalActive} />
<Suspense fallback={null}>
<EditModal onClose={hideEditModal} isOpen={isEditModalActive} />
</Suspense>
) : null;

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react';
import React, { Fragment, Suspense } from 'react';
import { FormattedMessage } from 'react-intl';
import { func, string, shape } from 'prop-types';
import { Edit2 as EditIcon } from 'react-feather';
Expand All @@ -9,11 +9,12 @@ import Icon from '../../Icon';
import LoadingIndicator from '../../LoadingIndicator';
import AddressForm from './AddressForm';
import Card from './card';
import EditModal from './editModal';
import defaultClasses from './shippingInformation.css';
import ShippingInformationOperations from './shippingInformation.gql';
import LinkButton from '../../LinkButton';

const EditModal = React.lazy(() => import('./editModal'));

const ShippingInformation = props => {
const { classes: propClasses, onSave, toggleActiveContent } = props;
const talonProps = useShippingInformation({
Expand Down Expand Up @@ -50,7 +51,9 @@ const ShippingInformation = props => {
}

const editModal = !isSignedIn ? (
<EditModal shippingData={shippingData} />
<Suspense fallback={null}>
<EditModal shippingData={shippingData} />
</Suspense>
) : null;

const shippingInformation = doneEditing ? (
Expand Down
17 changes: 10 additions & 7 deletions packages/venia-ui/lib/components/Header/accountTrigger.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, { Fragment } from 'react';
import React, { Fragment, Suspense } from 'react';
import { useIntl } from 'react-intl';
import { shape, string } from 'prop-types';

import { useAccountTrigger } from '@magento/peregrine/lib/talons/Header/useAccountTrigger';
import { mergeClasses } from '@magento/venia-ui/lib/classify';

import AccountChip from '../AccountChip';
import AccountMenu from '../AccountMenu';

import defaultClasses from './accountTrigger.css';

const AccountMenu = React.lazy(() => import('../AccountMenu'));

/**
* The AccountTrigger component is the call to action in the site header
* that toggles the AccountMenu dropdown.
Expand Down Expand Up @@ -51,11 +52,13 @@ const AccountTrigger = props => {
/>
</button>
</div>
<AccountMenu
ref={accountMenuRef}
accountMenuIsOpen={accountMenuIsOpen}
setAccountMenuIsOpen={setAccountMenuIsOpen}
/>
<Suspense fallback={null}>
<AccountMenu
ref={accountMenuRef}
accountMenuIsOpen={accountMenuIsOpen}
setAccountMenuIsOpen={setAccountMenuIsOpen}
/>
</Suspense>
</Fragment>
);
};
Expand Down
Loading