Server-Side Rendering: TL;DW - Supercharged

Chrome for Developers · Advanced ·🔧 Backend Engineering ·9y ago

Key Takeaways

Builds a backend for the advanced router using server-side rendering

Full Transcript

[Music] hello and welcome to the supercharged tldw last time Paul and I took our Advan through the code and build a back end for it that does serers side rendering I'm going to show you what that actually means in the browser so if you look at my screen you see nothing new it's still the advanc that we build and we can go to all the different subsections with the nice transitions that we have however if you look at the network panel in this case we are only transmitting minimal amounts of data Nam the exact data that we need to show what the section is about and yet if I refresh the page it still works because in that case the server has decided that the entire scaffolding needs to be sent and to how that works in detail let's go over to Theory Conor or as we call it the welcome to Theory Corner this node server is what we have built it's a web server written in node that takes the requests from the browser these requests could be for the homepage the about section the contact Section whatever we have in the site NV the server takes this request and reads a header file the contents and the foot file and assembles these three parts into response that get sent back to the to the browser however if there is a partial flag present in the query part that is instru instructing the server to only send back the contents which is what happens when we do dynamic in page navigation and this is actually what is called serers site rendering because if the initial request is for about the server will basically inject the contents for about into the return page into the scaffolding that is around it let's look at the actual code back in real world Corner uh let's jump straight into the code uh we're going to start at the bottom because that's the logical thing to do so we're using Express to uh start our web server and we are using Speedy because ironically the actual htb2 module doesn't work with Express but Speedy does and serves http2 and not speedy so that's kind of nice to have so we have our certificates here we're starting a web server that serves our app and we're binding it to Port 8081 the actual app itself consists of two handlers the one Handler that we're going to talk about in a bit that actually does the serite rendering and below that we're just using expresses static middleware that does nothing else and do straight up deliver files from disk over the network it's just static file delivery so let's talk about the actual Heart Of This Server which is the serers site rendering function uh it is a Handler and it takes care of all the requests that are for HTML websites these can have multiple shapes because it could be just a slash it could be/ index HTML or it could be any of the subsections with an optional index HTML at the end and we turn that into a regular expression and just put that into the express Handler and the first thing we do we are extracting the zeroth element from the parents object which is the first parenthesis from the reg expression meaning it is an empty string if you're on the homepage or is the name of the subsection if you're in any of the subsections and what we are figuring out next is if this is as I mentioned in theory Corner if it's a partial request or a full page request if there a partial request the only file that we need to read is the index HTML of that subsection so if we for example look in the index HTML of the about section it just has this one line that I showed in the def tools earlier if it is not a partial request we need to read the header file the partial file and the footer and assemble these to form the actual response what we're doing next is after successfully reading all these files is we're turning them into Strings because originally we get back buffers and we can't really work on buffers so we're turning them into Strings and then we're using handlebars the reason we are uh using handlebars which is a templating engine is because we have to do minor manipulations in the header to accommodate for the data we're going to inject and what I mean by that is let's take a look at the header file is that in the header file we have all the views that are currently not visible but these are four views and we only need three because one of them is actually going to be rendered by our serers side rendering logic so we wrote a little handlebar Handler that will exclude that remote view that is currently in use so if we are on the about page these three lines will not appear in the output because we're actually going to inject the data and this is what we're doing next uh we have these three files we're just assembling them step by step concatenating them and then sending them back to the client additionally we are also generating an eag this eag is there to allow efficient client side caching it is purely based on the contents that we have and we're just using a sha 256 checkm to build it based on the contents we uh adding these ech headers via the set method of our response and we also set sensible cach control headers these cash control headers mean that even though the client is allowed to cash them it has to revalidate every single time a request goes out for the same page in case an error happens in any of these steps and this is one of the beauty of promises we're just using the promise chains catch Handler to set the status to 500 meaning internal server error and appending our error string so that we as developer know what just went wrong and can fix our code luckily that didn't even happen on stream so we could never show off that it actually works and that's already it this is how you do server site rendering you take the index HTML you inject the content the user actually requested and then you send it back to the client the code I just skimmed over is on GitHub and you will find the link to the repo in the description of this video another link you can find in the description is the link to the fulllength live stream that Paul and I did where you can see how we buildt this entire thing step by step if you want to know more subscribe to this Channel on YouTube or follow us on Twitter thanks for watching see you next time the lights up there you should click them it makes you subscribe it's a good thing and also if you want to see me panic live on stream where I can't figure out why hb2 is not working go there that's the live stream and you will have an hour long of pure entertainment

