Skip to content

Commit

Permalink
feat: add serverless example
Browse files Browse the repository at this point in the history
  • Loading branch information
fivethreeo committed Dec 20, 2021
1 parent 4664b60 commit 30e3ee4
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/basic-serverless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Razzle Basic Example

## How to use

<!-- START install generated instructions please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN yarn update-examples TO UPDATE -->Create and start the example:

```bash
npx create-razzle-app --example basic basic

cd basic
yarn start
```
<!-- END install generated instructions please keep comment here to allow auto update -->


## Idea behind the example
This is a basic, bare-bones example of how to use razzle. It satisfies the entry points
`src/index.js` for the server and and `src/client.js` for the browser.
10 changes: 10 additions & 0 deletions examples/basic-serverless/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
logs
*.log
npm-debug.log*
.DS_Store

coverage
node_modules
build
public/static
.env.*.local
25 changes: 25 additions & 0 deletions examples/basic-serverless/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "razzle-examples-basic-serverless",
"version": "4.2.12",
"license": "MIT",
"scripts": {
"start": "razzle start",
"build": "razzle build",
"test": "razzle test --env=jsdom",
"start:prod": "NODE_ENV=production node build/server.js"
},
"dependencies": {
"express": "^4.17.1",
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"devDependencies": {
"razzle": "4.2.12",
"razzle-dev-utils": "4.2.12",
"mini-css-extract-plugin": "^0.9.0",
"html-webpack-plugin": "^4.5.2",
"webpack": "^4.44.1",
"babel-preset-razzle": "4.2.12",
"webpack-dev-server": "^3.11.2"
}
}
Binary file added examples/basic-serverless/public/favicon.ico
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/basic-serverless/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *

7 changes: 7 additions & 0 deletions examples/basic-serverless/razzle.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
options: {
buildType: 'iso-serverless',
}
};
5 changes: 5 additions & 0 deletions examples/basic-serverless/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"container": {
"port": 3000
}
}
13 changes: 13 additions & 0 deletions examples/basic-serverless/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
padding: 0;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Helvetica,
Arial,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol";
}
6 changes: 6 additions & 0 deletions examples/basic-serverless/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './App.css';

import React from 'react';
const App = () => <div>Welcome to Razzle.</div>;

export default App;
10 changes: 10 additions & 0 deletions examples/basic-serverless/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import App from './App';
import React from 'react';
import ReactDOM from 'react-dom';

describe('<App />', () => {
test('renders without exploding', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
});
9 changes: 9 additions & 0 deletions examples/basic-serverless/src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import { hydrate } from 'react-dom';
import App from './App';

hydrate(<App />, document.getElementById('root'));

if (module.hot) {
module.hot.accept();
}
27 changes: 27 additions & 0 deletions examples/basic-serverless/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}

const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
if (err) {
console.error(err);
return;
}
console.log(`> Started on port ${port}`);
});
55 changes: 55 additions & 0 deletions examples/basic-serverless/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import App from './App';
import React from 'react';
import express from 'express';
import { renderToString } from 'react-dom/server';

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);

const cssLinksFromAssets = (assets, entrypoint) => {
return assets[entrypoint] ? assets[entrypoint].css ?
assets[entrypoint].css.map(asset=>
`<link rel="stylesheet" href="${asset}">`
).join('') : '' : '';
};

const jsScriptTagsFromAssets = (assets, entrypoint, extra = '') => {
return assets[entrypoint] ? assets[entrypoint].js ?
assets[entrypoint].js.map(asset=>
`<script src="${asset}"${extra}></script>`
).join('') : '' : '';
};

export const renderApp = (req, res) => {
const markup = renderToString(<App />);

const html =
// prettier-ignore
`<!doctype html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charSet='utf-8' />
<title>Welcome to Razzle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${cssLinksFromAssets(assets, 'client')}
</head>
<body>
<div id="root">${markup}</div>
${jsScriptTagsFromAssets(assets, 'client', ' defer crossorigin')}
</body>
</html>`;

return { html };
};

const server = express();

server
.disable('x-powered-by')
.use(express.static(process.env.RAZZLE_PUBLIC_DIR))
.get('/*', (req, res) => {
const { html } = renderApp(req, res);
res.send(html);
});

export default server;

1 comment on commit 30e3ee4

@vercel
Copy link

@vercel vercel bot commented on 30e3ee4 Dec 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.