Implement PWA functionality (#455)

* implement PWA functionality

* disable tweet button when offline

* just remove tweet button instead
main
Michael Fix 6 years ago committed by GitHub
parent 95f0a27111
commit f018c454bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ export default props => (
color: props.color, color: props.color,
border: `1px solid ${props.color}` border: `1px solid ${props.color}`
}} }}
disabled={props.disabled}
> >
<span>{props.title}</span> <span>{props.title}</span>
<style jsx> <style jsx>

@ -46,7 +46,8 @@ class Editor extends React.Component {
...DEFAULT_SETTINGS, ...DEFAULT_SETTINGS,
loading: true, loading: true,
uploading: false, uploading: false,
code: props.content code: props.content,
online: true
} }
this.save = this.save.bind(this) this.save = this.save.bind(this)
@ -61,6 +62,9 @@ class Editor extends React.Component {
this.resetDefaultSettings = this.resetDefaultSettings.bind(this) this.resetDefaultSettings = this.resetDefaultSettings.bind(this)
this.getCarbonImage = this.getCarbonImage.bind(this) this.getCarbonImage = this.getCarbonImage.bind(this)
this.onDrop = this.onDrop.bind(this) this.onDrop = this.onDrop.bind(this)
this.setOffline = () => this.setState({ online: false })
this.setOnline = () => this.setState({ online: true })
} }
componentDidMount() { componentDidMount() {
@ -68,8 +72,17 @@ class Editor extends React.Component {
this.setState({ this.setState({
...getState(localStorage), ...getState(localStorage),
...this.props.initialState, ...this.props.initialState,
loading: false loading: false,
online: Boolean(window && window.navigator && window.navigator.onLine)
}) })
window.addEventListener('offline', this.setOffline);
window.addEventListener('online', this.setOnline);
}
componentWillUnmount() {
window.removeEventListener('offline', this.setOffline);
window.removeEventListener('online', this.setOnline);
} }
componentDidUpdate() { componentDidUpdate() {
@ -238,7 +251,7 @@ class Editor extends React.Component {
resetDefaultSettings={this.resetDefaultSettings} resetDefaultSettings={this.resetDefaultSettings}
/> />
<div className="buttons"> <div className="buttons">
{this.props.tweet && ( {this.props.tweet && this.state.online && (
<Button <Button
className="tweetButton" className="tweetButton"
onClick={this.upload} onClick={this.upload}

@ -37,6 +37,7 @@ export default () => {
<meta name="og:image" content="/static/banner.png" /> <meta name="og:image" content="/static/banner.png" />
<meta name="theme-color" content="#121212" /> <meta name="theme-color" content="#121212" />
<title>Carbon</title> <title>Carbon</title>
<link rel='manifest' href='/static/manifest.json' />
<link rel="shortcut icon" href="/static/favicon.ico" /> <link rel="shortcut icon" href="/static/favicon.ico" />
<link rel="stylesheet" href="/_next/static/style.css" /> <link rel="stylesheet" href="/_next/static/style.css" />
<link <link

@ -1,3 +1,4 @@
const withCSS = require('@zeit/next-css') const withCSS = require('@zeit/next-css')
/* Without CSS Modules, with PostCSS */ const withOffline = require('next-offline')
module.exports = withCSS()
module.exports = withOffline(withCSS())

@ -37,6 +37,7 @@
"morphmorph": "^0.1.0", "morphmorph": "^0.1.0",
"ms": "^2.0.0", "ms": "^2.0.0",
"next": "^6.0.3", "next": "^6.0.3",
"next-offline": "^2.9.0",
"now-logs": "^0.0.7", "now-logs": "^0.0.7",
"puppeteer": "1.4.0", "puppeteer": "1.4.0",
"react": "16.3.*", "react": "16.3.*",

@ -1,3 +1,4 @@
const path = require('path')
const express = require('express') const express = require('express')
const morgan = require('morgan') const morgan = require('morgan')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
@ -45,6 +46,8 @@ app
server.use(morgan('tiny')) server.use(morgan('tiny'))
} }
const filePath = path.join(__dirname, '.next', 'service-worker.js')
server.get('/service-worker.js', (req, res) => app.serveStatic(req, res, filePath))
// api endpoints // api endpoints
server.post('/twitter', bodyParser.json({ limit: '5mb' }), require('./handlers/twitter')) server.post('/twitter', bodyParser.json({ limit: '5mb' }), require('./handlers/twitter'))
server.post('/image', bodyParser.json({ limit: '5mb' }), wrap(imageHandler)) server.post('/image', bodyParser.json({ limit: '5mb' }), wrap(imageHandler))

@ -0,0 +1,18 @@
{
"name": "Carbon",
"short_name": "Carbon",
"background_color": "#121212",
"theme_color": "#121212",
"description": "Carbon is the easiest way to create and share beautiful images of your source code.",
"display": "standalone",
"start_url": "/",
"icons": [{
"src": "/static/brand/icon.png",
"type": "image/png",
"sizes": "448x448"
}, {
"src": "/static/brand/logo-square.png",
"type": "image/png",
"sizes": "1024x1024"
}]
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save