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

feat: code split & dynamic imports #188

Merged
merged 15 commits into from
Apr 12, 2024
Merged

Conversation

SgtPooki
Copy link
Member

@SgtPooki SgtPooki commented Apr 10, 2024

  • chore: remove unused component
  • tmp
  • feat: basic router implemented
  • fix: redirect page loads on subdomain
  • chore: cleaned up code

Title

feat: code split & dynamic imports

Description

Implements a version of #93 (comment).

Fixes #93
Fixes #147

FYI that this conflicts slightly with #182 and differences need to be resolved (should be a minor change) that PR was merged into this one

Summary of changes:

  • Code splitting with dynamic imports
  • Basic hash router implemented as context provider
  • some consolidation of logic and removal of old code (app.tsx is significantly less complicated)
  • refactored file names to snake-case.
  • chunks have hashes postfixed to resolve chore: add chunk hash to files other than ipfs-sw-sw.js #147
  • test/node.ts post-build test updated to support testing new dist files structure.

Notes & open questions

NOTE: This PR is in draft because serving different JS bundles based on subdomain is a little tricky, but I will look into that now that I have basic code splitting and dynamic loading working with decent pure routing functions.

Details about assets

> npm run clean && npm run build && ls -lhatr dist && du -hs dist && cat dist/index.html | grep 'script'

# ...
webpack 5.91.0 compiled successfully in 4012 ms
total 4.7M
drwxr-xr-x 33 sgtpooki 1.1K Apr 10 11:09 ..
-rw-r--r--  1 sgtpooki  51K Apr 10 11:09 ipfs-sw-main.js
-rw-r--r--  1 sgtpooki  30K Apr 10 11:09 ipfs-sw-778.js
-rw-r--r--  1 sgtpooki 5.2K Apr 10 11:09 ipfs-sw-778.css
-rw-r--r--  1 sgtpooki  16K Apr 10 11:09 ipfs-sw-781.js
-rw-r--r--  1 sgtpooki 2.5K Apr 10 11:09 ipfs-sw-992.js
-rw-r--r--  1 sgtpooki  25K Apr 10 11:09 ipfs-sw-850.js
-rw-r--r--  1 sgtpooki 2.5K Apr 10 11:09 ipfs-sw-611.js
-rw-r--r--  1 sgtpooki  223 Apr 10 11:09 ipfs-sw-styles.js
-rw-r--r--  1 sgtpooki 2.7K Apr 10 11:09 ipfs-sw-styles.css
-rw-r--r--  1 sgtpooki  55K Apr 10 11:09 ipfs-sw-sw.js
-rw-r--r--  1 sgtpooki 577K Apr 10 11:09 ipfs-sw-vendor-react.js
-rw-r--r--  1 sgtpooki  57K Apr 10 11:09 ipfs-sw-117.js
-rw-r--r--  1 sgtpooki   26 Apr 10 11:09 _redirects
-rw-r--r--  1 sgtpooki  88K Apr 10 11:09 ipfs-sw-96.js
-rw-r--r--  1 sgtpooki 3.4M Apr 10 11:09 ipfs-sw-561.js
drwxr-xr-x 31 sgtpooki  992 Apr 10 11:09 .
-rw-r--r--  1 sgtpooki   43 Apr 10 11:09 ipfs-sw-611.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 6.1K Apr 10 11:09 ipfs-sw-117.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 284K Apr 10 11:09 ipfs-sw-561.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 1.9K Apr 10 11:09 ipfs-sw-778.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 8.3K Apr 10 11:09 ipfs-sw-96.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 1.5K Apr 10 11:09 ipfs-sw-850.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 2.8K Apr 10 11:09 ipfs-sw-781.js.LICENSE.txt
-rw-r--r--  1 sgtpooki   43 Apr 10 11:09 ipfs-sw-992.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 8.3K Apr 10 11:09 ipfs-sw-sw.js.LICENSE.txt
-rw-r--r--  1 sgtpooki 5.9K Apr 10 11:09 ipfs-sw-main.js.LICENSE.txt
-rw-r--r--  1 sgtpooki    6 Apr 10 11:09 ipfs-sw-styles.js.LICENSE.txt
-rw-r--r--  1 sgtpooki  776 Apr 10 11:09 ipfs-sw-vendor-react.js.LICENSE.txt
-rw-r--r--  1 sgtpooki  34K Apr 10 11:09 favicon.ico
-rw-r--r--  1 sgtpooki 1.7K Apr 10 11:09 index.html
4.7M	dist
  <link rel="icon" href="/favicon.ico"><script defer src="ipfs-sw-vendor-react.js"></script><script defer src="ipfs-sw-styles.js"></script><script defer src="ipfs-sw-117.js"></script><script defer src="ipfs-sw-96.js"></script><script defer src="ipfs-sw-main.js"></script><link href="/ipfs-sw-styles.css" rel="stylesheet"></head>

What's pulled on first hit

The below are loaded in firefox using a server ran with:

> npx http-server -p 3335 dist

main domain http://localhost:3335

14 requests
1.11 MB / 1.04 MB transferred
Finish: 113 ms
DOMContentLoaded: 12 ms
load: 56 ms