Original Description

Surma recaps how he and Paul built a backend for the advanced router using server-side rendering! If you want to see the live stream in its full glory, you can watch it right here: https://goo.gl/vyF1Hb Find the code on Github here: https://goo.gl/adtyvO Watch more episodes of Supercharged here: https://goo.gl/WFD8py Subscribe to the Chrome Developers channel at http://goo.gl/LLLNvf
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Chrome for Developers · Chrome for Developers · 0 of 60

← Previous Next →
1 Polymer Performance Patterns (The Polymer Summit 2015)
Polymer Performance Patterns (The Polymer Summit 2015)
Chrome for Developers
2 Polymer Power Tools (The Polymer Summit 2015)
Polymer Power Tools (The Polymer Summit 2015)
Chrome for Developers
3 Chrome Dev Summit 2014 – Chrome Case Studies
Chrome Dev Summit 2014 – Chrome Case Studies
Chrome for Developers
4 Web Directions Code 2015 round up
Web Directions Code 2015 round up
Chrome for Developers
5 Maintainable Code - HTTP203
Maintainable Code - HTTP203
Chrome for Developers
6 iron-ajax… wat?! -- Polycasts #26
iron-ajax… wat?! -- Polycasts #26
Chrome for Developers
7 The Guardian - Supercharged
The Guardian - Supercharged
Chrome for Developers
8 ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
Chrome for Developers
9 #AskPolymer: Rob answers all the questions ever -- Polycasts #27
#AskPolymer: Rob answers all the questions ever -- Polycasts #27
Chrome for Developers
10 The Future of JavaScript - HTTP203
The Future of JavaScript - HTTP203
Chrome for Developers
11 Data Binding 101 -- Polycasts #28
Data Binding 101 -- Polycasts #28
Chrome for Developers
12 The Guardian part 2 - Supercharged
The Guardian part 2 - Supercharged
Chrome for Developers
13 The Future of Web Audio: with Chris Wilson and Chris Lowis
The Future of Web Audio: with Chris Wilson and Chris Lowis
Chrome for Developers
14 Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome for Developers
15 Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Chrome for Developers
16 #AskPolymer: How do you make the show? -- Polycasts #29
#AskPolymer: How do you make the show? -- Polycasts #29
Chrome for Developers
17 Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Chrome for Developers
18 Binding to Objects -- Polycasts #30
Binding to Objects -- Polycasts #30
Chrome for Developers
19 Player FM - Supercharged
Player FM - Supercharged
Chrome for Developers
20 Where’s the Designer? #AskPolymer -- Polycasts #31
Where’s the Designer? #AskPolymer -- Polycasts #31
Chrome for Developers
21 Jake Beats Wikipedia - HTTP203
Jake Beats Wikipedia - HTTP203
Chrome for Developers
22 Supercharged Observers! -- Polycasts #32
Supercharged Observers! -- Polycasts #32
Chrome for Developers
23 Jai's Web blog - Supercharged
Jai's Web blog - Supercharged
Chrome for Developers
24 Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Chrome for Developers
25 What about internationalization? #AskPolymer -- Polycasts #33
What about internationalization? #AskPolymer -- Polycasts #33
Chrome for Developers
26 Developing for Billions (Chrome Dev Summit 2015)
Developing for Billions (Chrome Dev Summit 2015)
Chrome for Developers
27 Google+ Performance Improvement Comparison
Google+ Performance Improvement Comparison
Chrome for Developers
28 Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Chrome for Developers
29 Progressive Web Apps (Chrome Dev Summit 2015)
Progressive Web Apps (Chrome Dev Summit 2015)
Chrome for Developers
30 Instant Loading with Service Workers (Chrome Dev Summit 2015)
Instant Loading with Service Workers (Chrome Dev Summit 2015)
Chrome for Developers
31 Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Chrome for Developers
32 Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Chrome for Developers
33 Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Chrome for Developers
34 Polymer - State of the Union (Chrome Dev Summit 2015)
Polymer - State of the Union (Chrome Dev Summit 2015)
Chrome for Developers
35 Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Chrome for Developers
36 Introduction to RAIL (Chrome Dev Summit 2015)
Introduction to RAIL (Chrome Dev Summit 2015)
Chrome for Developers
37 DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
Chrome for Developers
38 RAIL in the real world (Chrome Dev Summit 2015)
RAIL in the real world (Chrome Dev Summit 2015)
Chrome for Developers
39 #ChromeDevSummit talks are up - W00T! -- Polycast #34
#ChromeDevSummit talks are up - W00T! -- Polycast #34
Chrome for Developers
40 V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
Chrome for Developers
41 Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Chrome for Developers
42 Owning your performance: RAIL (Chrome Dev Summit 2015)
Owning your performance: RAIL (Chrome Dev Summit 2015)
Chrome for Developers
43 HTTP/2 101 (Chrome Dev Summit 2015)
HTTP/2 101 (Chrome Dev Summit 2015)
Chrome for Developers
44 Leadership Panel (Chrome Dev Summit 2015)
Leadership Panel (Chrome Dev Summit 2015)
Chrome for Developers
45 Build Processes, Totally Tooling Tips (S2, Ep 5)
Build Processes, Totally Tooling Tips (S2, Ep 5)
Chrome for Developers
46 Accessibility (Chrome Dev Summit 2015)
Accessibility (Chrome Dev Summit 2015)
Chrome for Developers
47 Binding to Arrays -- Polycasts #35
Binding to Arrays -- Polycasts #35
Chrome for Developers
48 HTTP2 - HTTP203
HTTP2 - HTTP203
Chrome for Developers
49 Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome for Developers
50 Call For Submissions - Supercharged
Call For Submissions - Supercharged
Chrome for Developers
51 Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Chrome for Developers
52 Testing AJAX with Web Component Tester -- Polycasts #37
Testing AJAX with Web Component Tester -- Polycasts #37
Chrome for Developers
53 Slack: Extended Xmas Special - Supercharged
Slack: Extended Xmas Special - Supercharged
Chrome for Developers
54 Browser testing with Travis & Sauce Labs -- Polycasts #38
Browser testing with Travis & Sauce Labs -- Polycasts #38
Chrome for Developers
55 Optimize for production with Vulcanize -- Polycasts #39
Optimize for production with Vulcanize -- Polycasts #39
Chrome for Developers
56 Highlights from Chrome Dev Summit 2015
Highlights from Chrome Dev Summit 2015
Chrome for Developers
57 Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome for Developers
58 Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Chrome for Developers
59 How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
Chrome for Developers
60 Colors – DevTools Tonight #0 (Pilot)
Colors – DevTools Tonight #0 (Pilot)
Chrome for Developers

Related Reads

📰
Java: Word to TXT Conversion
Learn to convert Word documents to plain text in Java, a crucial task in software development
Dev.to · Jeremy K.
📰
The New HTTP QUERY Method: How to Use It in Node.js and Express Today
Learn to use the new HTTP QUERY method in Node.js and Express to send requests with a body while keeping data safe
Dev.to · Dev Encyclopedia
📰
2 @Transactional Traps That Catch Even Senior Java Developers in interviews
Learn to avoid common @Transactional traps in Java to improve your coding skills and ace technical interviews
Dev.to · Nikhil Kamani
📰
10th Anniversary of the Excelize Open Source, New ersion 2.11.0 Released
Celebrate the 10th anniversary of Excelize, an open-source library for working with XLSX files, and learn how to use its new version 2.11.0 for improved productivity
Dev.to AI
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →