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

Reduce first load JS on blog posts by 53% #15

Merged
merged 4 commits into from May 2, 2021

Conversation

fracture91
Copy link
Contributor

@fracture91 fracture91 commented Apr 27, 2021

There are a couple small changes you can make to the react-syntax-highlighter imports that massively improve the bundle size for blog post pages. There are some minor tradeoffs, but I think this is a better default behavior.

I wrote a blog post about this with screenshots of the PageSpeed score impact on my repo along with some other changes (69 -> 98). This is basically a backport of some commits in this PR. The bundlewatch output from reverting one of the commits can be seen here.

This could also be aliased similarly to "ts" in Code.tsx.  Anyway, without
this fix you'll get errors in the following commits.
This component defers loading most of the syntax highlighting code until
after the page loads, and it will only download syntaxes that are used on
the page.

It comes with a tradeoff though: syntax highlighting no longer works with
client JS disabled, i.e. the code blocks sent from the server are not
highlighted.
PrismLight will have access to all syntaxes on the server side and provide
syntax-highlighted code blocks in HTML.  PrismAsyncLight will download
only the necessary syntaxes on the client to hydrate the code blocks.

This seems to work fine right here, but I noticed some server/client
mismatches in dev on my own site.  There are PRs up to fix those problems:

react-syntax-highlighter/react-syntax-highlighter#361
react-syntax-highlighter/react-syntax-highlighter#362
@colinhacks colinhacks merged commit 8c40328 into colinhacks:master May 2, 2021
@colinhacks
Copy link
Owner

Sounds good to me!

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.

None yet

2 participants