Skip to content

Instantly share code, notes, and snippets.

@jschr
jschr / lambda.tf
Last active April 12, 2017 04:24
lambda infrastructure
resource "aws_lambda_function" "website_generator" {
# the path to the lambda source code
filename = "${var.app_package}"
function_name = "website_generator"
runtime = "nodejs6.10"
# references the role's arn using the resource's
# output variable
role = "${aws_iam_role.website_generator_role.arn}"
# name of the export function
handler = "handler.default"
@jschr
jschr / handler.ts
Last active November 20, 2017 02:10
Handler step 1
import createWebpackConfig from './webpack.config'
const s3 = new AWS.S3()
// the lambda handler
// event contains information from the source of the lambda invocation
// it could be data provided by manually invoking it, a payload from
// API Gateway, an S3 putObject event or an SNS topic message.
// we are going to create a scheduled lambda function through CloudWatch
// and don't actually use the event data
@jschr
jschr / mount.tsx
Last active April 12, 2017 04:15
glamor mount
import * as React from 'react'
import { render } from 'react-dom'
import { rehydrate } from 'glamor'
export default function () {
const ssr = (window as SSRWindow).ssr
rehydrate(ssr.cssIds)
// use require to ensure glamor rehydrate is called
@jschr
jschr / Template.tsx
Last active April 13, 2017 01:43
glamor ssr
import * as React from 'react'
export default function Template({ css, js, body, ssr }) {
return (
<html lang='en' data-timestamp={(new Date()).toISOString()}>
<head>
<title>{ssr.appProps.username}</title>
<style>{css}</style>
</head>
<body>
@jschr
jschr / index.ts
Last active April 12, 2017 04:20
mount step 1
// index.ts imports both the mount and ssr functions
// because it's the entry point for the server and
// the browser
import mount from './mount'
import ssr from './ssr'
// dont mount if we are not running in the browser context
if (typeof document !== 'undefined') mount()
// the exported ssr function is only called by
@jschr
jschr / start_dev_server.sh
Created April 11, 2017 22:49
webpack tip #2
webpack-dev-server --env.FOO bar
@jschr
jschr / Template.tsx
Created April 11, 2017 22:28
Basic template step 1
export default function Template({ body, ssr, js }) {
return (
<html>
<head>
<title>{ssr.appProps.username}</title>
</head>
<body>
<div id='react-root' dangerouslySetInnerHTML={{ __html: body }} />
<script dangerouslySetInnerHTML={{ __html: `window.ssr = ${JSON.stringify(ssr)}` }} />
{ js.map(src => <script src={src} />) }
@jschr
jschr / ssr.tsx
Last active April 12, 2017 04:13
ssr function step1
import * as React from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import Template from './components/Template'
import App from './components/App'
// our simple ssr function accepts appProps and a webpackStats
// object provided by static-website-generator-webpack-plugin
export default function ssr({ webpackStats, appProps }) {
// fetch a list of all the assets created by the webpack compiler
@jschr
jschr / getProps.ts
Created April 11, 2017 21:31
webpack step 2 getProps
export default async function getProps(username) {
const sumary = await getGithubSummary(username)
return summary
}
@jschr
jschr / webpack.config.ts
Last active April 12, 2017 04:13
webpack config step 2
import getProps from './getProps'
// webpack supports exporting a function in your config
// without needing any changes to start the dev server
export default async function createWebpackConfig() {
// fetch our latest app props
const appProps = await getProps({
// variable set by dotenv for development
github: process.env.GITHUB_USERNAME
})