- [Instructor] One of the big promises
of the HTTP 2 protocol was the introduction of server push,
allowing the server to literally push assets to the client
before those assets were requested.
Here's the idea.
Traditionally, when a request for a web page
goes to the server,
the server returns the web page, the HTML document,
and the browser starts rendering that HTML document
and discovers that, "Hey, I need a CSS file."
So it sends another request to the server
and the server returns the CSS file.
Then the browser discovers,
it also needs a JavaScript file
and another request is sent to the server
and another file is sent back from the server,
and all of this takes a long time.
Server push allows you to configure the server to say,
"If you're requesting this HTML document,
"I know you'll also need this CSS file
"and this JavaScript file, so I'll send them along as well."
And a request for one file
results in three files being returned.
When the browser discovers it needs
the CSS and JavaScript file in this circumstance,
they are already in the browser cache.
Server push is typically configured
using the link HTTP header, and it looks like this.
You say link and then you define the file you want.
You set relationship to preload as,
and then the type of file it is,
style or image or whatever it is,
and then the file will automatically be preloaded.
Same with images,
you define the image, rel=preload; as=image.
When the HTTP request is sent in this circumstance
the link header has a list of these assets to be pushed.
And this is where things get a little bit sticky.
To use server push,
you have to be able to manipulate
the HTTP headers in the document,
and this is not done using HTML.
It has to be done at the server level.
If you're using good old fashioned PHP,
you can easily manipulate the headers on the fly
because your pages are generated on the server.
But for static site generators, for example,
this gets a lot more complicated.
You have to configure the server to know
which files should call which other files to be pushed,
and it becomes a huge logic chain that you need to set up.
Additionally, you have to be very specific about
what resources you're pushing to the client
to avoid pushing too much
or pushing irrelevant content to them.
So there are performance benefits here,
but they come at quite a lot of development cost
and they can also easily open the door
to accidentally pushing too much content to the client.
The bottom line is you can absolutely use server push
in modern applications
and you should do so in situations where it's needed,
but even a basic example of how to do this
in a modern application falls well outside
the scope of this course because the approach is tied
to the server infrastructure in use.