SvelteKit split second unstyled html - sveltekit

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).

Related

How can I know if everything actually optimized?

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.

Material UI Components not Rendering when built with express app (Production)

I'm building a frontend with React and Material UI, and everything is running smoothly when I run npm start.
I also have an express app that serves a production build of this frontend (I run npm run-script build and take the prod build and place it in my express app's target destination). When I run this express app locally (with node app.js), everything is rendering and operating smoothly there as well.
Now, when I go onto my Amazon Lightsail linux instance that I was using to deploy, and run the express app over there with node app.js, the app runs, but all of the material-ui components do not render at all. All I see is the background.
Initial investigation led me to believe that within ReactDOM.render, the UI I was rendering needed to be wrapped in MuiThemeProvider. Have tried this to no avail. Its led me to a couple questions.
CSS
In my production build (build->static->css) all I have is one css file, which only lists the background. This makes sense as to why I can at least see the background when I deploy. My question here is, I'm using makeStyles and withStyles in my jsx a lot to create the bulk of my classes. In my production build, should these get compiled down into css?
My other question is if it should get compiled down to css, why am I able to see all of my material ui components when I run the express app locally on port 9000, but can only see the background when I serve it onto my static port on lightsail? It's the same build, so I almost find it hard to believe there's something wrong with my build.
Anyone experience anything like this? Any tips or advice on how to debug would be appreciated, I've been stuck for quite some time.

How to prevent `Navigated to http://localhost:3000/` for electron-react app

Typescript just relase 3.7.3 with optional chaining, so I wanted to upgrade a project for this feature.
However, after runing yarn upgrade --latest, and doing yarn start, the app will reload infinitely.
After couple days of debugging. This is what I find out, it seems like if anything changed recently on /public folder, then the app will be Navigated to http://localhost:3000/ which will cause it to reload infinitely due to the design of my app.
The project was working for previous dependencies, so I am not sure whethere it's a react or electron upgrade that they changed it such that the app will watch for /public folder. When public folder changed, and causing the app to navigate to localhost:3000. Anyone know what's the cause of this phenomena? And the reason behind this change, and also how to pass around this?
By the way, my app was a simple epub reader application. Currently, my app will load all the images, css files and stored them to public folder (delete those files on unmount). Thus the app can load these images, stylesheets. (I just tried to store these files just inside the src folder, and it seems react won't be able to load these resources dynamically).

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.

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)

Resources