[2024-04-10T18:16:50.137Z]  "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.170Z]  "GET /ipfs-sw-vendor-react.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.172Z]  "GET /ipfs-sw-styles.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.172Z]  "GET /ipfs-sw-117.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.173Z]  "GET /ipfs-sw-96.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.173Z]  "GET /ipfs-sw-main.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.173Z]  "GET /ipfs-sw-styles.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.192Z]  "GET /ipfs-sw-992.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.192Z]  "GET /ipfs-sw-sw.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.194Z]  "GET /favicon.ico" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.197Z]  "GET /ipfs-sw-781.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.218Z]  "GET /ipfs-sw-117.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:16:50.223Z]  "GET /ipfs-sw-561.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"

main domain http://localhost:3335/#/ipfs-sw-config

2 requests
35.47 kB / 36.11 kB transferred
Finish: 14 ms

[2024-04-10T18:18:24.415Z]  "GET /ipfs-sw-778.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:18:24.419Z]  "GET /ipfs-sw-778.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"

subdomain http://specs-ipfs-tech.ipns.localhost:3335

27 requests
2.13 MB / 1.99 MB transferred
Finish: 233 ms
DOMContentLoaded: 16 ms
load: 72 ms

Note that the config page in the iframe makes additional requests for the same assets, and I have cache disabled.

[2024-04-10T18:13:55.477Z]  "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.508Z]  "GET /ipfs-sw-vendor-react.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.525Z]  "GET /ipfs-sw-styles.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.526Z]  "GET /ipfs-sw-117.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.526Z]  "GET /ipfs-sw-96.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.527Z]  "GET /ipfs-sw-styles.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.527Z]  "GET /ipfs-sw-main.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.550Z]  "GET /ipfs-sw-992.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.552Z]  "GET /ipfs-sw-sw.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.553Z]  "GET /favicon.ico" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.556Z]  "GET /ipfs-sw-850.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.587Z]  "GET /ipfs-sw-117.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.588Z]  "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.614Z]  "GET /ipfs-sw-561.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.616Z]  "GET /ipfs-sw-vendor-react.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.621Z]  "GET /ipfs-sw-styles.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.622Z]  "GET /ipfs-sw-117.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.622Z]  "GET /ipfs-sw-96.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.624Z]  "GET /ipfs-sw-styles.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.624Z]  "GET /ipfs-sw-main.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.659Z]  "GET /ipfs-sw-992.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.664Z]  "GET /ipfs-sw-778.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:55.664Z]  "GET /ipfs-sw-778.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:56.580Z]  "GET /ipfs-sw-sw.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:56.589Z]  "GET /ipfs-sw-117.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"
[2024-04-10T18:13:56.593Z]  "GET /ipfs-sw-561.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@SgtPooki SgtPooki marked this pull request as draft April 10, 2024 17:09
@SgtPooki SgtPooki mentioned this pull request Apr 10, 2024
3 tasks
@SgtPooki SgtPooki self-assigned this Apr 10, 2024
@SgtPooki SgtPooki requested review from 2color and lidel April 10, 2024 21:15
@SgtPooki SgtPooki marked this pull request as ready for review April 10, 2024 21:15
Copy link
Member Author

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

self review

Comment on lines +8 to +10
<Suspense fallback={<div>Loading...</div>}>
{currentRoute?.component != null && <currentRoute.component />}
</Suspense>
Copy link
Member Author

Choose a reason for hiding this comment

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

we could avoid any potential "not prod ready" issues with preact's Suspense by doing a simple useEffect wrapper of the async operation

src/components/Header.tsx Outdated Show resolved Hide resolved
src/components/input-validator.tsx Outdated Show resolved Hide resolved
export const ConfigProvider = ({ children, expanded = isLoadedInIframe }: { children: JSX.Element[] | JSX.Element, expanded?: boolean }): JSX.Element => {
const [isConfigExpanded, setConfigExpanded] = useState(expanded)
export const ConfigProvider = ({ children }: { children: JSX.Element[] | JSX.Element, expanded?: boolean }): JSX.Element => {
const [isConfigExpanded, setConfigExpanded] = useState(isConfigPage(window.location.hash))
Copy link
Member Author

Choose a reason for hiding this comment

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

could be updated to loaded dynamically but I don't think it would give us much for the effort

gotoPage(route?: string): void
}>({ currentRoute: undefined, gotoPage: () => {} })

export const RouterProvider = ({ children, routes }: { children: React.ReactNode, routes: Route[] }): JSX.Element => {
Copy link
Member Author

Choose a reason for hiding this comment

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

very basic hash-router implementation. I looked around for some and most were 200kb and unmaintained, or very large. All required some intimate knowledge or specific finagling for things we needed.

lmk if there's something I missed.

Comment on lines +2 to +3
import Form from '../components/Form.jsx'
import Header from '../components/Header.jsx'
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: rename these to snake-case, form.jsx and header.jsx

webpack.config.js Outdated Show resolved Hide resolved
@SgtPooki SgtPooki requested a review from a team April 12, 2024 18:45
Copy link
Member

@lidel lidel left a comment

Choose a reason for hiding this comment

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

Took this for a spin and works as expected, very cool improvement for initial and subdomain loads ❤️

Once this lands, it will be a good time to merge the new release too (#121), feel free to ship both!

@SgtPooki SgtPooki merged commit bdc2979 into main Apr 12, 2024
23 checks passed
@SgtPooki SgtPooki deleted the fix/dynamic-imported-ui-components branch April 12, 2024 23:46
@lidel lidel mentioned this pull request Apr 12, 2024
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

Successfully merging this pull request may close these issues.

chore: add chunk hash to files other than ipfs-sw-sw.js Subdomain loads require 30MiB download every time
2 participants