Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/with-polka/.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
22 changes: 22 additions & 0 deletions examples/with-polka/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Razzle Polka Example

## How to use

Download the example [or clone the whole project](https://github.com/jaredpalmer/razzle.git):

```bash
curl https://codeload.github.com/jaredpalmer/razzle/tar.gz/master | tar -xz --strip=2 razzle-master/examples/with-polka
cd with-polka
```

Install it and run:

```bash
yarn install
yarn start
```

## Idea behind the example

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
`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.
20 changes: 20 additions & 0 deletions examples/with-polka/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "razzle-examples-with-polka",
"version": "0.8.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": {
"polka": "^0.3.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"serve-static": "^1.13.2"
},
"devDependencies": {
"razzle": "^0.8.12"
}
}
Binary file added examples/with-polka/public/favicon.ico
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/with-polka/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *

13 changes: 13 additions & 0 deletions examples/with-polka/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/with-polka/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/with-polka/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/with-polka/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();
}
26 changes: 26 additions & 0 deletions examples/with-polka/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import app from './server';
import http from 'http';

const server = http.createServer(app);

let currentApp = app;

server.listen(process.env.PORT || 3000, error => {
if (error) {
console.log(error);
}

console.log('🚀 started');
});

if (module.hot) {
console.log('✅ Server-side HMR Enabled!');

module.hot.accept('./server', () => {
console.log('🔁 HMR Reloading `./server`...');
server.removeListener('request', currentApp);
const newApp = require('./server').default;
server.on('request', newApp);
currentApp = newApp;
});
}
37 changes: 37 additions & 0 deletions examples/with-polka/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import App from './App';
import React from 'react';
import polka from 'polka';
import serve from 'serve-static';
import { renderToString } from 'react-dom/server';

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

export default polka()
.use(serve(process.env.RAZZLE_PUBLIC_DIR))
.get('/*', (req, res) => {
const markup = renderToString(<App />);
res.end(
`<!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">
${
assets.client.css
? `<link rel="stylesheet" href="${assets.client.css}">`
: ''
}
${
process.env.NODE_ENV === 'production'
? `<script src="${assets.client.js}" defer></script>`
: `<script src="${assets.client.js}" defer crossorigin></script>`
}
</head>
<body>
<div id="root">${markup}</div>
</body>
</html>`
);
}).handler;