diff --git a/examples/with-polka/.gitignore b/examples/with-polka/.gitignore new file mode 100644 index 000000000..13d409785 --- /dev/null +++ b/examples/with-polka/.gitignore @@ -0,0 +1,10 @@ +logs +*.log +npm-debug.log* +.DS_Store + +coverage +node_modules +build +public/static +.env.*.local \ No newline at end of file diff --git a/examples/with-polka/README.md b/examples/with-polka/README.md new file mode 100644 index 000000000..66ae94e06 --- /dev/null +++ b/examples/with-polka/README.md @@ -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. diff --git a/examples/with-polka/package.json b/examples/with-polka/package.json new file mode 100644 index 000000000..25b429b81 --- /dev/null +++ b/examples/with-polka/package.json @@ -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" + } +} diff --git a/examples/with-polka/public/favicon.ico b/examples/with-polka/public/favicon.ico new file mode 100644 index 000000000..46bceb8c7 Binary files /dev/null and b/examples/with-polka/public/favicon.ico differ diff --git a/examples/with-polka/public/robots.txt b/examples/with-polka/public/robots.txt new file mode 100644 index 000000000..54fb98911 --- /dev/null +++ b/examples/with-polka/public/robots.txt @@ -0,0 +1,2 @@ +User-agent: * + diff --git a/examples/with-polka/src/App.css b/examples/with-polka/src/App.css new file mode 100644 index 000000000..e26469b20 --- /dev/null +++ b/examples/with-polka/src/App.css @@ -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"; +} diff --git a/examples/with-polka/src/App.js b/examples/with-polka/src/App.js new file mode 100644 index 000000000..d8840a547 --- /dev/null +++ b/examples/with-polka/src/App.js @@ -0,0 +1,6 @@ +import './App.css'; + +import React from 'react'; +const App = () =>
Welcome to Razzle.
; + +export default App; diff --git a/examples/with-polka/src/App.test.js b/examples/with-polka/src/App.test.js new file mode 100644 index 000000000..17cf4a181 --- /dev/null +++ b/examples/with-polka/src/App.test.js @@ -0,0 +1,10 @@ +import App from './App'; +import React from 'react'; +import ReactDOM from 'react-dom'; + +describe('', () => { + test('renders without exploding', () => { + const div = document.createElement('div'); + ReactDOM.render(, div); + }); +}); diff --git a/examples/with-polka/src/client.js b/examples/with-polka/src/client.js new file mode 100644 index 000000000..fbbe57121 --- /dev/null +++ b/examples/with-polka/src/client.js @@ -0,0 +1,9 @@ +import React from 'react'; +import { hydrate } from 'react-dom'; +import App from './App'; + +hydrate(, document.getElementById('root')); + +if (module.hot) { + module.hot.accept(); +} diff --git a/examples/with-polka/src/index.js b/examples/with-polka/src/index.js new file mode 100644 index 000000000..4f1c74d97 --- /dev/null +++ b/examples/with-polka/src/index.js @@ -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; + }); +} diff --git a/examples/with-polka/src/server.js b/examples/with-polka/src/server.js new file mode 100644 index 000000000..7d245b40e --- /dev/null +++ b/examples/with-polka/src/server.js @@ -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(); + res.end( + ` + + + + + Welcome to Razzle + + ${ + assets.client.css + ? `` + : '' + } + ${ + process.env.NODE_ENV === 'production' + ? `` + : `` + } + + +
${markup}
+ +` + ); + }).handler;