• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Serverless pre-rendering (SSR) for React SPA using AWS Lambda, S3, and CloudFront.

Serverless SSR for create-react-app

MIT License sbstjn.com superluminar.io

Full-featured AWS architecture to use Server-Side Rendering for any create-react-app project using Lambda, API Gateway, CloudFront. All infrastructure is configured using the AWS Cloud Development Kit and can be deployed using AWS CodePipeline and AWS CodeBuild.

Have fun, and be a nice anti-fascist human being!

https://d31tuk9nqnnpkk.cloudfront.net/

Guide (EN): React with server-side rendering on AWS Lambda on sbstjn.com
Guide (DE): React und server-side rendering mit AWS Lambda und CloudFront on superluminar.io

Whenever you search for server-side rendering of React applications, you will read it's hard to accomplish. But why? Most probably, you'll end up with frameworks like razzle or Next.js only because you wanted a little bit of pre-rendered HTML for your React application.

The idea of cra-serverless is pretty simple: Use your existing, un-ejected, and unpatched create-react-app and replace BrowserRouter with StaticRouter, deploy it to AWS Lambda, and finally put a CDN in front of it. You'll have your same React SPA, but now you can have pre-rendered HTML content for all routes in your application. This even works fine with frameworks like styled-components or apollo client for using GraphQL on AppSync.

React SPA with serverless pre-rendering

Yes, this is serverless server-side rendering, so let's call it serverless pre-rendering for React. ๐Ÿคฏ

Extended Guides:

Architecture

Usage

The primary goal for cra-serverless is to use a default create-react-app setup without any changes and avoid to eject it from react-scripts.

# Start your local environment as always
$ > yarn start

# Build your static SPA as always
$ > yarn build

You can develop and build SPA with the usual flow. Afterwards, you can run a local HTTP server with koa to test-drive your server-side rendering. The built project is intended to be uploaded to AWS Lambda, but you can deploy the Node.js application to other services as well.

# Start local server-side rendering service
$ > yarn run:local

# Build Node.js service for server-side rendering
$ > yarn build:server

All full-featured pipeline using AWS CodePipeline and AWS CodeBuild using the AWS Cloud Development Kit is included to support continious deployments. (Jump to Deployments and Configuration)

How it Works - In a Nutshell

Most React applications use the react-router-dom with BrowserRouter :

import React from 'react'
import { BrowserRouter } from 'react-router-dom'

React.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById('root'),
)

Lucky us, a StaticRouter exists as well and react-dom has a function called renderToString :

import { renderToString } from 'react-dom/server'
import { StaticRouter } from 'react-router-dom'

const markup = renderToString(
  <StaticRouter location={path}>
    <App />
  </StaticRouter>,
)

Next, the value of markup will be injected into your index.html file and that's it. In a Nutshell, cra-serverless uses existing features of the frameworks you already use and wraps them in a serverless architecture with AWS Lambda.

Deployments and Configuration

Based on cra-pipeline, all you need is a personal GitHub Access Token and your own fork of this repository. If you have both things in place, store the token in AWS Systems Manager:

$ > aws secretsmanager create-secret \
    --name GitHubToken \
    --secret-string abcdefg1234abcdefg56789abcdefg \
    --region us-east-1

{
  "ARN": "arn:aws:secretsmanager:us-east-1:123456789001:secret:GitHubToken-uNBxTr",
  "Name": "GitHubToken",
  "VersionId": "4acda3d1-877f-4032-b38e-17bc50239883"
}

As everything is configured using AWS Cloud Development Kit, you need to prepare (aka bootstrap) your AWS account once for the usage of the CDK in your desired AWS region:

$ > yarn cdk bootstrap --region us-east-1

โณ  Bootstrapping environment aws://123456789001/us-east-1...

0/2 | 5:06:49 PM | CREATE_IN_PROGRESS   | AWS::S3::Bucket | StagingBucket
0/2 | 5:06:50 PM | CREATE_IN_PROGRESS   | AWS::S3::Bucket | StagingBucket Resource creation Initiated
1/2 | 5:07:11 PM | CREATE_COMPLETE      | AWS::S3::Bucket | StagingBucket

โœ…  Environment aws://123456789001/us-east-1 bootstrapped.

Next, have a look at the ./config.ts file in the root folder and configure at least the github section of the file.

export const config = {
  name: 'cra-serverless',
  github: {
    owner: 'sbstjn',
    repository: 'cra-serverless',
  },
  env: { region: 'us-east-1' },
}

