Skip to content

Commit 12939e9

Browse files
author
William Esz
committed
Add polka example
Serves as a generic express middleware-compatible server example. (related to jaredpalmer/after.js#77)
1 parent 358d1bd commit 12939e9

13 files changed

Lines changed: 6773 additions & 0 deletions

File tree

examples/with-polka/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
logs
2+
*.log
3+
npm-debug.log*
4+
.DS_Store
5+
6+
coverage
7+
node_modules
8+
build
9+
public/static
10+
.env.*.local

examples/with-polka/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Razzle Polka Example
2+
3+
## How to use
4+
5+
Download the example [or clone the whole project](https://github.com/jaredpalmer/razzle.git):
6+
7+
```bash
8+
curl https://codeload.github.com/jaredpalmer/razzle/tar.gz/master | tar -xz --strip=2 razzle-master/examples/with-polka
9+
cd with-polka
10+
```
11+
12+
Install it and run:
13+
14+
```bash
15+
yarn install
16+
yarn start
17+
```
18+
19+
## Idea behind the example
20+
21+
An example of how to use a custom, express middleware-compatible server like [polka](https://github.com/lukeed/polka) with razzle. It satisfies the entry points
22+
`src/index.js` for the server and and `src/client.js` for the browser. HMR works for server-side changes too by creating new instances of the polka server handler.

examples/with-polka/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "razzle-examples-with-polka",
3+
"version": "0.8.12",
4+
"license": "MIT",
5+
"scripts": {
6+
"start": "razzle start",
7+
"build": "razzle build",
8+
"test": "razzle test --env=jsdom",
9+
"start:prod": "NODE_ENV=production node build/server.js"
10+
},
11+
"dependencies": {
12+
"polka": "^0.3.4",
13+
"react": "^16.2.0",
14+
"react-dom": "^16.2.0",
15+
"serve-static": "^1.13.2"
16+
},
17+
"devDependencies": {
18+
"razzle": "^0.8.12"
19+
}
20+
}
32.2 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+

examples/with-polka/src/App.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
font-family: -apple-system,
5+
BlinkMacSystemFont,
6+
"Segoe UI",
7+
Helvetica,
8+
Arial,
9+
sans-serif,
10+
"Apple Color Emoji",
11+
"Segoe UI Emoji",
12+
"Segoe UI Symbol";
13+
}

examples/with-polka/src/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import './App.css';
2+
3+
import React from 'react';
4+
const App = () => <div>Welcome to Razzle.</div>;
5+
6+
export default App;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import App from './App';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
5+
describe('<App />', () => {
6+
test('renders without exploding', () => {
7+
const div = document.createElement('div');
8+
ReactDOM.render(<App />, div);
9+
});
10+
});

examples/with-polka/src/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { hydrate } from 'react-dom';
3+
import App from './App';
4+
5+
hydrate(<App />, document.getElementById('root'));
6+
7+
if (module.hot) {
8+
module.hot.accept();
9+
}

examples/with-polka/src/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import app from './server';
2+
import http from 'http';
3+
4+
const server = http.createServer(app);
5+
6+
let currentApp = app;
7+
8+
server.listen(process.env.PORT || 3000, error => {
9+
if (error) {
10+
console.log(error);
11+
}
12+
13+
console.log('🚀 started');
14+
});
15+
16+
if (module.hot) {
17+
console.log('✅ Server-side HMR Enabled!');
18+
19+
module.hot.accept('./server', () => {
20+
console.log('🔁 HMR Reloading `./server`...');
21+
server.removeListener('request', currentApp);
22+
const newApp = require('./server').default;
23+
server.on('request', newApp);
24+
currentApp = newApp;
25+
});
26+
}

0 commit comments

Comments
 (0)