How can I know if everything actually optimized? - reactjs

I built a simple react app using Gatsby. All that the app does create a calender and show it to the screen. When I checked the performance of the app using LightHouse, I got 90% on desktop perf and 31% on mobile perf.
This is a bare bone site. I thought Gatsby would convert my react code to HTML and that is supposed to load blazing fast...
So I checked the html of the page, </script></head><body><div id="___gatsby"></div><script src="/polyfill.js" nomodule=""></script><script src="/commons.js"></script></body></html>
It looks like the whole page just runs from JS script; nothing is actually converted to HTML.
What did I do wrong? I ran the site using npm run develop?

When you run gatsby develop all page requests are served the barebones HTML shell that loads a development-targeted version of Gatsby that will render everything client-side only and integrates hot-module reloading1. No minification is performed on your source files this way, React is in development mode, and there are streaming connections opened to the dev server to listen for changes.
When you run gatsby build, Gatsby will create static HTML file entry points to each of your pages that contain the pre-rendered page in an optimized format. Each of your plugins should also produce optimized output, such as inlining critical CSS or generating minified CSS classnames. JavaScript will be tree-shaken, minified, and bundled in chunks by webpack, and React will be loaded in production mode.
If you'd like to test the performance of your Gatsby site before deploying, you can get a rough indicator by running gatsby build and then gatsby serve, then running your Lighthouse audit on that page. Keep in mind that this won't test the network or server performance of your live site, so it isn't going to match precisely (but should at least catch content related issues).
1 This is actually set to change starting with v2.3.0, which enables SSR during development.

Related

SvelteKit split second unstyled html