After changing the GitHub repository information, just deploy the CloudFormation stack for the included AWS CodePipeline and all resources will be created for you.

$ > yarn cdk deploy cra-serverless-pipeline

Pipeline: deploying...
Pipeline: creating CloudFormation changeset...

โœ…  Pipeline

Head over to the AWS Management Console and watch the beauty of a deploy pipeline and CloudFormation stacks. All resources will be created for you, and after a while a CloudFront Distribution is available for the included example application.

Further Reading

License

Feel free to use the code, it's released using the MIT license.

Contribution

You are welcome to contribute to this project! ๐Ÿ˜˜

To make sure you have a pleasant experience, please read the code of conduct. It outlines core values and beliefs and will make working together a happier experience.

More Repositories

1

timesheet.js

JavaScript library for HTML5 & CSS3 time sheets
JavaScript
6,946
star
2

noduino

JavaScript and Node.js Framework for controlling Arduino with HTML and WebSockets
CSS
1,229
star
3

serverless-analytics

Track website visitors with Serverless Analytics using Kinesis, Lambda, and TypeScript.
CSS
241
star
4

sqs-worker-serverless

Example for SQS Worker in AWS Lambda using Serverless
JavaScript
178
star
5

hanu

Golang Framework for writing Slack bots
Go
148
star
6

serverless-dynamodb-autoscaling

Serverless Plugin for Amazon DynamoDB Auto Scaling configuration.
TypeScript
148
star
7

serverless-stack-output

Serverless plugin to process AWS CloudFormation Stack Output
TypeScript
92
star
8

node-blog

Markdown Blog written in Node.js with twitter bootstrap UI and ace editor for composingโ€ฆ
JavaScript
83
star
9

68Keys.io

Build your own 68% Custom Mechanical Keyboard.
Sass
61
star
10

appsync-resolvers-example

Example project for AppSync, GraphQL, and AWS Lambda resolvers using Go.
Go
61
star
11

allot

Parse placeholder and wildcard text commands
Go
56
star
12

go-lambda-example

Example AWS Lambda function in Go and SAM
Go
49
star
13

serverless-geoip

Use MaxMind GeoLite2 database with AWS Lambda
JavaScript
43
star
14

appsync-resolvers

AWS AppSync Resolvers for GraphQL using AWS Lambda functions in Go.
Go
41
star
15

cra-pipeline

Use AWS CDK with CodePipeline to deploy a TypeScript Create-React-App.
TypeScript
40
star
16

serverless-sqs-alarms-plugin

Serverless wrapper to setup CloudWatch Alarms for SQS
JavaScript
36
star
17

lawos

SQS Worker for AWS Lambda.
JavaScript
29
star
18

go-appsync-graphql-cloudformation

AWS AppSync GraphQL API Proxy with Lambda, CloudFormation, and SAM
Makefile
29
star
19

serverless-s3bucket-sync

Serverless Plugin to sync local folders with an S3 bucket
TypeScript
25
star
20

mongoclikker

MongoDB viewer and editor written in Node
JavaScript
22
star
21

latenz

JavaScript HTTP latency analyzer
JavaScript
20
star
22

cdk-lambda-fleet

Deploy AWS Lambda functions using docker images to ECR with AWS CDK
TypeScript
19
star
23

github-releases-slack

Notify Slack about GitHub Releases.
Go
17
star
24

kirbycms-oauth-consumer

Kirby CMS Plugin for using Twitter and GitHub OAuth login
PHP
14
star
25

typescript-npm-boilerplate

Publish to NPM with TypeScript, CircleCI and GitHub Releases.
TypeScript
13
star
26

node-arduino-socket

Simple node.js application for piping a serial connection over websockets to your browser
JavaScript
12
star
27

DropFile.php

HTML, JavaScript and PHP file uploader
JavaScript
11
star
28

liveblog.js

Node.js App for Liveblogs and Event Tickers with Socket.IO and Express.js
JavaScript
10
star
29

aws-lambda-docker-node

Continuous Integration and Continuous Deployment setup for AWS Lambda functions with Docker images using GitHub Actions.
Makefile
10
star
30

node-kickstart

Simple Node.js example app with support for jade, less, ssl certificatesโ€ฆ
JavaScript
8
star
31

requirejs-express

Example app using Express and RequireJS in Node.js
JavaScript
8
star
32

lawos-serverless

Serverless example for an SQS Worker in AWS Lambda using Lawos
JavaScript
7
star
33

appsync-example-dynamodb

