Hello everyone, welcome to this video I wanted to talk about the astro rendering modes We talked about it in the December news episode released last week and we talked about astro at the release of the V4 We were not very clear about the rendering modes I wanted to come back on it and do a demo of the different rendering modes to understand how astro works I will switch to the screen sharing mode We will go directly into the code, we will not try to understand I will go to my VS Code editor I installed an
astro, I have the basic one I just modified it a little bit It is launched normally We have a home, a blog section I added a page with server side rendering I added a little bit of time, it will look for some boring sentences on an API I also have a streaming mode We have a bit of streaming in astro There is a sentence here, loaded via an API The sentence comes after the text This is what we call streaming, the page is displayed and some text comes after This is to push the thing The
first mode on astro is the static mode When we install an astro by default it is created like this, it is the static mode We will generate static pages in HTML It is a static mode The site is generated, you put it on a server It is HTML and it works We will do a little demo It is in development mode It is rendered little by little To switch to static mode, you just have to build I do a npm run build It generates all the static pages It displays exactly what it is generating It
will install it in dist You have all the images, the different pages With folders that correspond to the path We have HTML files in it We have the fonts, the blogs HTML in each folder The about page, the index The home page The preview mode to launch astro I am in production mode I have HTML files We can see The SSR page that calls an API To generate all the static pages The page is generated when the build is done The information doesn't change It is the principle of static The pages are generated in the
deployment The sentence is always the same There is no page regeneration It is perfect, you have a small site It can work for 300, 400, 1000 pages It lengthens the time of deployment It is not annoying It works very well for sites that change little information When you publish a blog, it doesn't change You deploy, it works You don't need a server, you can put it anywhere Without PHP, without JS, it runs You just need an apache or whatever Then, in static mode We have a problem We have a site with a lot of pages
We can imagine a site with 5,000, 10,000 pages When you deploy the site, you need to generate all the pages In static, you need to generate them It will take a lot of time If you call an API, a CMS, it can take a lot of time It becomes a problem It is a long deployment, you need to make a change It takes 5, 10 or 20 minutes They installed a new mode It is server mode If I go to the doc We have output mode We have the default static We have server mode and
hybrid We will go to server mode Hop Server In server mode, you need an adapter When you go to server mode, there is a node server To generate the pages If you want to host on a normal server You need to install the node adapter You can use Vercel, Netlify or whatever In the doc, there are different official adapters I already installed the node adapter To save time Now, I will rebuild I will have an error The blog mode does not work Because we have a small difference I will comment the code In static mode,
we have a function called get_static_path It is the same as on Next.js It generates the paths of the pages This function is not useful In SSR mode I can remove this I also have a difference We have collections To display content The difference is that the information of the post Is stored in the properties of the page It does not work in SSR mode In SSR mode, we have a different system It gathers the slug of the URL If there is no slug, we make an error Then, it will take the collection of the blog
It will try to find the one that corresponds to the slug It will collect the information to display it There are also the front-matter data The system is a little different There will be no error It does not say anything I launch the preview mode We see that a node server is running All my pages are generated by the server When we switch to server mode We see the server side rendering It just called the API It displayed the sentences We see my text and my sentence This was to show you the streaming mode We
have nothing to do We call the API and we use await It will display what is already available It displays the sentence This is what we call the streaming mode It's good to have a faster rendering What I wanted to show you It's important to understand By default, on the server mode Everything is rendered in server If there is a page that I do not want to render I want a static page I have to indicate it I do an export Prerender true I indicate that this page I do not want a server rendering But
a static page Let's start again It generates the static page All the others are in SSR I launch the preview There will be no difference Except that about is not available I missed the page This one is faster It's a HTML file To make a better demo I will do this on the server side rendering page It's a call that displays the sentences It will be more talkative I do my build It will generate 2 pages 2 pages are rendered I launch the preview You will see the big difference The page is static Why do
I show you this? Because the server mode By default, it will put all the pages in server Then you have to indicate Those you do not want in server And you want a static rendering Except that the hybrid mode is a little different I cut my server We go in the config We go in hybrid mode There is a subtlety If I launch the page It should not change much The server side rendering is still a little long I put a slip on it The difference Is that the pages are rendered In static mode by
default And the opposite is to indicate Which page you want in server I prefer this hybrid mode If I go back to my server side rendering And I do a false pre-render It will become a page rendered in server I will do the streaming Let's say I have 2 pages I want this one to be dynamic And rendered each time I want to have a refreshed page I launch the preview I go to the mode The home is static I did not say a false pre-render The blog is static The SSR and the streaming I
did not say a false pre-render Here we have a page rendered in server It renders a part of the page And then it will look for the sentences via the API And the streaming mode, we see the sentence that comes right after This is what you have to understand In server mode All the pages are rendered in server You have to indicate That you want a page in static mode You will put a false pre-render It will render the page during the build In hybrid mode, all the pages are static You have to indicate which
page you want in server mode This hybrid mode is more logical We will want some pages in server And the majority of pages in static That's why we use Astro The hybrid is more suitable for many cases Where we will want to render some dynamic elements I hope you understood the difference between these modes By default it is static Astro was created like that Then we had the server mode Which allows you to pass all the page in server And then you indicate which elements you want in static mode And the last mode is the
hybrid mode Which is more suitable for most cases We use Astro to render static elements Sometimes we need a page with an API For me it is the best of the two worlds We have static and server side rendering Thank you for watching this video See you soon for another video