Rails Using.a Javascript Client to Select a File Than Have the Server Upload the File

In that location is something going on within the front-cease customs recently. Server-side rendering is getting more and more traction thanks to React and its born server-side hydration characteristic. Merely it's not the only solution to deliver a fast experience to the user with a super fast time-to-first-byte (TTFB) score: Pre-rendering is also a pretty good strategy. What's the divergence betwixt these solutions and a fully client-rendered application?

Client-rendered Application

Since frameworks like Angular, Ember.js, and Backbone exists, front-end developers take tended to render everything customer-side. Thanks to Google and its ability to "read" JavaScript, information technology works pretty well, and it'due south even SEO friendly.

With a client-side rendering solution, yous redirect the request to a single HTML file and the server will deliver it without any content (or with a loading screen) until you fetch all the JavaScript and allow the browser compile everything earlier rendering the content.

Nether a good and reliable internet connection, it'south pretty fast and works well. But it tin can be a lot better, and it doesn't have to exist difficult to make it that way. That'due south what we will see in the following sections.

Server-side Rendering (SSR)

An SSR solution is something nosotros used to practice a lot, many years ago, but tend to forget in favor of a client-side rendering solution.

With sometime server-side rendering solutions, y'all built a web page—with PHP for example—the server compiled everything, included the data, and delivered a fully populated HTML page to the client. It was fast and constructive.

But… every fourth dimension you navigated to another road, the server had to do the work all once again: Go the PHP file, compile it, and evangelize the HTML, with all the CSS and JS delaying the folio load to a few hundred ms or fifty-fifty whole seconds.

What if you could do the starting time folio load with the SSR solution, and and then utilise a framework to do dynamic routing with AJAX, fetching just the necessary data?

This is why SSR is getting more and more traction within the community because React popularized this problem with an like shooting fish in a barrel-to-use solution: The RenderToString method.

This new kind of web application is called a universal app or an isomorphic app. There's still some controversy over the exact meanings of these terms and the relationship between them, but many people utilize them interchangeably.

Anyway, the advantage of this solution is being able to develop an app server-side and client-side with the same code and deliver a actually fast experience to the user with custom data. The disadvantage is that you need to run a server.

SSR is used to fetch information and pre-populate a page with custom content, leveraging the server's reliable cyberspace connection. That is, the server's own internet connection is better than that of a user with lie-fi), so it's able to prefetch and amalgamate data before delivering information technology to the user.

With the pre-populated data, using an SSR app tin can besides fix an issue that customer-rendered apps have with social sharing and the OpenGraph system. For example, if you have merely one index.html file to deliver to the customer, they will simply have one type of metadata—most probable your homepage metadata. This won't be contextualized when you desire to share a different road, so none of your routes will be shown on other sites with their proper user content (description and preview picture) that users would desire to share with the world.

Pre-rendering

The mandatory server for a universal app tin be a deterrent for some and may be overkill for a small awarding. This is why pre-rendering can exist a actually nice alternative.

I discovered this solution with Preact and its own CLI that allows you to compile all pre-selected routes so you can store a fully populated HTML file to a static server. This lets you deliver a super-fast experience to the user, thanks to the Preact/React hydration function, without the need for Node.js.

The grab is, considering this isn't SSR, you don't accept user-specific data to show at this point—information technology's only a static (and somewhat generic) file sent directly on the first request, every bit-is. And so if you have user-specific data, hither is where you tin integrate a beautifully designed skeleton to bear witness the user their data is coming, to avoid some frustration on their function:

Using a document skeleton as part of a loading indicator

There is another catch: In order for this technique to work, you lot still need to have a proxy or something to redirect the user to the right file.

Why?

With a unmarried-folio awarding, you demand to redirect all requests to the root file, and so the framework redirects the user with its born routing organisation. So the start page load is always the same root file.

In order for a pre-rendering solution to piece of work, you demand to tell your proxy that some routes need specific files and non always the root index.html file.

For example, say you have iv routes (/, /almost, /jobs, and weblog) and all of them have different layouts. You lot demand 4 different HTML files to evangelize the skeleton to the user that volition then permit React/Preact/etc. rehydrate it with information. And then if you lot redirect all those routes to the root index.html file, the page will have an unpleasant, glitchy feel during loading, whereby the user will see the skeleton of the incorrect page until it finishes loading and replaces the layout. For example, the user might see a homepage skeleton with only one column, when they had asked for a dissimilar page with a Pinterest-similar gallery.

The solution is to tell your proxy that each of those 4 routes needs a specific file:

  • https://my-website.com → Redirect to the root index.html file
  • https://my-website.com/about → Redirect to the /about/index.html file
  • https://my-website.com/jobs → Redirect to the /jobs/index.html file
  • https://my-website.com/web log → Redirect to the /web log/index.html file

This is why this solution can be useful for small applications—yous can see how painful information technology would be if you had a few hundred pages.

Strictly speaking, it'due south not mandatory to do it this way—you could just use a static file directly. For example, https://my-website.com/most/ will piece of work without any redirection because it will automatically search for an index.html inside its directory. Simply you demand this proxy if you have param urls—https://my-website.com/profile/guillaume will need to redirect the asking to /profile/alphabetize.html with its own layout, considering profile/guillaume/index.html doesn't exist and will trigger a 404 error.

A flowchart showing how a proxy makes a difference in a pre-rendering solution, as described in the previous paragraph

In brusk, there are 3 bones views at play with the rendering strategies described to a higher place: A loading screen, a skeleton, and the full folio once it's finally rendered.

Comparing a loading screen, a skeleton, and a fully-rendered page

Depending on the strategy, sometimes we use all iii of these views, and sometimes nosotros jump direct to a fully-rendered page. Just in one use example are we forced to use a different approach:

Method Landing (eastward.g. /) Static (eastward.yard. /nigh) Fixed Dynamic (e.g. /news) Parameterized Dynamic (e.grand. /users/:user-id)
Customer-rendered Loading → Full Loading → Full Loading → Skeleton → Full Loading → Skeleton → Full
Pre-rendered Total Full Skeleton → Full HTTP 404 (folio not found)
Pre-rendered With Proxy Full Full Skeleton → Full Skeleton → Full
SSR Full Full Full Full

Client-but Rendering is Ofttimes Non Enough

Client-rendered applications are something we should avert now considering we can do meliorate for the user. And doing improve, in this example, is as like shooting fish in a barrel as the pre-rendering solution. It's definitely an comeback over client-simply rendering and easier to implement than a fully server-side-rendered application.

An SSR/universal application tin be really powerful if y'all take a big awarding with a lot of different pages. It allows your content to be focused and relevant when talking to a social crawler. This is also true for search engine robots, which now have your site's functioning into account when ranking it.

Stay tuned for a follow-upward tutorial, where I will walk through the transformation of an SPA into pre-rendered and SSR versions, and compare their performance.

johnstonparse1947.blogspot.com

Source: https://www.toptal.com/front-end/client-side-vs-server-side-pre-rendering

0 Response to "Rails Using.a Javascript Client to Select a File Than Have the Server Upload the File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel