Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jgeschwendt committed Nov 12, 2018
1 parent 735129b commit 9bd8e33
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 70 deletions.
16 changes: 15 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
service: serverless-react
service: serverless-react-ssr

frameworkVersion: '>=1.9.0 <2.0.0'

Expand Down Expand Up @@ -89,6 +89,17 @@ resources:
S3WebsiteEndpoint: s3-website-us-west-2.amazonaws.com

Resources:
ApiGatewayMethodProxyVarGet:
Properties:
Integration:
CacheKeyParameters:
- method.request.path.proxy
CacheNamespace: ApiGatewayMethodProxyVarGetCacheNS
RequestParameters:
integration.request.path.proxy: method.request.path.proxy
RequestParameters:
method.request.path.proxy: true

ApiGatewayBasePathMapping:
DependsOn: ApiGatewayDomainName
Type: AWS::ApiGateway::BasePathMapping
Expand Down Expand Up @@ -166,6 +177,9 @@ resources:
AuthorizationType: NONE
HttpMethod: GET
Integration:
CacheKeyParameters:
- method.request.path.proxy
CacheNamespace: ApiGatewayMethodStaticProxyVarGetCacheNS
Credentials: {'Fn::Join': ['', ['arn:aws:iam::', {Ref: AWS::AccountId}, ':role/', {Ref: ApiGatewayIamRoleExecution}]]}
IntegrationHttpMethod: GET
IntegrationResponses:
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/browser/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class extends ApolloClient<any> {
super({
cache: new InMemoryCache({
fragmentMatcher: new IntrospectionFragmentMatcher({
introspectionQueryResultData: window.__APOLLO_FRAGS__
introspectionQueryResultData: window.__APOLLO_SCHEMA__
})
}).restore(window.__APOLLO_STATE__),
link: new HttpLink({ fetch, uri }),
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/browser/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { renderRoutes } from 'react-router-config'
import { BrowserRouter } from 'react-router-dom'
import { ThemeProvider } from 'styled-components'

export default ({ client, context, location, routes, store, theme }) => (
export default ({ client, location, routes, store, theme }) => (
<Provider store={store}>
<ApolloProvider client={client}>
<ThemeProvider theme={theme}>
<BrowserRouter location={location} context={context}>
<BrowserRouter location={location}>
{renderRoutes(routes)}
</BrowserRouter>
</ThemeProvider>
Expand Down
3 changes: 1 addition & 2 deletions src/platforms/browser/hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import App from './components/App'

const store = createStore(reducer , window.__REDUX_STATE__, (window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose)())

export default (element, context = {}) => {
export default (element) => {
loadableReady(() => {
hydrate(createElement(App, {
client: new ApolloClient({ uri: `https:${process.env.API_DOMAIN_NAME}` }),
context,
location: window.location.pathname || '/',
routes,
store,
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/browser/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { default as Hydrate } from './hydrate'

Hydrate(document.getElementById('root'), window.__LAMBDA_STATE__)
Hydrate(document.getElementById('root'))
3 changes: 1 addition & 2 deletions src/platforms/lambda/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { StyleSheetManager, ThemeProvider } from 'styled-components'

export default ({
client,
context,
extractor,
location,
routes,
Expand All @@ -22,7 +21,7 @@ export default ({
<ApolloProvider client={client}>
<StyleSheetManager sheet={sheet}>
<ThemeProvider theme={theme}>
<StaticRouter location={location} context={context}>
<StaticRouter location={location}>
{renderRoutes(routes)}
</StaticRouter>
</ThemeProvider>
Expand Down
6 changes: 2 additions & 4 deletions src/platforms/lambda/document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export default ({
content,
context,
extractor,
helmet,
introspectionQueryResultData,
Expand All @@ -21,10 +20,9 @@ export default ({
<body ${helmet.bodyAttributes.toString()}>
<div id="root">${content}</div>
<script>${`
window.__APOLLO_SCHEMA__=${JSON.stringify(introspectionQueryResultData).replace(/</g, '\\u003c')};
window.__APOLLO_STATE__=${JSON.stringify(state).replace(/</g, '\\u003c')};
window.__APOLLO_FRAGS__=${JSON.stringify(introspectionQueryResultData).replace(/</g, '\\u003c')};
window.__LAMBDA_STATE__=${JSON.stringify(context).replace(/</g, '\\u003c')};
window.__REDUX_STATE__=${JSON.stringify(store.getState()).replace(/</g, '\\u003c')};
window.__REDUX_STORE__=${JSON.stringify(store.getState()).replace(/</g, '\\u003c')};
`}</script>
${extractor.getScriptTags()}
<script src="/static/vendors.js"></script>
Expand Down
13 changes: 8 additions & 5 deletions src/platforms/lambda/fragment-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ export default ({ uri }) => fetch(uri, {
})
.then(result => result.json())
.then(result => {
// here we're filtering out any type information unrelated to unions or interfaces
result.data.__schema.types = result.data.__schema.types.filter(
type => type.possibleTypes !== null
)
return result.data
if (result && result.data && result.data.__schema && result.data.__schema.types) {
// here we're filtering out any type information unrelated to unions or interfaces
result.data.__schema.types = result.data.__schema.types.filter(
type => type.possibleTypes !== null
)
return result.data
}
return null
})
11 changes: 11 additions & 0 deletions src/platforms/lambda/middleware/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default async (event, context, callback) => {
callback(null, {
body: process.env.STAGE === 'prod'
? 'User-agent: *\nDisallow:\nSitemap: https://' + process.env.APP_DOMAIN_NAME + '/sitemap.xml\n'
: 'User-agent: *\nDisallow: /\n',
headers: {
'Access-Control-Allow-Origin': '*'
},
statusCode: 200
})
}
30 changes: 30 additions & 0 deletions src/platforms/lambda/middleware/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as sitemap from 'sitemap'

const sitemapDocument = async () => new Promise((resolve, reject) => {
const mapping = sitemap.createSitemap({
cacheTime: 600000,
hostname: process.env.API_DOMAIN_NAME,
urls: [
{ url: '/' }
]
})

mapping.toXML((error, xml) => {
if (error) {
reject(error)
} else {
resolve(xml)
}
})
})

export default async (event, context, callback) => {
callback(null, {
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
'Access-Control-Allow-Origin': '*'
},
body: await sitemapDocument()
})
}
11 changes: 6 additions & 5 deletions src/platforms/lambda/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import reducer from '../../app/reducer'
import routes from '../../app/routes'
import theme from '../../app/theme'

export default async ({ context, location }) => {
export default async ({ location }) => {
const { client, introspectionQueryResultData } = await LambdaContainerCache
const statsFile = process.env.IS_OFFLINE
? resolve('.webpack/service/loadable-stats.json')
: resolve('loadable-stats.json')
const store = createStore(reducer)

await client.resetStore() // the cache persists between lambda requests
const extractor = new ChunkExtractor({ entrypoints: [], statsFile: resolve('.webpack/service/loadable-stats.json') })

const extractor = new ChunkExtractor({ entrypoints: [], statsFile })
const sheet = new ServerStyleSheet()

const content = await renderToStringWithData(
createElement(App, {
client,
context,
extractor,
location,
routes,
Expand All @@ -41,7 +43,6 @@ export default async ({ context, location }) => {

return HTML({
content,
context,
extractor,
introspectionQueryResultData,
helmet,
Expand Down
30 changes: 5 additions & 25 deletions src/platforms/lambda/server.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import render from './render'
import sitemap from './sitemap'
import robots from './middleware/robots'
import sitemap from './middleware/sitemap'

const server = async (event, context, callback) => {
switch (event.path) {
case '/sitemap.xml':
callback(null, {
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
'Access-Control-Allow-Origin': '*'
},
body: await sitemap({ uri: process.env.API_DOMAIN_NAME })
})
return

case '/robots.txt':
callback(null, {
body: process.env.STAGE === 'prod'
? 'User-agent: *\nDisallow:\nSitemap: https://' + process.env.APP_DOMAIN_NAME + '/sitemap.xml\n'
: 'User-agent: *\nDisallow: /\n',
headers: {
'Access-Control-Allow-Origin': '*'
},
statusCode: 200
})
return

case '/sitemap.xml': await sitemap(event, context, callback); return
case '/robots.txt': await robots(event, context, callback); return
default:
const html = await render({ context, location: event.path })
const html = await render({ location: event.path })
const body = `<!DOCTYPE html>${html}`

callback(null, {
Expand Down
20 changes: 0 additions & 20 deletions src/platforms/lambda/sitemap.ts

This file was deleted.

3 changes: 1 addition & 2 deletions typings/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ declare module '*.json' {
}

interface Window {
__APOLLO_FRAGS__: any
__APOLLO_SCHEMA__: any
__APOLLO_STATE__: any
__LAMBDA_STATE__: any
__REDUX_STATE__: any
}

0 comments on commit 9bd8e33

Please sign in to comment.