I created an base app though the sveltekit cli commands. The options I chose are scss and typescript. At soon as I started the application for a split second I saw unstyled html. This happens every time and with every project i create. I did some testing and it seems like the css(app.scss) is loaded after the html(localhost). Another this that seems consistent is that is happens every time i reload the page, but not with navigating. This indicates that it is probably server-side.
In my mind the html and css should load in the same file, though SvelteKit might have a different approach.
I had the same issue with Sapper and solved that. But I forgot how I fixed it. Also with the new SvelteKit setup many things are different. Do you guys know how to fix this?
Thanks in advance
SvelteKit does not bundle everything together during development mode and injects support for HMR. This may result in flashes of unstyled content.
The flashes of unstyled content should go away in production mode, when you deploy with an adapter.
This stock SvelteKit template does not flash unstyled content. The only change was changing the adapter from the node adapter to the static adapter so it could be hosted on Netlify. (I have also confirmed with the Netlify adapter, so it's not a difference between static and dynamic.)
How to test with the stock SvelteKit template:
npm init svelte#next
yarn
yarn dev --open # Dev server: flashes of unstyled content.
yarn build
cd build
node index.js # Production server: no flashes of unstyled content.
Explanation from SvelteKit announcement:
Right now, we're seeing the rise of the unbundled development
workflow, which is radically simpler: instead of eagerly bundling your
app, a dev server can serve modules (converted to JavaScript, if
necessary) on-demand, meaning startup is essentially instantaneous
however large your app becomes.
...
That's not to say we're abandoning bundlers altogether. It's still
essential to optimise your app for production, and SvelteKit uses
Rollup to make your apps as fast and lean as they possibly can be
(which includes things like extracting styles into static .css files).

Edit Files After Build, React

I built an app in React with create-react-app. Just JavaScript, CSS, HTML & React. I ran npm build then deployed the app to Netlify.
I want to go back and edit some CSS. So, I cd into the directory from my laptop and deploy on localhost:5000. I open VS Code and make changes however none of the changes are reflected in the browser # localhost:5000.
When I was building the app, the way I had it set up allowed me to view each change immediately in the browser when I save the file.
Are files editable after you run npm build? What am I missing here?
When you run a build on a react app (or any other app) code will be converted from es6 to es5 and then probably minified (depends on webpack config) so code is unreachable and you need .map files to debug code in production environment.
So the most clean way to act on deployed code is to make a new build with updated features and deploy again the frontend.
In local development react boilerplates usually make intensive use of hot-reload, a plugin that allow code to be hot replaced while app is running.
Built application instead load chunks of JS files once and CACHE it. So in order to see your changes you have to clean cache or force a refresh (home+F5 on windows, CMD+R on OSX) to be sure that your changes are visible.
Despite this I discourage to edit the build files. When you have to update the code stay on development mode, before deploy, build your code and test it live.
You could create some files outside the src folder and access them with fecth from app.js or even import them from index.html ... so if you wanted to change something you could do it without having to do a build again.

Does a React application HAVE to run on its own server process?

I come from a background in Angular, but I wanted to start learning React. I want to make a React front-end for a nodejs Express web service.
In Angular, I could simply build the source code into a static folder with a standard index.html, and it can be deployed on any web server. With React however, every tutorial I see is about running React in its own server using create-react-app and npm start.
I know you can also just use script tags to import the React framework in a static page, but then you can't use JSX syntax, and I would like to. Is it possible to simply build a React project into a static folder with an index.html that can be deployed on any web server?
Yep, you can do what you're describing. If you want to use JSX syntax, you'll need Babel, which transpiles it into standard JavaScript.
You can avoid the complexities of setting it up by using create-react-app to create the app, then running npm build instead of npm start. This will compile everything into a build directory, complete with index.html.
CRA uses its server for development purposes. You don't need CRA for using React of course. But, as your app getting bigger and bigger you will need some more extra tools. For example you want to see your code instantly without refreshing your browser. Here comes the dev server and hot reloading.
CRA uses Webpack behind the scenes. It is a great tool for bundling (obviously) all your files (including css), minifying, uglifying, optimizing your code etc.
For simple code or testing purposes using one file and attaching React and Babel to your file could be enough but for real apps you will need more. Either you will use something like Webpack on your own or you will use CRA and let it do all the extra stuff for you.

Difference between production and development build in ReactJS

Recently I started learning react and I saw a tutorial where they used Webpack to create the production and development builds. But there was no explanation on what the difference between those two builds is and which one you have to use when. I searched the internet but didn't find anything that helped me. Does anyone have a tutorial or an explanation that I missed/didn't read?
The development build is used - as the name suggests - for development reasons. You have Source Maps, debugging and often times hot reloading ability in those builds.
The production build, on the other hand, runs in production mode which means this is the code running on your client's machine. The production build runs uglify and builds your source files into one or multiple minimized files. It also extracts CSS and images and of course any other sources you're loading with Webpack. There's also no hot reloading included. Source Maps might be included as separate files depending on your webpack devtool settings.
What specifically separates production from development is dependent on your preferences and requirements, which means it pretty much depends on what you write in your Webpack configuration.
The webpack-production documentation is very straight-forward.
Also, the article Webpack 3 + React — Production build tips describes the process of creating production builds for React with Webpack pretty good.
The very basic difference is that Production Build has ugly, minified(compressed) version of your javascript code, so this makes rendering of file on end user's browser very quick and performance enhancing.
You can also verify if production build is being used in the website by applying a google plugin extension, which when activated on your browser, will always tell you if the website is using react js on the front end and also tells whether the build type is production or development.
when react is development build,
production-ready versions of React and React DOM as single files are available as well,
<script src="https://unpkg.com/react#16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.production.min.js"></script>
NOTE: Remember that only React files ending with .production.min.js are suitable for production.
The production and development build come into the picture just because of performance impact in real life deployed the application. Also, it happens that the location where the application is deployed is another continent altogether, so rendering development build js files on UI will take a hell of a time as compared to production version which is very crisp, compact, compressed, uglified for better user experience and loading on UI. for information CLICK HERE
react.development.js provides us extra features like debugging, hmr(Hot module reloading) and lots of other stuffs that you might
use while developing app with the help of bundlers like webpack, parcel, vite. This bundler bundles and minifies our code to be
deployed on production
These minified files will be deployed on production which removes lots of unnecessary files which will not be used by our app
for this we have react.production.js to make our much faster(as bundlers and lots of other files have done there work and are not required now)

Can you and How to serve react js or angular js without a static sever?

How to use the production version of a react or angular project and run without a local server?
( like opening from index.html or something similar)
I want to make a static web application that uses react or angular 2 as a starting point. Eventually, this will be dynamic so it make sense to take advantage of either.
I don't mind using local http-serve to serve the html and css pages in development but due to some restrictions i won't be able to set up a local server or run npm start from the terminal.
I did a lot of research but never found a definitive answer as to
So like How to use the production version of a react or angular project and run without a local server?
( like opening from index.html or something similar
React (same as Angular) is a client-side library/framework. So all you have to do is to bundle your application to a single .js file. That file will probably include React/Angular and you may just load it in your HTML file. You definitely don't need a http-serve for that. It is possible to deploy your index.html and your bundle.js file to a shared hosting and just load the app.

Resources