Server-Side Rendering API | Gatsby (2024)

Server-Side Rendering (SSR) allows you to render a page at run-time with data that is fetched when a user visits the page.The server generates the full HTML during HTTP request and sends it to the user. The API is focused on data fetching outside of the Gatsby data layer.

Note: This feature requires running NodeJS server.It is currently fully supported with gatsby serve and in Gatsby Cloud.

Creating server-rendered pages

You can create server-rendered pages the same way as regular pages (including using the File System Route API).

The main difference is that page component must export an async function called getServerData:

When a page component exports getServerData function, Gatsby treats all pages built with this componentas server-rendered.

The context parameter is an object with the following keys:

  • headers: The request headers
  • method: The request method, e.g. GET
  • url: The request URL
  • query: An object representing the query string
  • params: If you use File System Route API the URL path gets passed in as params. For example, if your page is at src/pages/{Product.name}.js the params will be an object like { name: 'value' }.

getServerData can return an object with several possible keys:

  • status (optional): The HTTP status code that should be returned. Should be a valid HTTP status code.
  • props (optional): Object containing the data passed to serverData page prop. Should be a serializable object.
  • headers (optional): Object containing headers that are sent to the browser and caching proxies/CDNs (e.g., cache headers).

Use serverData prop in React page component

The props object you return from getServerData gets passed to the page component as serverData prop.

Interplay with build-time GraphQL queries

Server-rendered pages also support regular Gatsby GraphQL page queries. The page query is executed at build time,and the data is passed to React component as a data prop on each render (along with the serverData prop).

Keep in mind that data will be the same every time the page renders, but serverData will change according to return value of your getServerData function.Runtime GraphQL queries are not supported yet.

Working with server-rendered pages locally

Server-rendered pages work with both gatsby develop and gatsby serve. The page will bere-generated on each request.

Using in production

Server-Side Rendering requires a running NodeJS server. You can put NodeJS running gatsby servebehind a content delivery network (CDN) like Fastly, however that also requires additional infrastructure (like monitoring, logging, and crash-recovery).

Complete setup with auto-scaling is available for you in Gatsby Cloud out-of-the-box.

How it works

For SSR every request only runs on server-side. On Gatsby Cloud the request is sent to a worker process that runs your getServerData function, passes this data to React component and returns HTML back to the user. By default, every request is a cache miss and for caching you’ll need to set custom HTTP Cache-Control headers.

When you directly visit a page you’ll get served the HTML. If you request a page on client-side navigation through Gatsby’s Link component the response will be JSON. Gatsby’s router uses this to render the page on the client.

This all happens automatically and you’ll only need to define a getServerData function in your page. You can’t export it from non-page files.

Additional Resources

Start building today on

Netlify!

Server-Side Rendering API | Gatsby (2024)
Top Articles
Types of Money | Commodity, Representative, Fiat, and Bank Money – Money Instructor
Discover the Top 6 Zodiac Signs Who Are True Sweethearts! | AstroTalk
Foxy Roxxie Coomer
Duralast Gold Cv Axle
Truist Bank Near Here
Is pickleball Betts' next conquest? 'That's my jam'
Chase Bank Operating Hours
Bucks County Job Requisitions
Los Angeles Craigs List
Gwdonate Org
Tracking Your Shipments with Maher Terminal
Shreveport Active 911
Kris Carolla Obituary
2016 Ford Fusion Belt Diagram
Gon Deer Forum
Bitlife Tyrone's
Overton Funeral Home Waterloo Iowa
Driving Directions To Bed Bath & Beyond
Clear Fork Progress Book
라이키 유출
Tygodnik Polityka - Polityka.pl
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Georgia Cash 3 Midday-Lottery Results & Winning Numbers
Cpt 90677 Reimbursem*nt 2023
Craigslist Ludington Michigan
Pixel Combat Unblocked
Pfcu Chestnut Street
Metro By T Mobile Sign In
Graphic Look Inside Jeffrey Dresser
Litter-Robot 3 Pinch Contact & DFI Kit
2016 Honda Accord Belt Diagram
Does Iherb Accept Ebt
Synchrony Manage Account
Myql Loan Login
Mcgiftcardmall.con
2008 DODGE RAM diesel for sale - Gladstone, OR - craigslist
Paperless Employee/Kiewit Pay Statements
Anhedönia Last Name Origin
Amc.santa Anita
Strange World Showtimes Near Century Stadium 25 And Xd
Port Huron Newspaper
Tacos Diego Hugoton Ks
Phmc.myloancare.com
Dying Light Mother's Day Roof
Das schönste Comeback des Jahres: Warum die Vengaboys nie wieder gehen dürfen
Mlb Hitting Streak Record Holder Crossword Clue
Random Warzone 2 Loadout Generator
Quest Diagnostics Mt Morris Appointment
Julies Freebies Instant Win
Fallout 76 Fox Locations
Goosetown Communications Guilford Ct
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 6387

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.