Dynamic Static Sites

A presentation at API World in September 2018 in San Jose, CA, USA by Shy Ruparel

Slide 1

Slide 1

Dynamic Static Sites

Slide 2

Slide 2

Shy Ruparel Developer Evangelist | Contentful @ShyRuparel He/Him

Slide 3

Slide 3

Don’t let a CMS get in the way of shipping software. Contentful provides a content infrastructure that enables teams to power content in any digital product.

Slide 4

Slide 4

What we’ll do today • A brief history of static website • Why static sites are awesome • Why and how to combine static sites and serverless

Slide 5

Slide 5

Static Websites A brief history

Slide 6

Slide 6

THE FIRST WEBSITE WAS A STATIC SITE

Slide 7

Slide 7

LET’S DEVELOP LIKE ITS 1991 • Every website was static • Layout wasn’t a thing everything was one vertical flow. • Link management was manual

Slide 8

Slide 8

Browser Webserver Workstation Developer

Slide 9

Slide 9

FIRST WEBSEARCH

Slide 10

Slide 10

FIRST WEBCOMIC

Slide 11

Slide 11

THINGS GOT DYNAMIC • CGI was introduced in 1994 • Rasmus Lerdorf releases his “Personal Home Page Tools” in 1995 • Brendan Eich writes the prototype for JavaScript in 1995 Photo by Pamela Saunders on Unsplash

Slide 12

Slide 12

Database Browser Webserver Workstation Developer

Slide 13

Slide 13

Fun Fact: SQL is 44 years old

Slide 14

Slide 14

E-COMMERCE IS TAKING OFF

Slide 15

Slide 15

YOUR OWN SPACE

Slide 16

Slide 16

IT’S A BUSINESS NOW • Microsoft ASP was releases in 1996, Sun JSP in 1999 • Hosting a dynamic, CMS, based website became easy • Microsoft FrontPage and Marcomedia Dreamweaver released in 1996 and 1997 Photo by rawpixel.com on Unsplash

Slide 17

Slide 17

Editors Browser Application server Database Load balancer SCM Developer

Slide 18

Slide 18

APPS ON THE WEB

Slide 19

Slide 19

I GOT THE POWER • XMLHttpRequest was part of IE5 in 1999 • SPA have been discussed since 2003 • jQuery was published in 2006, AngularJS in 2009 Photo by Brooke Lark on Unsplash

Slide 20

Slide 20

Modern static site builders

Slide 21

Slide 21

GITHUB

Slide 22

Slide 22

Slide 23

Slide 23

Editors Browser Static Site Host Build server SCM Developers

Slide 24

Slide 24

Middleman Hexo

Slide 25

Slide 25

IT JUST MAKES YOUR LIFE EASIER No Security issues Trivial caching Without a server-side application or CDNs are trivial to set up. Since every database the attack surface is minimal. file gets delivered to every user as-is, no complex logic is necessary. Great performance Easy maintenance Shuffling bytes from storage to the No random 500 errors, no dropped network is what servers are great at. database connections.

Slide 26

Slide 26

“This project is too complex. A static site won’t do.“

Slide 27

Slide 27

Common requirements 01 “Our editors don’t know git, 02 “We have a lot of content, 03 “Our contact forms needs 04 Photo by Štefan Štefančík on Unsplash they need a GUI.” we need full-text search.” to send an email.” “We’re running an online shop. You can’t do that with a static site.”

Slide 28

Slide 28

A CLOUD AND AN API WALK INTO A BAR • AWS got started in 2002, Lambda added in 2014 • Roy Fielding defines REST in his 2000 PhD dissertation • Twilio was founded in 2007; Stripe in 2011 Photo by Stanford Smith on Unsplash

Slide 29

Slide 29

Serverless Architectures

Slide 30

Slide 30

Source: https://martinfowler.com/bliki/Serverless.html

Slide 31

Slide 31

A NEW BREED OF STATIC SITE GENERATORS Seamlessly combining static sites and single page applications Blazing-fast static site generator for React Universal Vue.js Applications

Slide 32

Slide 32

How does that solve anything? With great services

Slide 33

Slide 33

3 WAYS OF ACCESSING A SERVICE Build Browser Function

Slide 34

Slide 34

01 “Our editors don’t know git, they need a GUI.” A content infrastructure service - or a headless CMS - gives editors the graphical interface they need. You content becomes just an API. Build Browser Function

Slide 35

Slide 35

Browser Static Site Host Build server Content Infrastructure Editors SCM Developers

Slide 36

Slide 36

02 “We have a lot of content, we need full-text search.” Using a Search-as-a-Service provider not only saves you from running your own search infrastructure but also allows integrating multiple content providers into one search. Build Browser

Slide 37

Slide 37

Editors Browser Static Site Host Build server SCM Developers

Slide 38

Slide 38

03 “Our contact forms needs to send an email.” AWS Lambda Cloud email services are a de facto standard in modern architectures but they’re not accessible from the client. A tiny bit of glue code hosted on a FaaS platform bridges the gap. Browser Function

Slide 39

Slide 39

HOW TINY? const sendGridConfig = {api_user: 'SENDGRID_USERNAME', api_key: 'SENDGRID_PASSWORD'}; const transporter = nodemailer.createTransport(sgTransport({auth: sendGridConfig})); const sendMail = pify(transporter.sendMail); exports.handler = function(event, context) { const templateDir = path.join(__dirname, 'templates', event.template); const template = new EmailTemplate(templateDir); const render = pify(template.render); template.render(event.context) .then(result => { event.text = result.text; event.html = result.html; return sendMailAsync(event); }) .then(() => context.done(null, {message: 'Yaay success'}))) .catch(() => context.done(new Error('Something went wrong!'))); };

Slide 40

Slide 40

AWS Lambda Sendgrid Editors Browser Static Site Host Build server SCM Developers

Slide 41

Slide 41

04 “We’re running an online shop. You can’t do that with a static site.” Combing a payment processing service and a shopping cart platform allows you to run your entire business off a static site. Build Browser Function

Slide 42

Slide 42

Snipcart Editors Browser Static Site Host Build server SCM Developers

Slide 43

Slide 43

Bringing it all together

Slide 44

Slide 44

Browser Static Site Host AWS Lambda Snipcart Sendgrid Build server Content Infrastructure Editors SCM Developers

Slide 45

Slide 45

AND MANY MORE

Slide 46

Slide 46

COMBINING STATIC SITES AND SERVERLESS Isolated services improve security Easy caching Every dynamic function is a limited Dynamic resources are hidden behind service with only the necessary permissions. APIs and well defined routes everything else can be cached. Great performance Easy maintenance Static things are instant; dynamic Most services can be handled by 3rd things are fast. parties requiring no maintenance.

Slide 47

Slide 47

Shy Ruparel Developer Evangelist | Contentful @ShyRuparel He/Him Slide Credit - @RouvenWessling