GraphQL API using AWS AppSync, SAM, and DynamoDB
Makefile
7
star
34

serverless-alexa-skill

Serverless example for a custom Amazon Alexa Skill
JavaScript
7
star
35

faas.club

Curated list for serverless resources and everything for Functions as a Service at https://faas.club
JavaScript
7
star
36

taco.js

Rating awesomeness with Express.js
CSS
5
star
37

node-markdownblog

Engine for managing markdown filesโ€ฆ
JavaScript
4
star
38

cdk-template

My GitHub Template for AWS CDK projects and GitHub actions.
TypeScript
4
star
39

node-kickstart-example

Example application for node-kickstart
JavaScript
4
star
40

node-simple-cdn

Simple CDN host ballancing for node
JavaScript
4
star
41

faas-sam-lambda-go

AWS Serverless Application Model w/ Go
Go
4
star
42

XMLBuilder.js

Create XML data with JavaScript
JavaScript
4
star
43

alexa-example-skill

Example code of a custom Skill for Amazon Echo & Alexa.
JavaScript
4
star
44

archive

Amazon S3 Replication with KMS, CDK, and CloudFormation StackSets.
TypeScript
4
star
45

SimplePHP

A bunch of static PHP classes making everyday code easier and comfortable.
PHP
4
star
46

hanu-example

Example usage of the hanu Go framework
Go
3
star
47

rdm

Scaffold common GitHub project files like README, CoC, or LICENSE.md
Go
3
star
48

php-jade

PHP example app using jade template engine
PHP
3
star
49

faas-sam-lambda-typescript

AWS Serverless Application Model w/ TypeScript
TypeScript
3
star
50

SocialStream.php

PHP Framework to fetch your recent Social Media events
PHP
3
star
51

super-arduino-bro

Basic pixel Jump'n'Run for 8x8 RGB LED matrix and Arduino โ€” v. 0.1
C++
3
star
52

gatsby-disable-404

Disable 404 pages for Gatsby websites
TypeScript
2
star
53

lambda-error

Node.js error object for streamlined errors using AWS Lambda and API Gateway
JavaScript
2
star
54

Snug.php

Static Page Generator using PHP, HAML and LESS
PHP
1
star
55

actions

GitHub Actions
Dockerfile
1
star
56

reng

Release Name Generator in PHP 5.3 - aka. handy-hyena
PHP
1
star
57

sqs-object

Convert JavaScript objects to SQS Meta Attributes
JavaScript
1
star
58

cdk-encrypted-bucket

Example for polyglot CDK construct for NPM, PyPi, and NuGet
TypeScript
1
star
59

gauges-referrer

Tiny Node.JS Helper for the Gaug.es API
JavaScript
1
star
60

pinged.php

Lightweight PHP logging service with HTTP interface
PHP
1
star
61

aws-codepipeline-example

Go
1
star
62

SimpleObjects

A bunch of PHP objects making everyday code easier and comfortable
PHP
1
star
63

node-proclog

Basic event logger with Node and MongoDB and Websocket Live Monitoring
1
star
64

Boilerplate.swift

Swift3 code with UnitTests that run on macOS, tvOS, iOS, watchOS and Linux.
Swift
1
star
65

discogs-collection

Export your Discogs.com collection value to CloudWatch Metrics
TypeScript
1
star
66

circleci-orbs

CircleCI Orbs for common tasks.
1
star
67

stopper

Minimal stopwatch object to count and measure time in JavaScript.
JavaScript
1
star
68

falke

Alexa skill to check when a Tatort is broadcasted
JavaScript
1
star
69

feedback

Command line feedback for Go
Go
1
star
70

node-cloudstream

Query latest items stored at http://getCloudApp.com
JavaScript
1
star
71

docker-node

Toolbox for Node.
Dockerfile
1
star
72

SliPHP

Atomic view and layout engine for PHP 5.4 and above.
PHP
1
star
73

aws-edukit-example

Example baseline for prototyping with the Core2 AWS IoT EduKit.
C
1
star
74

layers

Manage, build, and deploy AWS Lambda Layers
Makefile
1
star
75

faas-sam-lambda-python

AWS Serverless Application Model w/ Python
Python
1
star
76

function-path

Deeplink into nested JavaScript module functions
TypeScript
1
star
77

faas-makefile

Reusable Makefile to bootstrap AWS::Serverless CloudFormation projects
Makefile
1
star
78

docker-go

Docker image for CircleCI projects using Golang.
Dockerfile
1
star